我没有得到任何打印结果,但我不明白为什么?
read -e -i "no" -p "Install? " result
if [ '$result' == 'yes' ]; then
declare -a subs=('one' 'two')
for sub in "${subs[@]}"
do
echo "$sub"
done
fi
Run Code Online (Sandbox Code Playgroud)
jes*_*e_b 14
您需要双引号$result而不是单引号,否则它不会扩展。
[ '$result' == 'yes' ]永远不会评估为 true ,因为它试图将文字$result与文字进行比较yes。
此外,正如 Kusalananda 指出的那样;该==操作员是对于bash测试构建体[[,而=操作者是用于标准(POSIX)测试的构建体[。
因此,您应该将该行更改为: [ "$result" = 'yes' ]
另一个需要了解的好工具是可用于跟踪脚本的set内置函数及其-x开关。如果您添加set -x到原始脚本的顶部并运行它,您将看到以下打印出来:
+ read -e -i no -p 'Install? ' result
Install? yes
+ '[' '$result' == yes ']'
Run Code Online (Sandbox Code Playgroud)
如您所见,它试图将“$result”与“yes”进行比较。正确引用时,您不会看到变量$result,而是像这样扩展:
+ read -e -i no -p 'Install? ' result
Install? yes
+ '[' yes == yes ']'
+ subs=('one' 'two')
+ declare -a subs
+ for sub in '"${subs[@]}"'
+ echo one
one
+ for sub in '"${subs[@]}"'
+ echo two
two
Run Code Online (Sandbox Code Playgroud)
每当您用脚本将头撞在墙上时,您应该打开set -x并跟踪它在做什么以及哪里出错了。