gpt4 book ai didi

linux - Shell 多选列表失败

转载 作者:太空宇宙 更新时间:2023-11-04 09:06:47 25 4
gpt4 key购买 nike

#!/bin/bash

ARG_ARRAY=(num1 num2 num3 num4 num5 num6 num7 num8 num9 num10)
dir=$(find . -type f)
i=0

for f in ${dir[@]};do
printf "$((++i)) $f "
done

printf "\nPlease select the files:\n"
echo "Using the numbers like: 1 2 4 5 "

read ${ARG_ARRAY[@]}


echo $num1
echo $num2
echo $num3
echo $num4
echo $num5
echo $num6
echo $num7
echo $num8
echo $num9
echo $num10

for f in ${ARG_ARRAY[@]}
do
var=$f
echo ${$var}
done

上面的代码旨在显示 .让用户选择它们,然后打印用户选择的文件名。但是我发现这个序列可以成功打印

echo $num1
echo $num2
echo $num3
echo $num4
echo $num5
echo $num6
echo $num7
echo $num8
echo $num9
echo $num10

虽然这会出错

for f in ${ARG_ARRAY[@]}
do
var=$f
echo ${$var}
done


./test.sh: line 30: num1: command not found

./test.sh: line 30: num2: command not found

./test.sh: line 30: num3: command not found

./test.sh: line 30: num4: command not found

./test.sh: line 30: num5: command not found

./test.sh: line 30: num6: command not found

./test.sh: line 30: num7: command not found

./test.sh: line 30: num8: command not found

./test.sh: line 30: num9: command not found

./test.sh: line 30: num10: command not found

谁能帮我解决这个问题?为什么第二种方式失败了?

最佳答案

您可以为此目的使用eval,例如:

for f in ${ARG_ARRAY[@]}
do
var=$f
eval echo \$$var
done

关于linux - Shell 多选列表失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9480473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com