首页 新闻 赞助 找找看

shell脚本报错[: !=: unary operator expected

0
[待解决问题]

!/bin/bash

testing string equality

testuser=baduser

if [ $UESR != $testuser ]
then
echo "This is not $testuser"
else
echo "Welcome $testuser"
fi
如上会报错:[: !=: unary operator expected
如下设置会正常:

!/bin/bash

testing string equality

testuser=baduser

if [[ $UESR != $testuser ]]
then
echo "This is not $testuser"
else
echo "Welcome $testuser"
fi
为什么再加一层[]就解决了,原因是什么

liutao-666的主页 liutao-666 | 菜鸟二级 | 园豆:202
提问于:2022-02-18 08:05
< >
分享
所有回答(1)
0

Hi, 这个是对bash语法的问题

[ ... ] 这个相当于shell命令 test
[[ ... ]] 这个才是逻辑判断的语句

用 [ ... ] 的话,可以这样写,不能用在 if 中
[ $USER != $testuser ] && echo "This is not $testuser" || echo "Welcome $testuser"
等价于:test $USER != $testuser && echo "This is not $testuser" || echo "Welcome $testuser"

PS. 你贴出来的代码中 $UESR 拼错了,应该是 $USER。

wang_yb | 园豆:4891 (老鸟四级) | 2022-02-18 15:43
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册