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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
11-16-2009, 10:10 PM
|
#1
|
Member
Registered: Nov 2009
Posts: 30
Rep:
|
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?
|
|
|
11-16-2009, 10:23 PM
|
#2
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
put "|" . eg 1|3) echo "1 or 3" ;;
|
|
|
11-16-2009, 11:09 PM
|
#3
|
LQ Newbie
Registered: Oct 2008
Posts: 13
Rep:
|
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
|
|
|
11-16-2009, 11:10 PM
|
#4
|
Member
Registered: Nov 2009
Posts: 30
Original Poster
Rep:
|
Ok will try it out first. Thanks.
Last edited by wjs1990; 11-16-2009 at 11:13 PM.
|
|
|
11-16-2009, 11:16 PM
|
#5
|
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726
|
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.
|
|
|
11-17-2009, 12:36 AM
|
#6
|
Member
Registered: Nov 2009
Posts: 30
Original Poster
Rep:
|
Thanks people!
|
|
|
11-19-2009, 10:56 PM
|
#7
|
Member
Registered: Nov 2009
Posts: 30
Original Poster
Rep:
|
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?
|
|
|
11-20-2009, 12:37 AM
|
#8
|
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726
|
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.
|
|
|
All times are GMT -5. The time now is 10:55 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|