سؤال

I've seen other people write about this, but nothing has been working for me. I want to answer "yes" to a prompt from within a script when I go to remove a program. Here is my script so far:

#!/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

I read that the "expect" should work but it hasn't, unless I did something wrong. Any advice is appreciated!

George

هل كانت مفيدة؟

المحلول

Remove the [] around your case conditions, and you probably didn't want the * either:

#!/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

The reason they use [yY]* in other scripts is because they want to match input with lowercase "y" or uppercase "y", followed by any characters, like "Y", "yes", "YES", etc.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top