Issue with while/case statement.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue with while/case statement.
# 1  
Old 10-22-2015
Issue with while/case statement.

Hi,

In my script I have while loop with case statement its working fine, I have added two more arguments as input to script. So i have addedd 2 cases in the existing case statement.

-a and -d are newly added blocks.

Am passing the arguments for the same as below.

test.sh -s NY -t N -f DLY -a 20151022 -d 4

But the same is not getting evaluated. -s, -t and -f are evaluating fine.

Do i need to do something for adding new casess in existing case statement, For any case if it would be issue with spaces how to resolve it. Spent my full day time but no clue.

Appriciate your responses, Thanks in advance. Below is the code snippet.

Code:
while [ $# -gt 0 ]; do
case ${1} in
--Src|-s
	IN_SRC=$(echo {2} | tr [a-z] [A-Z])
	shift;;
--type|-t
	IN_TYPE=$(echo {2} | tr [a-z] [A-Z])
	shift;;
--fre|-f
	IN_FRE=$(echo {2} | tr [a-z] [A-Z])
	shift;;
--asofd|-a
	IN_DATE=$(echo {2} | tr [a-z] [A-Z])
	shift;;
--DayWk|-d
	IN_DAYWK=$(echo {2} | tr [a-z] [A-Z])
	shift;;
-*)
	echo"Error log details"
;;
*) break;;
esac
shift
done


Last edited by Corona688; 10-22-2015 at 02:46 PM..
# 2  
Old 10-22-2015
Please use code tags as required by forum rules!

Are you sure it works/worked before you added the new case patterns?
- patterns need to be terminated with a )
- for {2}, the $ sign is missing

If both errors are corrected, the parameters will be recognized as expected.

---------- Post updated at 14:46 ---------- Previous update was at 14:45 ----------

BTW, you can use shell's parameter expansion to convert lower to upper case...
# 3  
Old 10-22-2015
Yes..this script just type in notepad, missing $ its type error. But in actual script is proper, issue with last two parameters are not recognized.

Code:
while [ $# -gt 0 ]; do
case ${1} in
--Src|-s
	IN_SRC=$(echo {$2} | tr [a-z] [A-Z])
	shift;;
--type|-t
	IN_TYPE=$(echo {$2} | tr [a-z] [A-Z])
	shift;;
--fre|-f
	IN_FRE=$(echo {$2} | tr [a-z] [A-Z])
	shift;;
--asofd|-a
	IN_DATE=$(echo {$2} | tr [a-z] [A-Z])
	shift;;
--DayWk|-d
	IN_DAYWK=$(echo {$2} | tr [a-z] [A-Z])
	shift;;
-*)
	echo"Error log details"
;;
*) break;;
esac
shift
done

---------- Post updated at 08:03 AM ---------- Previous update was at 08:00 AM ----------

earlier one is some syntax erroros, updated script

Hi,

I have while loop with case statement its working fine, I have added two more arguments as input to script. So i have added 2 cases in the existing case statement.

-a and -d are newly added blocks.

Am passing the arguments for the same as below.
Code:
test.sh -s NY -t N -f DLY -a 20151022 -d 4

But the same is not getting evaluated. -s, -t and -f are evaluating fine.

Do I need to do something for adding new cases in existing case statement, For any case if it would be issue with spaces how to resolve it. Spent my full day time but no clue.

Appreciate your responses, Thanks in advance. Below is the code snippet.

Code:
while [ $# -gt 0 ]; do
case ${1} in
--Src|-s)
	IN_SRC=$(echo {$2} | tr [a-z] [A-Z])
	shift;;
--type|-t)
	IN_TYPE=$(echo {$2} | tr [a-z] [A-Z])
	shift;;
--fre|-f)
	IN_FRE=$(echo {$2} | tr [a-z] [A-Z])
	shift;;
--asofd|-a)
	IN_DATE=$(echo {$2} | tr [a-z] [A-Z])
	shift;;
--DayWk|-d)
	IN_DAYWK=$(echo {$2} | tr [a-z] [A-Z])
	shift;;
-*)
	echo"Error log details"
;;
*) break;;
esac
shift
done


Moderator's Comments:
Mod Comment
Please wrap all code, files, input & output/errors in CODE tags as per the forum rules.
It makes it far easier to read and preserves multiple spaces for indenting or fixed width data.

Last edited by rbatte1; 10-22-2015 at 12:37 PM.. Reason: Added CODE tags, corrected spelling and capital letters
# 4  
Old 10-22-2015
What about the code tags?

---------- Post updated at 15:20 ---------- Previous update was at 15:17 ----------

Why aren't you happy with the code? It works perfectly for me...

Please post your system (OS, shell) info.
# 5  
Old 10-22-2015
There is a potential issue with shell substitution.
Put quotes around all command arguments, for example replace
Code:
        IN_SRC=$(echo {$2} | tr [a-z] [A-Z])

by
Code:
        IN_SRC=$(echo "$2" | tr "[a-z]" "[A-Z]")

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh case statement issue

Hi. I wrote the following case statement to replace a series of 'ELIF' statements as it looks better and is easier to maintain. However, for some reason the commands don't fully work in this format. Take option 1. It should call a script that runs in the background but it doesn't work. Can anyone... (3 Replies)
Discussion started by: user052009
3 Replies

2. UNIX for Dummies Questions & Answers

Case statement help

Hi I am new to shell scripting, I wanted to make a shell script that has a case statement asking the user to select their city 1)london 2)tokyo 3) etc., I then want the users input to be stored in a variable and echoed out in another script; so for example if the user selects tokyo, tokyo city code... (2 Replies)
Discussion started by: scriptnewbie
2 Replies

3. Homework & Coursework Questions

Case Statement

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Hey, guys I really need some help with a project. "Write a shell program that examines the command line... (8 Replies)
Discussion started by: sk192010`
8 Replies

4. 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

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

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

7. UNIX for Dummies Questions & Answers

CASE statement

Hi, I am writing a bash shell script. My script has a few user defined parameters. When the script runs the first thing it does is make sure that these parameters are valid. One of the parameters is called YEAR. A valid input for YEAR can be 1997-2000. One way I have come up with to ensure... (3 Replies)
Discussion started by: msb65
3 Replies

8. Shell Programming and Scripting

case statement

Hi all, I think i'm asking a sqtupid question here.. i'm using case sttament, what is the syntax or symbol for "or"? I thought was || here a quick sample of my case statment echo "Would you like to update your detail ?" read response case $response in ... (2 Replies)
Discussion started by: c00kie88
2 Replies

9. UNIX for Dummies Questions & Answers

If or Case Statement

I want to write a program with the following variables: a=7000 b=24000 c=613.8 The user can enter two words: Vivid or Blue for example. The challenge is that the user might not want to write the words the way they appear. The user can write V or v or vivid or Vivid or write Blue or blue, or B,... (1 Reply)
Discussion started by: Ernst
1 Replies

10. Shell Programming and Scripting

Case Statement

Can anyone please tell me why this wont work! Thanks so much! #!/bin/sh for file do case $file in *.*.*) echo Cannot have more than 1 dot exit ;; *'**'*) echo Cannot have more than 1 asterisk exit ;; *'*'*|?.) echo this is a target (19 Replies)
Discussion started by: Zeta_Acosta
19 Replies
Login or Register to Ask a Question