I was wondering how can we get a fall trough case structure.
Eg: If we have two cases A and B and a default; if I do not break after A, it should execute all the statements of A as well as B if I break after B else fall through even lower.
I tried this:
Code:
#!/bin/sh
v1=1
case ${v1} in
"1")
echo "1"
"0")
echo "0"
;;
*)
;;
esac
The output I get is :
Code:
fallCase.sh[4]: 0403-057 Syntax error at line 7 : `)' is not expected.
I guess the ")" pointed out by the error is the one I have highlighted above in the code; and it's problematic because of the missing ";;" after first case.
Could you please tell me what I should be doing to correct it!
You answered it yourself - the ;; is missing behind A ie. the check for "1".
For having A true and wanting B executed too, you should just add the execution of B behind A. It is not very well written then but it works. If there are more complex things but "echo 1" or "echo 0", you might want to write these actions as functions above the case construct. So you will only have to call the function names.
Example:
Code:
#!/bin/sh
my_func1()
{
echo 1
# and some more things to do in here maybe
# ...
}
my_func2()
{
echo 0
# and some more things to do in here maybe
# ...
}
v1=1
case ${v1} in
"1")
my_func1
my_func2
;;
"0")
my_func2
;;
*)
;;
esac
exit 0
It's possible to only have one function, but you'd be sending it arguments...and inside that function, you're going to end up re-implementing the exact same thing.
Somewhere you will have to call your 2nd function again. I have no automatical "fall through" as you asked before in mind. At least by just calling it by it's name you have at least less overhead.
I was wondering how can we get a fall trough case structure.
Eg: If we have two cases A and B and a default; if I do not break after A, it should execute all the statements of A as well as B if I break after B else fall through even lower.
[...]
Some shells (ksh93, zsh and bash4) support what you are asking for:
Code:
ksh-M 93t 2008-11-04$ v=a
ksh-M 93t 2008-11-04$ case $v in a) print a;& b) print b;;esac
a
b
Thanks everyone for the replies. I think I should go with what Zaxxon told me earlier and as I had initially done it. I don't require functions because only single line of code, so that can be put there itself.
Regards,
HKansal
Last edited by hkansal; 08-03-2009 at 04:06 PM..
Reason: spelling mistake
Hi folks,
I am new to bash scripting so please excuse my question.
Is there any chance to combine a case structure with the read command?
Like
case (read -p "" variable) in
x)
Thx! (7 Replies)
Hi,
while true
do
printf "$FBOLD\nPlease enter the minutes (0-59): $FREG"
read MIN
case "$MIN" in
||s) break 2;;
*)
echo ""
echo "Invalid minutes, please try again.";;
esac
done
In the above... (4 Replies)
Hello Experts,
I ve been trying to build another shell where I am using the following code.
transact="tv5cpc1"
case "$transact" in
"...cp..")
xActType=""
;;
"...de..")
xActType="sp_dep"
;;
"...ep..")
xActType="sp_epa"
;;
"....v.")
... (4 Replies)
Hello Groups
I am trying to find out ways of comparing a value from a 'c' structure to a value in another 'C' structure. the 'C' structure can be a List or liked list as it contains lot many records.
if we loop it in both the structures it is going to consume time.
I am looking for a simple... (3 Replies)
Hi,
Can anyone help me with the following case structure?
echo "Please enter the date for which you want the Report:\c"
read dat
d1=`echo $dat|cut -c7-8`
m1=`echo $dat|cut -c5-6`
y1=`echo $dat|cut -c1-4`
yr=`expr "$d1" - 1`
case "$yr" in
0)
MONTH=`expr "$m1" - 1`... (4 Replies)
Hi all
Is it possible to copy a structure of a directory only.
e.g.
I have a file with the following entries that is a result of a find :-
/dir1/dir2/file.dbf
/dir1/dir2/dir3/file1.dbf
/dir1/file.dbf
I want to copy these to a directory and keep the structure however starting at a new dir... (8 Replies)
Hello,
I am having a problem setting a range of numbers for the "case" structure.
I can use with no problems, but
when I use it doesn't work???
Does the case struture allow numeric ranges?
eg:
echo -e "enter number between 0 and 60: \c"
read $answer
case $answer in
) echo... (2 Replies)