从bash脚本回答[Y / n]提示

时间:2014-02-19 21:16:11

标签: bash ubuntu prompt

我见过其他人写过这个,但没有任何东西对我有用。当我去删除程序时,我想对脚本中的提示回答“是”。到目前为止,这是我的脚本:

#!/bin/bash

a="This is where all the packages will be listed to install"

echo "What do you want to do?"
while true; do
    read -p "Enter: install, remove, or cancel: " yn
    case $yn in
        [install]* ) $a echo "ROS successfully uninstalled"; break;;
        [remove]* ) sudo apt-get remove ros-*; break;;
        [cancel]* ) exit;;
        * ) echo "INVALID ANSWER!...";;
    esac

done

我读到“期望”应该有效,但它没有,除非我做错了。任何建议表示赞赏!

乔治

1 个答案:

答案 0 :(得分:1)

删除案例条件周围的[],您可能也不想要*

#!/bin/bash

a="This is where all the packages will be listed to install"

echo "What do you want to do?"
while true; do
    read -p "Enter: install, remove, or cancel: " yn
    case $yn in
        install ) $a echo "ROS successfully uninstalled"; break;;
        remove ) sudo apt-get remove ros-*; break;;
        cancel ) exit;;
        * ) echo "INVALID ANSWER!...";;
    esac

done

他们在其他脚本中使用[yY]*的原因是因为他们希望将输入与小写“y”或大写“y”匹配,后跟任何字符,如“Y”,“yes”,“YES”等等。