Find the answer to your Linux question:
Results 1 to 5 of 5
Hi, i am new to shell script and i am trying to make a menu that has three options: backup,restore and quit. if you were to type a letter or ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2013
    Posts
    5

    Bash loop script problem.


    Hi, i am new to shell script and i am trying to make a menu that has three options: backup,restore and quit. if you were to type a letter or number that was not on the menu it will display an error message telling you the proper format. However when the error message displays it closes my menu and i would like to create a loop so that when the error message appears it will take you back to the menu.

    hope this makes sense, here's my code so far.(menu2.sh is the name of the script file).

    Code:
    *) echo "Please press a letter between A to C" ;;
    if *)
    then
    ./menu2.sh
    esac

  2. #2
    Linux User
    Join Date
    Dec 2011
    Location
    Turtle Island West
    Posts
    376
    Maybe something like this:
    Code:
        count=5 # number of items
        ans=
        while [ -z "$ans" ]; do
            #
            # ...print menu here
            #
            echo -n "Enter a number (1-${count}): "
            read ans
            if [ -z "$ans" ]; then
                break
            fi
            let ans+=0 # converts non-digits to 0 value
            if [ $ans -lt 1 -o $ans -gt $count ]; then
                echo "Invalid number (1-${count})."
                ans=
                continue
            else
                num=$ans
            fi
        done
    Peace and Cheer.

  3. #3
    Linux User
    Join Date
    Dec 2011
    Location
    Turtle Island West
    Posts
    376
    I was trying to edit that post for clarification, but for some reason the SAVE button doesn't seem to be working. Probably my ancient browser.

    Anyway, the lines
    Code:
            if [ -z "$ans" ]; then
                break
            fi
    should read
    Code:
            if [ -z "$ans" ]; then
                break # exit loop if only ENTER is pressed, 
                      # or could set some default value here
                      # like ans=4
            fi

  4. #4
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    224
    hi,

    help yourself use `select` compound command.

    `case` construction is
    Code:
    case $variable in
    pattern1) :action
    ;;
    pattern2) :otheraction
    ;;
    esac

  5. #5
    Just Joined!
    Join Date
    May 2013
    Posts
    5
    Quote Originally Posted by Miven View Post
    I was trying to edit that post for clarification, but for some reason the SAVE button doesn't seem to be working. Probably my ancient browser.

    Anyway, the lines
    Code:
            if [ -z "$ans" ]; then
                break
            fi
    should read
    Code:
            if [ -z "$ans" ]; then
                break # exit loop if only ENTER is pressed, 
                      # or could set some default value here
                      # like ans=4
            fi
    Thanks guys for the code but how would i implement it into my menu so that for example if a user picked A)Backup , B)Restore or entered an invalid character, how would i add the code so that i would be taken back to the menu after it had finished a backup or restore. (sorry im very new to shell script).

    here's my code so far:
    Code:
    #!/bin/bash
    
    clear #clears all other commands
      
       echo "-----------------------------------------"
       echo " **************Main Menu*****************"
       echo "-----------------------------------------"
       echo "Choose an option"
       echo "[A]Backup"
       echo "[B]Restore"
       echo "[C]Quit"
       echo
       echo "-----------------------------------------"
       echo
       read option
       case "$option" in
    
       "A" | "a" )  
       read -p "name your backup : " tarball
       read -p "Directory to back up: " bckdir
       echo
       tar -cf $tarball $backdir
       ;;
    
       "B" | "b" )
       read -p "Name of restore file: " tarball
       tar -xf $tarball
       ;;  
       
        "C" | "c" )
        
        echo "Exiting......"
        exit;;
      
    
    
    *) echo "Please press a letter between A to C" ;;
    
    
       esac

Posting Permissions

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