我写这是为了检查你的输入,看它是否是这个bash脚本y
,n
或别的东西(这将是一个更大的项目的一部分):
#!/bin/bash
echo "Have you updated the PATH variable in your .bashrc file yet? [y/n]"
read response
if [$response = "y"]; then
echo "Checkpoint passed"
set $checkpoint = "t"
elif [$response = "n"]; then
echo "Please set the PATH variable in your .bashrc file before running this script."
set $checkpoint = "f"
else
echo "Please only use 'y' and 'n'."
set $checkpoint = "f"
fi
但是每次运行它时,无论我输入什么内容,都会得到与该错误类似的错误:
Have you updated the PATH variable in your .bashrc file yet? [y/n]
y
./snbjdkhome: line 6: [y: command not found
./snbjdkhome: line 9: [y: command not found
Please only use 'y' and 'n'.
那么我的代码有什么问题呢?(我对shell脚本非常陌生。)
这将使您清楚(检查空白):
$ var=c
$ ["$var" == "c"] && echo "OK"
[c: command not found
$ ["$var" == "c" ] && echo "OK"
[c: command not found
$ [ "$var" == "c"] && echo "OK"
bash: [: missing `]'
$ [ "$var" == "c" ] && echo "OK"
OK
因此,在使用test
([ ]
)命令时,需要在条件之前和之后放置空格。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句