Skip to main content

How do I prompt for input in a Linux shell script?


I want to pause input in a shell script, and prompt the user for choices. The standard 'Yes, No, or Cancel' type question. How do I accomplish this at a typical bash prompt?



Source: Tips4allCCNA FINAL EXAM

Comments

  1. The simplest and most widely available method to get user input at a shell prompt is the 'read' command. The best way to illustrate its use is a simple demonstration:

    while true; do
    read -p "Do you wish to install this program?" yn
    case $yn in
    [Yy]* ) make install; break;;
    [Nn]* ) exit;;
    * ) echo "Please answer yes or no.";;
    esac
    done


    Another method, pointed out by Steven Huwig, is bash's 'select' command. Here is the same example using select:

    echo "Do you wish to install this program?"
    select yn in "Yes" "No"; do
    case $yn in
    Yes ) make install; break;;
    No ) exit;;
    esac
    done


    With select you don't need to sanitize the input... it prompts you with your choices, and you type a number corresponding to the choice you want. Select also loops automatically... there's no need for a 'while true' loop to retry if they give invalid input.

    ReplyDelete
  2. echo "Please enter some input: "
    read input_variable
    echo "You entered: $input_variable"

    ReplyDelete
  3. Since BASH4, you can now suggest an answer (-i), so the user only have to press return to enter it :

    read -e -p "Enter the path to the file: " -i "/usr/local/etc/" FILEPATH

    (remember to use the readline option (-e) to allow line editing with arrow keys)

    ReplyDelete
  4. Bash has select for this purpose.

    select result in Yes No Cancel
    do
    echo $result
    done

    ReplyDelete
  5. read -p "Are you alright? (y/n) " RESP
    if [ "$RESP" = "y" ]; then
    echo "Glad to hear it"
    else
    echo "You need more bash programming"
    fi

    ReplyDelete
  6. Use the read command
    Just do:


    echo Would you like to install? "(Y or N)"

    read x

    if [ "x" = "y"]


    and then all of the other stuff you need

    ReplyDelete
  7. I suggest you use dialog... it's simple and easy to use, there's also a gnome version called gdialog that takes the exact same parameters, but shows it GUI style on X.

    ReplyDelete
  8. inquire () {
    echo -n "$1 [$2/$3]? "
    read answer
    finish="-1"
    while [ "$finish" = '-1' ]
    do
    finish="1"
    if [ "$answer" = '' ];
    then
    answer=""
    else
    case $answer in
    y | Y | yes | YES ) answer="y";;
    n | N | no | NO ) answer="n";;
    *) finish="-1";
    echo -n 'Invalid response -- please reenter:';
    read answer;;
    esac
    fi
    done
    }

    ... other stuff

    inquire ""Install now? " "y" "n"

    ...


    Where'd my formatting go? I guess that shows what happens if you cut and paste from unix text.

    ReplyDelete
  9. echo "Do you wish to install this program?"
    select yn in "Yes" "No"; do
    case $yn in
    Yes ) make install; break;;
    No ) exit;;
    esac
    done`


    Hi Christopher,
    I used the above code in one of my bash script, even though it says select Yes or No it won't work if I press "Y" or "YES", rather I have to input "1" to say "YES" or 2 to say "NO", it does what it should on option "1" i.e. rm -rf filname (i am deleting a file after its downloaded and extracted) - once it has deleted it, it doesn't returns to the prompt rather it sits on ?#
    Any ideas ? Your suggestions would be helpful
    Thanks again !

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex