我想创建一个脚本,该脚本应该接受一个参数并检查它是否等于给定的单词然后相应地显示一条消息.我使用ubuntu OS的bash shell.我根据教程尝试了一些东西 - http://www.tech-recipes.com/rx/209/bournebash-shell-scripts-string-comparison/但它失败了.
#!/bin/bash
if ["$1"=="password"]
then
echo correct password
else
echo wrong password
fi
Run Code Online (Sandbox Code Playgroud)
bash Script.sh密码.
错误消息是 - [密码=密码]:找不到命令.
怎么解决?
空格:
#!/bin/bash
if [ "$1" == "password" ]
then
echo correct password
else
echo wrong password
fi
Run Code Online (Sandbox Code Playgroud)
if
实际上是命令之后的表达式,命令由空格分隔.所以你的命令是["$1"=="password"]
扩展到[password==password]
,那是不存在的(/usr/bin/[password==password]
任何人?).
在我更正的代码中,命令是[
(是的,有一个/bin/[
),而其余的行是参数.
有关man test
详细信息(test
是一种别名[
).