【shell 】syntax error in conditional expression

楊奇龍發表於2011-03-13
編寫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

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22664653/viewspace-689293/,如需轉載,請註明出處,否則將追究法律責任。

相關文章