Linux Bourne shell-目录可写检查始终返回为;“不可写”;

Linux Bourne shell-目录可写检查始终返回为;“不可写”;,linux,shell,Linux,Shell,在测试目录是否不可写时,此函数始终返回True。谁能给我解释一下原因吗?只有“目录是否可写”(到目前为止)失败。但是我想包括整个函数,以便提供更多关于之前发生的事情的细节 此函数接受“before”和“after”字符串,通过用破折号替换所有空格来重命名文件和/或目录。我只是想使用“重命名”功能,但有一个预先构建的框架脚本需要遵循 更新的scipt: my_rename() { echo "Trying: myrename $1 $2" # im

在测试目录是否不可写时,此函数始终返回True。谁能给我解释一下原因吗?只有“目录是否可写”(到目前为止)失败。但是我想包括整个函数,以便提供更多关于之前发生的事情的细节

此函数接受“before”和“after”字符串,通过用破折号替换所有空格来重命名文件和/或目录。我只是想使用“重命名”功能,但有一个预先构建的框架脚本需要遵循

更新的scipt:

    my_rename()
    {
        echo "Trying: myrename $1 $2"

        # implement this function to my_rename $1 to $2
        # The following error checking must happen:

        # 1. check if the directory where $1 resided is writeable, if not then report an error
        # Is it a directory or a file
        if [ -d $1 ]
        then
            dnam=$1
            ftyp=$"dir"
        else
            # It's a file. Let's get the dirname
            dnam=$(dirname $1)\"
            ftyp=$"file"
        fi

        echo "dnam $dnam"

        # Is the directory writable
        errcd=0
        if [ ! -w "$dnam" ]
        then
            # Not writable. Show an error.
            echo "Directory $dnam is not writable by $(whoami)"
            errcd=1
        fi

        # 2. check if "$2" exists -if it does report and error and don't do the mv command
        if [ "$errcd" -eq 0 ] && ([ -f $2 ] || [ -d $2 ])
        then
            if [ $ftyp = "file" ]
            then
                echo "File $2 already exists"
            else
                echo "Directory $2 already exists"
            fi
            errcd=1
        fi

        # 3. check the status of the mv command and report any errors
        if [ "$errcd" -eq 0 -a -e $2 ]
        then
            exec "mv -f $1 $2"
            if [ $? -gt 0 ]
            then
                echo "Command failed: myrename $1 $2"
            fi
        fi
    }


    bryan@bryan-VirtualBox:~/renametest$ ./script3.sh -f ~/renametest
    Trying: myrename "/home/bryan/renametest/1 2" "/home/bryan/renametest/1-2"
    dnam "/home/bryan/renametest"
    Directory "/home/bryan/renametest" is not writable by bryan

    drwxr-xr-x  3 bryan bryan  4096 2013-04-03 17:10 renametest
由于功能请求仍然被拒绝,我在这里复制上述解决方案。


回复:w问题,向上游看。如果第一个参数是目录,则dnam已设置为该参数,但如果该参数不是目录或不存在,则执行以下操作:dnam=$(dirname$1)\“(结尾的转义”表示有问题。)显示的输出示例已被编辑,或作为转义的“建议,您将这些值作为值的一部分用引号传递到函数中-这不是个好主意,我可以向您保证系统上的文件/目录名不包含引号。看起来此问题出现在调用此函数时,而不是在函数中威廉

你到底用的是哪种外壳?也许-w没有做你想让它做的事?嗯,我用的是Ubuntu10.10。因此,bash是默认的(我认为)。但是我为的是使用Sunos5.9和KornShell,这里有很多语法问题。例如,
if$ftyp=“file”
应该是
if[$ftyp=“file”]
if$?>如果[$?-gt 0]
,则0应为
。(并考虑这一点:<代码>如果[ $Errd] - EQ 0 -A(-F $ 2 -O-D 2美元)] <或代码>或者可能只是<代码>如果[ [ $Errd] - EQ 0 -A -E$ 2 ] 你也应该考虑$FTYP中的值,以及为什么它不告诉你任何关于$2的事。@威廉:谢谢你指出这些。我确信这是显而易见的,但是是的,我在这方面还是很新的。我已经做了您提到的3个更改(在这种情况下选择[“$errcd”-eq 0-a-e$2]。但是原始问题仍然存在。w测试仍然返回不可写。而且([-f$2]| |[-d$2])不是吗测试告诉我$2是否存在并且是一个文件或目录?或者我也误解了吗?嗯…我想我知道你用$2可能会得到什么…既然我们已经返回了已知的文件或目录,为什么要测试它是否是一个文件或目录?如果是这样,我如何测试“to”($2)是否已经存在?