我不明白你爲什麼會需要知道你需要刪除舊文件name..unless。我寫得很快,看起來很好。它不需要舊的發佈文件名稱。
!/bin/bash
remove_old()
{
rm -f "$1"
}
make_new()
{
ln -s "$1" "$2"
}
if [ $# -ne 3 ]; then
{
echo "Nope, you need to provide..."
echo "(1) link/file name to be removed."
echo "(2) link/file name to be created"
echo "(3) the actual file location for #2"
}
elif [ ! -f "${3}" ]; then
{
echo "Sorry, your target ${3} doesn’t exist or is not accessible"
}
elif [ -f ${2} ]; then
{
echo "Sorry, the new file already exists...want me to remove it?"
read remove
if [ "$remove" = "Y" ]||[ "$remove" = "y"]; then
{
rm -f "$2"
remove_old $1
make_new $3 $2
}
else
{
exit
}
fi
}
else
{
remove_old $1
make_new $3 $2
}
fi
這裏是調試輸出...
bash -xv autolinker.sh my_link my_new_link test_2/test_file
#!/bin/bash
remove_old()
{
rm -f "$1"
}
make_new()
{
ln -s "$1" "$2"
}
if [ $# -ne 3 ]; then
{
echo "Nope, you need to provide..."
echo "(1) link/file name to be removed."
echo "(2) link/file name to be created"
echo "(3) the actual file location for #2"
}
elif [ ! -f "${3}" ]; then
{
echo "Sorry, your target ${3} doesn’t exist or is not accessible"
}
elif [ -f ${2} ]; then
{
echo "Sorry, the new file already exists...want me to remove it?"
read remove
if [ "$remove" = "Y" ]||[ "$remove" = "y"]; then
{
rm -f "$2"
remove_old $1
make_new $3 $2
}
else
{
exit
}
fi
}
else
{
remove_old $1
make_new $3 $2
}
fi
+ '[' 3 -ne 3 ']'
+ '[' '!' -f test_2/test_file ']'
+ '[' -f my_new_link ']'
+ echo 'Sorry, the new file already exists...want me to remove it?'
Sorry, the new file already exists...want me to remove it?
+ read remove
Y
+ '[' Y = Y ']'
+ rm -f my_new_link
+ remove_old my_link
+ rm -f my_link
+ make_new test_2/test_file my_new_link
+ ln -s test_2/test_file my_new_link
您需要的是['readlink'(http://man.cx/readlink)工具 –
謝謝,我認爲,應該讓我走在最前面。從未聽過這個命令。 – LazyCubicleMonkey