目录是否可写

use*_*539 2 linux testing bash shell writable

任何人都可以告诉我为什么这总是说该目录是不可写的,当它绝对是?

    $dnam="/home/bryan/renametest/C D"

    # Is the directory writable
    err=0
    if [ ! -w $dnam ]
    then
        # Not writable. Pop the error and exit.
        echo "Directory $dnam is not writable"
        err=1
    fi

Gor*_*son 5

你需要双引号$dnam- 没有它们,它被解释为两个单独的shell单词,"/ home/bryan/renametest/C"和"D",这使得测试表达式无效,因此失败.这应该工作:

if [ ! -w "$dnam" ]
Run Code Online (Sandbox Code Playgroud)

@tink的建议[[ ]]是一种更干净的方式来做这样的测试,但只能在bash中使用(以及一些其他具有扩展语法的shell).你得到的事实[[: not found意味着你使用的是一个相当基本的shell,而不是bash.