LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-05-2003, 05:43 AM   #1
davee
Member
 
Registered: Oct 2002
Location: Ayrshire, Scotland
Distribution: Suse(home) RHEL (Work)
Posts: 263

Rep: Reputation: 30
bash - comparing a variable to several values


I've written a script which I want to check for valid input:

dom_option="";
while [ "$dom_option" != [s,r,q,c] ]; do
echo "Choose (s)tart, (r)estart, (q)uit server or (c)ancel."
read dom_option
done

What should I put where I've presently got s,r,q,c so it only accepts one of these values before exiting the loop? Do I have to put four 'or' statements in my comparison?

I know I've read the syntax somewhere, but can't find it...

Dave
 
Old 05-05-2003, 06:35 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,451
Blog Entries: 54

Rep: Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893
If you only want to validate char dom_option you could present it as a case statement:
case "${dom_option}" in
s|r|q|c) continue;;
*) echo "Not a valid option"; exit 1;;
esac

...or you could use a "select" statement to only allow one option of s q r or c:
select action in exit s q r c; do
case "${action}" in
exit) echo "Exiting."; exit 0;;
*) echo "You selected \"${action}\""; break;;
esac
done

Other ways would be to use Bash' getopt or expr. Here's expr:
expr index "${dom_action}" "sqrc" >/dev/null
case "$?" in 1) echo "Not a valid option"; exit 1;; esac

Last edited by unSpawn; 05-05-2003 at 06:41 AM.
 
Old 05-05-2003, 06:52 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
One way to do that:
(This also accepts uppercase answers.)
Code:
#!/bin/sh

while test -z "$dom_option" ; do
    echo "Choose (s)tart, (r)estart, (q)uit server or (c)ancel."
    read dom_option
    case "$dom_option" in
	s|S|r|R|q|Q|c|C) ;;
	*) dom_option="" ;;
    esac
done
echo "OK"
[edit]
Oops, was already answered by unSpawn while I was typing (too slow).
[/edit]

Last edited by Hko; 05-05-2003 at 03:54 PM.
 
Old 05-05-2003, 07:26 AM   #4
davee
Member
 
Registered: Oct 2002
Location: Ayrshire, Scotland
Distribution: Suse(home) RHEL (Work)
Posts: 263

Original Poster
Rep: Reputation: 30
Thanks, folks, that's been a great help (as always!). Another script ready for users to break...

Does anyone think it's worth having a scripting forum, distinct from a programming forum?

Dave!

Last edited by davee; 05-05-2003 at 08:51 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
BASH scripting: check for numeric values linuxLuser Programming 11 11-14-2011 10:11 AM
BASH Script: variable values referencing for console arguments sadarax Programming 1 11-14-2005 05:23 PM
null values in bash scripts always true? jdupre Programming 5 09-27-2005 05:22 PM
bash; reading values from a file km4hr Programming 16 07-28-2005 02:07 PM
Comparing file sizes using a bash script. IanChristie Programming 5 12-19-2003 10:14 PM


All times are GMT -5. The time now is 09:12 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration