下面我使用if
語句和case
語句來安排我的參數在下面的順序,以減輕我對rsync做的重複輸入。 case
陳述對於下面的if
塊更明智嗎?如果是這樣,怎麼樣?Bash:if vs. case
#!/bin/bash
rsync="rsync -vrtzhP --delete"
localmus=" /cygdrive/c/Users/user/Music/Zune/"
remotemus=" 10.252.252.254::Zune/"
localcal=" /cygdrive/c/Users/user/calibre/"
remotecal=" 10.252.252.254::calibre/"
dry=" -n"
if [ $1 == "zune" ] && [ $2 == "tohere" ]
then
toex=$rsync$remotemus$localmus
fi
if [ $1 == "zune" ] && [ $2 == "tothere" ]
then
toex=$rsync$localmus$remotemus
fi
if [ $1 == "calibre" ] && [ $2 == "tohere" ]
then
toex=$rsync$remotecal$localcal
fi
if [ $1 == "calibre" ] && [ $2 == "tothere" ]
then
toex=$rsync$localcal$remotecal
fi
if [[ $3 == "dry" ]]
then
toex=$toex$dry
fi
echo
echo $toex
echo
echo "Execute? y/n: "
read answer
case $answer in
y)
eval $toex
;;
n)
echo NO!
;;
esac
Youcould串聯了$ 1,$ 2就可以切換。 – eckes