Linux Shell多选列表失败

Linux Shell多选列表失败,linux,shell,Linux,Shell,上面的代码旨在显示中的所有文件。并让usr选择它们,然后打印用户选择的文件名。但我发现这个序列可以成功打印 #!/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 fi

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

#!/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

有人能帮我解决这个问题吗?第二种方法失败的原因?

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

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
        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