削除する必要がない限り、古いファイル名を知る必要がある理由がわかりません。私はこれを本当に素早く書きましたが、うまくいくようです。古いリリースのファイル名は必要ありません。
!/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