在测试目录是否不可写时,此函数总是返回 True。谁能向我解释为什么?只有“目录可写”(到目前为止)失败了。但我想包含整个函数,以提供更多关于事先发生的事情的细节。
此函数接受“之前”和“之后”字符串,通过用破折号替换所有空格来重命名文件和/或目录。我只是打算使用“重命名”功能,但是我需要遵循一个预先构建的骨架脚本。
更新的脚本:
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