shell脚本出现错误: [: 13: Illegal num

striveman 2011-06-15 05:11:55
#! /bin/sh

LOG_DIR=/var/log
ROOT_UID=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

cd $LOG_DIR
if [ `pwd` != "$LOG_DIR" ]
then
echo "can't change to $LOG_DIR."
exit $E_XCD
fi

tail -$lines messages > mesg.temp
mv mesg.temp messages;

cat /dev/null > wtmp
echo "logs cleaned up."
exit 0
...全文
956 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
steptodream 2011-06-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 justkk 的回复:]

这样呢?
if [ $UID -ne $ROOT_UID ]
[/Quote]
这样的代码 一旦有一个变量可能为空 就肯定报错
bash的陷阱里讲 尽量不要这样用
justkk 2011-06-15
  • 打赏
  • 举报
回复
首先执行export UID=0
再执行./dellog.sh
striveman 2011-06-15
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 justkk 的回复:]

这样呢?
if [ $UID -ne $ROOT_UID ]
[/Quote]

还是不行呀报

root@ubuntu:/home/peng/src/test/sh# ./dellog.sh
[: 13: -ne: unexpected operator
logs cleaned up.
justkk 2011-06-15
  • 打赏
  • 举报
回复
这样呢?
if [ $UID -ne $ROOT_UID ]
striveman 2011-06-15
  • 打赏
  • 举报
回复
现在使用 !=
执行时,不报错了。
可一直提示must be root to run this script.


root@ubuntu:/home/peng/src/test/sh# echo $UID
0
root@ubuntu:/home/peng/src/test/sh# ./dellog.sh
must be root to run this script.
freetstar 2011-06-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lgxwqq111 的回复:]

if [ "$UID" -ne "$ROOT_UID" ]

应该是 ==
[/Quote]
应该是!=,字符串比较用!= ,数字用-ne
freetstar 2011-06-15
  • 打赏
  • 举报
回复
if [ "$UID" -ne "$ROOT_UID" ]

应该是 ==

23,217

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧