Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Bash Script: "Yes/No" User Input.

  1. #1
    Join Date
    May 2007
    Beans
    314
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Bash Script: "Yes/No" User Input.

    I have begun playing with bash scripting and i want to make a very simple script for when i do a fresh reinstall.

    Simply it is a list of:

    "Sudo apt-get install XXX
    Sudo apt-get install YYY...etc"

    What i want to do is put in a question before each install so its something like:

    "do you want to install XXX?
    If yes sudo apt-get install XXX
    if no continue onto the next line
    do you want to install YYY?
    If yes sudo apt-get install YYY
    if no continue onto the next line...etc"
    Ubuntu Guide: GutsySound GuideTake CommandFree Ubuntu StickersLearn to ProgramUndelete ext3
    Ubuntu 7.10 Gutsy 64AMD - Ubuntu User Id: 15132 ■ Last FM

  2. #2
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,715

    Re: Bash Script: "Yes/No" User Input.

    Look for info on the "read" command.

  3. #3
    Join Date
    Apr 2007
    Beans
    2,662
    Distro
    Ubuntu

    Re: Bash Script: "Yes/No" User Input.

    i.e. type "info read" at the prompt. Curiously, there is no man page ("man read") available.

  4. #4
    Join Date
    Feb 2007
    Beans
    2,729

    Re: Bash Script: "Yes/No" User Input.

    This post has some examples of how to use the read command:

    http://ubuntuforums.org/showthread.php?t=530833

    MrC

  5. #5
    Join Date
    Jun 2007
    Location
    Indianapolis, IN
    Beans
    37
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: Bash Script: "Yes/No" User Input.

    How about something like this:

    Code:
    read -p "Install XXX (y/n)?"
    [ "$REPLY" == "y" ] || sudo apt-get install XXX
    read -p "Install YYY (y/n)?"
    [ "$REPLY" == "y" ] || sudo apt-get install YYY
    If the user types "y", it runs the or'ed (||) code. Otherwise it skips it.

    Edit: Might want to force running as root when you start, instead of sudo'ing each statement, like my script that MrC linked to right above.
    Last edited by sacherjj; August 21st, 2007 at 07:46 PM. Reason: changed != "y" to == "y".

  6. #6
    Join Date
    Oct 2007
    Beans
    33

    Re: Bash Script: "Yes/No" User Input.

    i just tried that out with my adapted script to test that out.

    Code:
    #!/bin/bash
    echo hey!
    read -p "write 'gooba gooba gooba' (y/n)?"
    [ "$REPLY" == "y" ] || echo "gooba gooba gooba"
    read -p "Install YYY (y/n)?"
    [ "$REPLY" == "y" ] || sudo apt-get install YYY
    and as this screenshot shows, i said yes to gooba and no to install, and it did it the wrong way around!
    how'd that come about!?

    edit:
    ah!
    Last edited by sacherjj; August 21st, 2007 at 07:46 PM.. Reason: changed != "y" to == "y".
    you were right the first time.
    glad u did that though, now i know how to make it a "press this" or a "press anything but this"
    Last edited by Siljrath; August 18th, 2009 at 10:42 PM.
    today, i am a #!er. like ubuntu, all the bang, but with crunched bloat.

  7. #7
    Join Date
    Feb 2007
    Beans
    2,729

    Re: Bash Script: "Yes/No" User Input.

    Use && when you want the second part to run (be evaluated) when the first part is true.

    Use || when you want the second part to run (be evaluated) when the first part is false.
    MrC

  8. #8
    Join Date
    Oct 2007
    Beans
    33

    Re: Bash Script: "Yes/No" User Input.

    Quote Originally Posted by Mr. C. View Post
    Use && when you want the second part to run (be evaluated) when the first part is true.

    Use || when you want the second part to run (be evaluated) when the first part is false.
    ah, cool. so you can invert the true/false with both &&/|| and !=/==.


    edit:
    so for example,
    Code:
    #!/bin/bash
    echo hey!
    read -p "write 'gooba gooba gooba' (y/n)?"
    [ "$REPLY" != "y" ] || echo "gooba gooba gooba"
    read -p "Install YYY (y/n)?"
    [ "$REPLY" == "y" ] && sudo apt-get install YYY
    both work. ... except of course YYY is just a placeholder, not a package.
    Last edited by Siljrath; August 18th, 2009 at 10:52 PM.
    today, i am a #!er. like ubuntu, all the bang, but with crunched bloat.

  9. #9
    Join Date
    Feb 2007
    Beans
    4,045
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: Bash Script: "Yes/No" User Input.

    You can also combine them to get something similar to an if/else, though if either [[ or aptitude fails, it will jump to the part after ||

    Code:
    read -n1 -p "Install foo? (y/n) "
    echo
    [[ $REPLY = [yY] ]] && echo sudo aptitude install foo || { echo "You didn't answer yes, or installation failed."; exit 1; }

  10. #10
    Join Date
    Dec 2006
    Beans
    1,133
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Bash Script: "Yes/No" User Input.

    Quote Originally Posted by vanadium View Post
    i.e. type "info read" at the prompt. Curiously, there is no man page ("man read") available.
    "read" is part of bash, it is covered in "man bash"
    There are no dumb questions, just dumb answers.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •