LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-16-2009, 10:10 PM   #1
wjs1990
Member
 
Registered: Nov 2009
Posts: 30

Rep: Reputation: 15
Switch statement for bash script


Hello, i am currently trying out the switch statement using bash script.

CODE:
showmenu () {
echo "1. Number1"
echo "2. Number2"
echo "3. Number3"
echo "4. All"
echo "5. Quit"
}

while true
do
showmenu
read choice
echo "Enter a choice:"
case "$choice" in
"1")
echo "Number One"
;;
"2")
echo "Number Two"
;;
"3")
echo "Number Three"
;;
"4")
echo "Number One, Two, Three"
;;
"5")
echo "Program Exited"
exit 0
;;
*)
echo "Please enter number ONLY ranging from 1-5!"
;;
esac
done

OUTPUT:
1. Number1
2. Number2
3. Number3
4. All
5. Quit
Enter a choice:

So, when the code is run, a menu with option 1-5 will be shown, then the user will be asked to enter a choice and finally an output is shown. But it is possible if the user want to enter multiple choices. For example, user enter choice "1" and "3", so the output will be "Number One" and "Number Three". Any idea?
 
Old 11-16-2009, 10:23 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
put "|" . eg 1|3) echo "1 or 3" ;;
 
Old 11-16-2009, 11:09 PM   #3
stevenworr
LQ Newbie
 
Registered: Oct 2008
Posts: 13

Rep: Reputation: 1
Just something to get you started.


Code:
#! /bin/bash
showmenu ()
{
    typeset ii
    typeset -i jj=1
    typeset -i kk
    typeset -i valid=0	# valid=1 if input is good

    while (( ! valid ))
    do
	for ii in "${options[@]}"
	do
	    echo "$jj) $ii"
	    let jj++
	done
	read -e -p 'Select a list of actions : ' -a answer
	jj=0
	valid=1
	for kk in "${answer[@]}"
	do
	    if (( kk < 1 || kk > "${#options[@]}" ))
	    then
		echo "Error Item $jj is out of bounds" 1>&2
		valid=0
		break
	    fi
	    let jj++
	done
    done
}

typeset -r c1=Number1
typeset -r c2=Number2
typeset -r c3=Number3
typeset -r c4=All
typeset -r c5=Quit
typeset -ra options=($c1 $c2 $c3 $c4 $c5)
typeset -a answer
typeset -i kk
while true
do
    showmenu
    for kk in "${answer[@]}"
    do
	case $kk in
	1)
	    echo 'Number One'
	    ;;
	2)
	    echo 'Number Two'
	    ;;
	3)
	    echo 'Number Three'
	    ;;
	4)
	    echo 'Number One, Two, Three'
	    ;;
	5)
	    echo 'Program Exit'
	    exit 0
	    ;;
	esac
    done 
done
 
Old 11-16-2009, 11:10 PM   #4
wjs1990
Member
 
Registered: Nov 2009
Posts: 30

Original Poster
Rep: Reputation: 15
Ok will try it out first. Thanks.

Last edited by wjs1990; 11-16-2009 at 11:13 PM.
 
Old 11-16-2009, 11:16 PM   #5
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726

Rep: Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706
This can be done just by wrapping your case block in a for loop and changing one line.


Code:
#!/bin/bash
showmenu () {
    echo "1. Number1"
    echo "2. Number2"
    echo "3. Number3"
    echo "4. All"
    echo "5. Quit"
}

while true ; do
    showmenu
    read choices
    for choice in $choices ; do
	case "$choice" in
	    1)
		echo "Number One" ;;
	    2)
		echo "Number Two" ;;
	    3)
		echo "Number Three" ;;
	    4)
		echo "Numbers One, two, three" ;;
	    5)
		echo "Exit"
		exit 0 ;;
	    *)
		echo "Please enter number ONLY ranging from 1-5!"
		;;
	esac
    done
done
You can now enter any number of numbers seperated by white space.

Cheers,

EVo2.
 
Old 11-17-2009, 12:36 AM   #6
wjs1990
Member
 
Registered: Nov 2009
Posts: 30

Original Poster
Rep: Reputation: 15
Thanks people!
 
Old 11-19-2009, 10:56 PM   #7
wjs1990
Member
 
Registered: Nov 2009
Posts: 30

Original Poster
Rep: Reputation: 15
Hi all, i am currently trying out this code.

CODE:

showmenu () {
echo "1. Number1"
echo "2. Number2"
echo "3. Number3"
echo "4. All"
echo "5. Quit"
}

while true
do
showmenu
echo "Enter a choice (1-5):"
read choices
clear
echo -e '\E[034;1m'"\033[1mSunOS Audit ...\033[0m"
for choice in $choices
do
case "$choice" in
1)
#clear
echo "Number One"
;;
2)
#clear
echo "Number Two"
;;
3)
#clear
echo "Number Three"
;;
4)
#clear
echo "Number One, Two, Three"
;;
5)
#clear
echo -e '\E[031;1m'"\033[1mPROGRAM CLOSED!\033[0m"
exit 0
;;
*)
echo "INVALID INPUT! PLEASE TRY AGAIN!"
;;
esac
done
echo -e '\E[034;1m'"\033[1mSCAN COMPLETED\033[0m"
exit 0
done

RUN:

1. Number1
2. Number2
3. Number3
4. All
5. Quit
Enter a choice (1-5):
1 32 2

When the code is run, the user will be prompt to enter the choices he or she wants. So right now, i enter the choices "1 32 2". But however choice "32" is actually invalid, so the output will be this:

OUTPUT:
SunOS Audit ...
Number One
INVALID INPUT! PLEASE TRY AGAIN!
Number Two
SCAN COMPLETED

Is it possible that if an invalid choice is being catch, the script will immediately prompt the user to re-enter the choice again? Any idea?
 
Old 11-20-2009, 12:37 AM   #8
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726

Rep: Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706Reputation: 1706
Yes, use "break" to jump out of the for loop, which will send control back to the top of the while loop

Code:
*)
echo "INVALID INPUT! PLEASE TRY AGAIN!"
break
;;
Evo2.

Last edited by evo2; 11-20-2009 at 12:39 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using apt-get in if statement in bash script jax8 Programming 4 03-29-2009 11:23 PM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 07:00 AM
problem with if statement in bash script crewblunts Programming 3 03-11-2006 01:38 PM
how to close 'else' statement block in bash script servnov Linux - General 9 11-12-2004 04:53 PM
Need help adding an IF / ELSE statement to my Bash Script Relix Linux - General 1 08-01-2002 03:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration