现在我遇到的问题只是“是”或“否”语句。它们不允许输入,但看起来和其他语句一样设置好了……
mc_addplugin() {
if pgrep -u $USERNAME -f $SERVICE > /dev/null
then
echo "$SERVICE is running!"
echo "Please stop the service before adding a plugin."
else
echo "Paste the URL to the .JAR Plugin..."
read JARURL
JARNAME=$(basename "$JARURL")
echo "$JARNAME"
if [ -d "$TEMPPLUGINS" ]
then
as_user "cd $TEMPLUGINS && wget -A.jar -O ./$JARNAME $JARURL"
else
as_user "cd $PLUGINSPATH && mkdir $TEMPPLUGINS && cd $TEMPPLUGINS && wget -A.jar -O ./$JARNAME $JARURL"
fi
if [ -f "./$JARNAME" ]
then
as_user "cd $PLUGINSPATH"
if [ -f "./$JARNAME" ]
then
if `diff $PLUGINSPATH/$JARNAME $TEMPPLUGINS/$JARNAME >/dev/null`
then
echo "You are already running the latest version of $JARNAME."
NOW=`date "+%Y-%m-%d_%Hh%M"`
echo "Are you sure you want to overwrite this plugin? [Y/n]"
echo "Note: Your old plugin will be moved to the "$TEMPPLUGINS" folder with todays date."
select yn in "Yes" "No"; do
case $yn in
Yes ) as_user "mv $PLUGINSPATH/$JARNAME $TEMPPLUGINS/${JARNAME}_${NOW} && mv $TEMPPLUGINS/$JARNAME $PLUGINSPATH/$JARNAME && rm -rf $TEMPPLUGINS";
echo "Plugin has been install successfully!"; break;;
No ) echo "The plugin has not been installed! Removing temporary plugin and exiting..."
as_user "rm $TEMPPLUGINS/$JARNAME"; exit;;
esac
done
echo "Would you like to start the $SERVICE now? [Y/n]"
select yn in "Yes" "No"; do
case $yn in
Yes ) mc_start; break;;
No ) "$SERVICE not running! To start the service run: /etc/init.d/craftbukkit start"; exit;;
esac
done
else
NOW=`date "+%Y-%m-%d_%Hh%M"`
echo "Are you sure you want to overwrite this plugin? [Y/n]"
echo "Note: Your old plugin will be moved to the "$TEMPPLUGINS" folder with todays date."
select yn in "Yes" "No"; do
case $yn in
Yes ) as_user "mv $PLUGINSPATH/$JARNAME $TEMPPLUGINS/${JARNAME}_${NOW} && mv $TEMPPLUGINS/$JARNAME $PLUGINSPATH/$JARNAME && rm -rf $TEMPPLUGINS";
echo "Plugin has been install successfully!"; break;;
No ) echo "The plugin has not been installed! Removing temporary plugin and exiting..."
as_user "rm $TEMPPLUGINS/$JARNAME"; exit;;
esac
done
echo "Would you like to start the $SERVICE now? [Y/n]"
select yn in "Yes" "No"; do
case $yn in
Yes ) mc_start; break;;
No ) "$SERVICE not running! To start the service run: /etc/init.d/craftbukkit start"; exit;;
esac
done
fi
else
echo "Are you sure you want to add this new plugin? [Y/n]"
select yn in "Yes" "No"; do
case $yn in
Yes ) as_user "mv $PLUGINSPATH/$JARNAME $TEMPPLUGINS/${JARNAME}_${NOW} && mv $TEMPPLUGINS/$JARNAME $PLUGINSPATH/$JARNAME"; break;;
No ) echo "The plugin has not been installed! Removing temporary plugin and exiting..."
as_user "rm $TEMPPLUGINS/$JARNAME"; exit;;
esac
done
echo "Would you like to start the $SERVICE now? [Y/n]?"
select yn in "Yes" "No"; do
case $yn in
Yes ) mc_start; break;;
No ) "$SERVICE not running! To start the service run: /etc/init.d/craftbukkit start"; exit;;
esac
done
fi
else
echo "Failed to download the plugin from the URL you specified!"
exit;
fi
fi
}
运行时你会得到类似这样的结果:
You are already running the latest version of Vault.jar.
Are you sure you want to overwrite this plugin? [Y/n]
Note: Your old plugin will be moved to the /home/pyrexiacraft/server/plugins/temp_plugins folder with todays date.
1) Yes
2) No
#? y
答案1
select
希望您输入相应的数字;在这种情况下,1 表示是,2 表示否。
不过,用户输入的实际字符串会存储在名为的变量中REPLY
,因此除了数字之外,您还可以解析它。
不过,我只想写一个函数来处理是/否问题;是/否查询并不是真正select
设计的目的。比如
confirm() {
local answer
read -p "$1" -n1 answer
while [[ $answer && $answer != [YyNn] ]]; do
printf '\nPlease answer y or n\n' >&2
read -p "$1" -n1 answer
done
printf '\n'
[[ -z $answer || $answer = [Yy] ]]
}
#...
if confirm "Are you sure? [Y/n] "; then
printf 'Ok\n'
else
printf 'Fine, I will find something else to do\n'
fi
Y、y 或 ENTER 将使函数返回 true,N 或 n 将使函数返回 false。您可以扩展它以将默认值作为第二个参数。