当测试目录是否不可写时,此函数总是返回True。有人能给我解释一下原因吗?到目前为止,只有“这个目录是可写的”才会失败。但我想包含整个函数,以提供更多关于之前发生的事情的细节。
此函数接受“之前”和“之后”字符串,通过将所有空格替换为短划线来重命名文件和/或目录。我只是打算使用"rename“函数,但是我需要遵循一个预先构建好的框架脚本。
更新的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发布于 2014-06-18 14:24:35
由于对的特性请求仍然被拒绝,因此我将上面的解决方案复制到这里。
Re:-w问题,往上游看。如果第一个参数是一个目录dnam被设置为它,但如果它不是一个目录或不存在,则执行以下操作: dnam=$(dirname $1)\“(在末尾进行转义表示出了什么问题)。您显示的输出示例要么已被编辑,要么如转义“所示,您将这些值作为值的一部分用引号传递到函数中-这不是一个好主意,我可以向您保证系统上的文件/目录名称不包含引号。看起来这个问题出现在您调用此函数时,而不是在函数中。- William
https://stackoverflow.com/questions/15792324
复制相似问题