BASH - case statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH - case statement
# 1  
Old 12-03-2013
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.[It works few days before]
Code:
case $var in
[a-z])
          echo "Lower case alphabet"
          ;;
[A-Z]
          echo "upper case alphabet"
          ;;
esac

Thanks,
Ganesh

Last edited by Franklin52; 12-03-2013 at 09:42 AM.. Reason: Please use code tags
# 2  
Old 12-03-2013
The first argument to a bash shell script is usually assigned to $1. Are you assigning that to your $var variable elsewhere in your script? If you change $var to $1 it works fine.
# 3  
Old 12-03-2013
Missing something?
Code:
case $var in
[a-z])
          echo "Lower case alphabet"
          ;;
[A-Z])
          echo "upper case alphabet"
          ;;
esac

# 4  
Old 12-03-2013
Chaged $var as $1 - It's not working

Below is the full code - I cant able to find out what causes this error

Code:
echo "Enter the character"
read var
case $var in
[a-z])
         echo "Entered a lowercase alphabet"
         ;;
[A-Z])
         echo "Entered a uppercase alphabet"
         ;;
[0-9])
         echo "Entered a numeric value"
         ;;
?)
         echo "Entered a special character"
         ;;
*)
         echo "Entered more than one character"
         ;;
esac


Last edited by vbe; 12-03-2013 at 10:36 AM.. Reason: Replace icode tags witgs code tags
# 5  
Old 12-03-2013
Put quotes around the $var variable in the case statement:

Code:
echo "Enter the character: "
read var
case "$var" in
[a-z])
    echo "Entered a lowercase alphabet"
    ;;
[A-Z])
    echo "Entered a uppercase alphabet"
    ;;
[0-9])
    echo "Entered a numeric value"
    ;;
?)
    echo "Entered a special character"
    ;;
*)
    echo "Entered more than one character"
    ;;
esac

# 6  
Old 12-03-2013
except uppercase all four other cases are working.

But uppercase is working only for Z(upper case Z)
Bash throws as "Entered lower case alphabet" for all other letters[A-Y]
# 7  
Old 12-03-2013
Quote:
Originally Posted by in2nix4life
Put quotes around the $var variable in the case statement:
Unless the shell's case-statement implementation is buggy, double-quoting won't make a difference, since it only prevents expansions which won't occur anyway (field splitting and pathname expansion).

Regards,
Alister

---------- Post updated at 10:06 AM ---------- Previous update was at 10:00 AM ----------

Quote:
Originally Posted by GaneshAnanth
except uppercase all four other cases are working.

But uppercase is working only for Z(upper case Z)
Bash throws as "Entered lower case alphabet" for all other letters[A-Y]
That sounds like a locale collation issue to me. Your locale probably interleaves upper and lower case letters. Either switch to the C (aka POSIX) locale or use character classes, [[:upper:]] and [[:lower:]], which are valid in all locales.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash read input in case statement not working as expected

I'm having an issue with bash read input when using a case statement. The script halts and doesn't read the input on the first loop. if I hit enter then the scripts starts to respond as expected. Need some help here. defaultans=8hrs read -e -i $defaultans -p "${bldwht}How long would you like... (5 Replies)
Discussion started by: woodson2
5 Replies

2. Shell Programming and Scripting

Bash case Statement and Using Line Anchors?

Hello All, I am writing a script that is to be placed on multiple servers, and of course I've started running into some compatibility issues for certain shell commands. The code below worked just fine on most of my machines except for a couple. Here I had 4 separate lines in my script that... (3 Replies)
Discussion started by: mrm5102
3 Replies

3. Shell Programming and Scripting

Case Statement

Hey, guys I really need some help with a project. "Write a shell program that examines the command line arguments, counts and collects the number of options. Basically it has to collect and count the arguments that start with a "-" and the one's that don't start with a - I know I have to use... (2 Replies)
Discussion started by: sk192010`
2 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

Case statement

Hello, The standard case statement :- case "$1" in "IE0263") commands;; "IE0264") commands;; esac is it possible to have :- case "$1" in "IE0263" OR "IE0878") commands;; "IE0264") commands;; esac Thanks (4 Replies)
Discussion started by: jmahal
4 Replies

6. Shell Programming and Scripting

Can you use logical operators in a case statement (bash)?

I'm pretty sure I already know the answer to this, but I want to make sure I'm not overlooking anything. I'm working on a log monitoring script and every 10 lines I want to display a summary of events. The thing is, there are a lot of possible events, that likely won't have happened, so I only want... (0 Replies)
Discussion started by: DeCoTwc
0 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

[BASH] recognise new line regex in case statement

Hi, I'm trying to write a routine to parse a file that contains data that will be read into arrays. The file is composed of labels to identify data types and arbitrary lines of data with the usual remarks and empty new lines as is common with config files. The initial pass is built as so:... (3 Replies)
Discussion started by: ASGR
3 Replies

9. Shell Programming and Scripting

case statement

Hi, I am writing case statement to execute some finction, my requirement is once one of the case statement is executed again it has to prompt for the option. for script in `echo "$Script_Selected"` do case $script in 1) getNoOFActUsers ;; 2) moveServerrOORotation ;; ... (2 Replies)
Discussion started by: Satyak
2 Replies

10. Shell Programming and Scripting

case statement

Hi all, is it possible to create a 'dynamic' case statement. ie select option in `ls` do case satement depending on results of the above `ls` done I hope I have explained this ok! Thanks Helen (1 Reply)
Discussion started by: Bab00shka
1 Replies
Login or Register to Ask a Question