【shell 】syntax error in conditional expression
![](https://csdnimg.cn/release/blogv2/dist/pc/img/reprint.png)
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCurrentTime2.png)
![](https://csdnimg.cn/release/blogv2/dist/pc/img/articleReadEyes2.png)
![](https://csdnimg.cn/release/blogv2/dist/pc/img/tobarCollect2.png)
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newHeart2023Black.png)
#!/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