【shell 】syntax error in conditional expression
weixin_34050427于 2016-04-15 10:04:53 发布 阅读量3.6k 收藏 2 点赞数 1 文章标签: shell 版权 编写shell 脚本时遇见 syntax error in conditional expression 错误,#!/bin/bash
# cleanup /var/log/message
LOG_DIR=/var/log
ROOT_DID=0
LINES=50
E_XCD=66
E_NOTROOT=67
if [[ "$UID" -ne "$ROOT_UID"]]
then
echo "Must be root to run this script."
exit $E_NOTROOT
fi
if [ -n "$1"]
then
lines=$1
else
lines=$LINES
fi
.......
[email protected] # sh test.sh
test.sh: line 12: syntax error in conditional expression
test.sh: line 13: syntax error near `then'
test.sh: line 13: `then'
仔细查看是由于 if 条件中的中括号[ ]与变量之间必须有空格
[email protected] # vi test.sh
#!/bin/bash
# cleanup /var/log/message
LOG_DIR=/var/log
ROOT_DID=0
LINES=50
E_XCD=66
E_NOTROOT=67
if [[ "$UID" -ne "$ROOT_UID" ]]
then
echo "Must be root to run this script."
exit $E_NOTROOT
fi
if [ -n "$1"]
then
lines=$1
else
lines=$LINES
fi
.....
"test.sh" 60L, 793C written
修改以后,再次执行成功
[email protected] # sh test.sh 20
Logs cleaned up 标签:shell,conditional,syntax,sh,error,test,expression From: https://www.cnblogs.com/pengmn/p/18211496