奇怪的行为
  我的KornShell(ksh)手册说,如果文件存在并且它是一个目录, -d表达式返回true。  因此if [[ -d file ]]如果file是一个目录, if [[ -d file ]]应该返回TRUE。  但在我的情况下,这不是它的工作原理。  如果文件存在并且不是目录,它会返回TRUE,但是shell的手册说“它是一个目录”。  那么,有没有人知道它为什么与它应该是相反的? 
它工作正常; 这是你的期望是错误的。 在shell中, 0返回值为true ,并且非零返回值为false 。
$ true ; echo $?
0
$ false ; echo $?
1
ksh文件操作符| 如果:
kshFileOperatorsFunction.ksh
#***Function to demo ksh file Operators.***#
fileOperators(){
    echo "Entering fileOperators function."
    if [[ ! -a $1 ]]; then
        print "file $1 does not exist."
        return 1
    fi
    if [[ -d $1 ]]; then
        print -n "$1 is a directory that you may "
        if [[ ! -x $1 ]]; then
            print -n "not "
        fi
        print "search."
    elif [[ -f $1 ]]; then
         print "$1 is a regular file."
    else
         print "$1 is a special type of file."
    fi
    if [[ -O $1 ]]; then
        print 'you own the file.'
    else
        print 'you do not own the file.'
    fi
    if [[ -r $1 ]]; then
        print 'you have read permission on the file.'
    fi
    if [[ -w $1 ]]; then
        print 'you have write permission on the file.'
    fi
    if [[ -x $1 && ! -d $1 ]]; then
        print 'you have execute permission on the file.'
    fi
    echo "Exiting fileOperators function."
}
参考资料:O'Reilly,学习KornShell卷1
链接地址: http://www.djcxy.com/p/17521.html上一篇: Strange behaviour of
