【shell 】syntax error in conditional expression
编写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
.......
root@client.example.com # 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 条件中的中括号[ ]与变量之间必须有空格
root@client.example.com # 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
修改以后,再次执行成功
root@client.example.com # sh test.sh 20
Logs cleaned up