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.

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

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

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.

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.

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.

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

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.

There is a potential issue with shell substitution.
Put quotes around all command arguments, for example replace

        IN_SRC=$(echo {$2} | tr [a-z] [A-Z])

by

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