unary operator expected

问题:

1unary operator expected

[root@foundation0 hrr]# ./r3

./r3: line 29: [:/var/log: unary operator expected

Logs cleaned up.

29行如下:

29 if [ `pwd` !="$LOG_DIR" ]

改成

if [ "$PWD" !="$LOG_DIR" ]

就好了

 

[root@foundation0 hrr]# cat r3

#!/bin/bash

LOG_DIR=/var/log

ROOT_UID=0

LINES=10

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" ]   #n是非空的意思

then

       lines=$1

else

       lines=$LINES

fi

#E_WRONGARGS=65

#case "$1" in

#"")lines=50;;

#*[!0-9]*)echo "Usage:`basename $0`file-to-cleanup";

#      exit $E_WRONGARGS;;

#*)lines=$1;;

#esac

 

cd $LOG_DIR

#if [ "`pwd`"!="$LOG_DIR" ]

if [ "$PWD" !="$LOG_DIR" ]

#如果是在[]里,最好用$加大写命令

then

       echo "Can't change to $LOG_DIR."

       exit $E_XCD

fi

#cd /var/log ||{

#      echo "Cannot change to nessary directory.">&2

#      exit $E_XCD;

#}

 

tail -$lines messages > mesg.temp

 

cat /dev/null > messages

cat /dev/null > wtmp

echo "Logs cleaned up."

 

mv mesg.temp messages

exit 0

[root@foundation0 hrr]#



转载:http://blog.csdn.net/goodlixueyong/article/details/6564591

你可能感兴趣的:(shell,rhel7)