奇怪的行为

我的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文件操作符| 如果:

  • -a | 文件已存在
  • -d | 文件是一个目录
  • -f | 文件是一个常规文件(即不是一个目录或其他特殊类型的文件)
  • -r | 您已阅读文件许可权
  • -s | 文件存在并且不是空的
  • -w | 您有文件的写入权限
  • -x | 您拥有文件执行权限或目录搜索权限(如果它是目录)
  • -O | 文件您拥有文件
  • -G | 文件您的组ID与文件的ID相同
  • 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

    下一篇: Running JAR file on AIX via KornShell script