1

I have a script with a main menu done using a case statement. The script runs, welcome screen info is echo'ed to the screen, user hits enter to clear welcome screen stuff (empty read statement), user is asked to input a number corresponding to the menu item (read menuNum). It works fine as is, but I want to expand functionality a bit and allow the user to skip the welcome screen and go straight to a menu item by using an argument when running the script.

For example if my menu is: 1) file operations 2) user info 3) processes, then i want the user to type "scriptfile.sh file" into the console to go straight to the file menu. This would somehow assign menuNum=1.

I don't know if this is possible using redirection of input, or really if it's possible at all. Any help or hints would be greatly appreciated. Thank you.

Basically, this is my script:

#!/bin/bash
#DEFINE ARGUMENTS
if [[ "$1" = "man" ]]; then man ./manpage.txt; exit; fi #argument "man" opens man page, closes main script
if [ "$1" = "debug" ]
    then clear
    echo -e "This area provides debug information:\n"
    echo -e "There's a lot here, but it's not related to my question."
    echo -e "\nPress [Enter] to continue."
    read
fi 
if [[ "$1" = "file" ]]; then bash tsharonfp.sh < 1; exit; fi #go straight to file menu
if [[ "$1" = "user" ]]; then bash tsharonfp.sh < 2; exit; fi #go straight to user menu
if [[ "$1" = "info" ]]; then bash tsharonfp.sh < 3; exit; fi #go straight to info menu
if [[ "$1" = "fun" ]]; then bash tsharonfp.sh < 4; exit; fi #go straight to fun menu
if [[ "$1" = "process" ]]; then bash tsharonfp.sh < 5; exit; fi #go straight to proc menu
#/DEFINE ARGUMENTS
clear
echo -e "My script title, contact info, and other stuff gets printed to screen here."
read #hitting enter clears welcome screen stuff    
clear
while : #opens while1 loop
do #while1 loop
echo -e "Main Menu\n"
echo -e "[1] File Menu\n[2] User Menu\n[3] Info Menu\n[4] Fun Menu\n[5] Process Menu\n[88] Exit\n[99] Shutdown"
echo -ne "\nEnter Choice: "
read menuNum

case $menuNum in #open mainmenu case
    1) #File;; #statement isn't commented, of course, but you get the idea
    2) #user;;
    3) #info;;
    4) #fun;;
    5) #proc;;
    88) exit;;
    99) shutdown  -h;;
    *) echo "invalid input";;
esac #closes mainmenu case
done #closes while1 loop
3
  • 3
    Show the script that you have. Commented Oct 18, 2013 at 7:08
  • i don't see the problem what tsharonfp.sh is doing? if tsharonfp.sh has to accept parameters in {1,...,5,88,99} you should pass it this way if [[ "$1" = "file" ]]; then bash tsharonfp.sh 1; exit; fi not with tsharonfp.sh <1; Commented Oct 18, 2013 at 9:14
  • 1. You don't need to quote variables inside [[, only inside [ (even if the values have embedded spaces). You only need to quote literals when they have embedded whitespace. 2. You might consider then exec bash tsharonfp.sh "$1" fi. Using exec means there is no need to exit (unless exec fails). 3. probably best to modularise your script. Commented Oct 18, 2013 at 10:47

1 Answer 1

1

Just a few changes takes care of your problem. Set menuNum based on argument and only ask/read if not set.

#!/bin/bash
#DEFINE ARGUMENTS
if [[ "$1" = "man" ]]; then man ./manpage.txt; exit; fi #argument "man" opens man page, closes main script
if [ "$1" = "debug" ]
    then clear
    echo -e "This area provides debug information:\n"
    echo -e "There's a lot here, but it's not related to my question."
    echo -e "\nPress [Enter] to continue."
    read
fi


if [ "$1" = "file" -o "$1" = "1" ]; then menuNum=1;
elif [ "$1" = "user" -o "$1" = "2" ]; then menuNum=2;
elif [ "$1" = "info" -o "$1" = "3" ]; then menuNum=3;
elif [ "$1" = "fun" -o "$1" = "4" ]; then menuNum=4;
elif [ "$1" = "process" -o "$1" = "5" ]; then menuNum=5;
else menuNum=0; fi

#/DEFINE ARGUMENTS
clear
echo -e "My script title, contact info, and other stuff gets printed to screen here."
[ $menuNum = 0 ] && read #hitting enter clears welcome screen stuff    

clear
while : #opens while1 loop
do #while1 loop
echo -e "Main Menu\n"
echo -e "[1] File Menu\n[2] User Menu\n[3] Info Menu\n[4] Fun Menu\n[5] Process Menu\n[88] Exit\n[99] Shutdown"
echo -ne "\nEnter Choice: "
[ $menuNum = 0 ] && read menuNum

case $menuNum in #open mainmenu case
    1) #File;; #statement isn't commented, of course, but you get the idea
    2) #user;;
    3) #info;;
    4) #fun;;
    5) #proc;;
    88) exit;;
    99) shutdown  -h;;
    *) echo "invalid input";;
esac #closes mainmenu case
menuNum=0
done #closes while1 loop
Sign up to request clarification or add additional context in comments.

1 Comment

Wouldn't elif or case be better instead a bunch of if's? After all, $1 can't be "file" AND "user" AND "info", etc. Using [ with -o can be ambiguous, maybe use [[ with || might be clearer? You use similar later in the script anyway!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.