Bash Case Issues..


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Case Issues..
# 1  
Old 10-12-2012
Bash Case Issues..

Hi,

I'm having some trouble with using "case...esac" in Bash.

I've googled it and am stuggling to understand the syntax and how to do certain things.

Firstly, I want to be able to choose a case based on a variable number

For example, I have in my code a place where a user can enter a number using read.

Code:
read SEL

Then I want to use a case to decide what should be done.

I want it to be able to figure out whether it is a number or letter.

Using "if" you can always use
Code:
if [ $SEL -eq $SEL 2>/dev/null ]
then
    echo "Is Number"
else
    echo "Contains Letters or Symbols"
fi

I also want to have a case based on ranges of numbers too, but the upper range isn't a fixed number

So, in my code I find what the last number will be and define it as LASTNUM

In my case I want it be

Code:
case "$SEL"
    0)
        # Do Something for 0
    ;;
    1-$LASTNUM)
        # Do something for between 1 and LASTNUM
    ;;
    Any variation of Q q or Quit)
       # Do something for Q, q or Quit
    ;;
    *)
       # Do something for everything else
    ;;
esac

I've been really struggling and any help would be much appreciated.

Thanks in advance! Smilie
# 2  
Old 10-12-2012
Code:
case "$SEL" in
    0)
       echo zero
    ;;
    [1-9]*)
       echo number
    ;;
    [qQ]|[qQ]uit)
       echo quit
    ;;
    *)
       echo no choice
    ;;
esac

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 10-16-2012
Thanks that works perfectly!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash case error

Please - this is my FIRST ever attempt on scrip and I just want simple answer if possible. I am modifying existing and working script. I just want to and more to it so I started with simple insertion of working code into menu. I am doing OK, but I cannot figure out why I am executing... (8 Replies)
Discussion started by: annacreek
8 Replies

2. Shell Programming and Scripting

BASH - case statement

Hi Gurus, I have the below BASH code which does not works for upper case alphabets except Z (upper case Z). What may be the reason. Also escape sequences like \n, \t, \b, \033(1m \033(0m (For bold letter) are not working. case $var in ) echo "Lower case alphabet" ;; ... (7 Replies)
Discussion started by: GaneshAnanth
7 Replies

3. Shell Programming and Scripting

Bash/cron issues

Hi all, I am trying to run a cronjob to push my files to my git repo once a week and output a prompt to a logfile, my script works fine if I invoke it manually but my cronjob wont run for some reason, I have sourced the file, and restarted my Mac to no avail, right now I believe I have the cronjob... (8 Replies)
Discussion started by: gmenfan83
8 Replies

4. Shell Programming and Scripting

Problem using bash case statement

I have the following bash script and it is not accepting the lines "--"|"--""-") "--""-"") while do echo "Current Argument is ${1}" case "$1" in "--"|"--""-") echo "Argument is ${1}" shift # Skip ahead one to the next argument. ... (1 Reply)
Discussion started by: kristinu
1 Replies

5. Shell Programming and Scripting

Bash Script issues

So what i am trying to do is write a script that takes in any number of scrambeled words and unscrambles them. I have already addressed the issues of partial matches, exits silently if there are no matches, and finds words regardless of case that they were input. but while trying to get it so... (3 Replies)
Discussion started by: alindner
3 Replies

6. Shell Programming and Scripting

Bash script issues

Hi. The below part of my bash script kicks out the following error message: $ ./extract_eod_report_stats_new.sh 2010-04-23 ./extract_eod_report_stats_new.sh: line 204: syntax error near unexpected token `(' ./extract_eod_report_stats_new.sh: line 204: `TRANSACTIONS_RECEIVED_TOP=`grep... (6 Replies)
Discussion started by: Peter.Lovell
6 Replies

7. Shell Programming and Scripting

bash case statement output help

greetings, I have a script that is taking input like this: a b c d aa bb aaa bbb ccc ddd and formating it to be like this: a b c d aa bb aaa bbb ccc ddd (4 Replies)
Discussion started by: adambot
4 Replies

8. Shell Programming and Scripting

Example of switch case in Bash

can anyone post a sample code for a switch case in shell (1 Reply)
Discussion started by: sumit the cool
1 Replies

9. Shell Programming and Scripting

bash regex =~ case insensetive, possible?

It can get very annoying that bash regex =~ is case-sensetive, is there a way to set it to be case-insensetive? if ]; then echo match else echo no match fi (8 Replies)
Discussion started by: TehOne
8 Replies
Login or Register to Ask a Question