我已经编写了这个 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
Run Code Online (Sandbox Code Playgroud)
但是每次运行它时,无论我输入什么,都会收到与此类似的错误:
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'.
Run Code Online (Sandbox Code Playgroud)
那么我的代码有什么问题?(我对 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
Run Code Online (Sandbox Code Playgroud)
因此,在使用test
( [ ]
) 命令时,您需要在条件前后放置空格。
归档时间: |
|
查看次数: |
2573 次 |
最近记录: |