1752808 Members
5870 Online
108789 Solutions
New Discussion юеВ

branching

 
SOLVED
Go to solution
lnl
Occasional Advisor

branching

Hello,

I have a case statement in a function. I invoke it from the Main program and it works great except when I select option "C" to cancel or when I have a typ'O, it executes the next line "echo "Listing file"??? I don't want it to get to that line?

DISPLAY ()
{
echo "1. list file1"
echo "2. list file2"
echo "enter option? \c"
read input
case $input in
1) input_cmd="ls f1" ;;
2) input_cmd="ls f2" ;;
C) echo "cancelled"
return 0
;;
*) echo "incorrect option"
return 1
;;
esac
}

main script
DISPLAY
echo "Listing file"
7 REPLIES 7
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: branching

First, you should change your logic just a bit so that a return of 0 indicates success; non-zero is a failure. This is in keeping with standard UNIX conventions.


DISPLAY ()
{
typeset -i FSTAT=0
echo "1. list file1"
echo "2. list file2"
echo "enter option? \c"
read input
case ${input} in
1) input_cmd="ls f1"
;;
2) input_cmd="ls f2"
;;
C) echo "cancelled"
FSTAT=2
;;
*) echo "incorrect option"
FSTAT=2
;;
esac
return ${FSTAT}
} # DISPLAY

main script
XX=$(DISPLAY)
STAT=${?}
# ${XX} contains the stdout of DISPLAY (if any) and ${STAT} contains the return code.
# only do something if ${STAT} equals 0
if [[ ${STAT} -eq 0 ]]
then
echo "Listing file"
fi

If it ain't broke, I can fix that.
Dennis Handly
Acclaimed Contributor

Re: branching

If you don't want to return from DISPLAY, you need to use exit rather than return.

Or in Clay's example, you'll need extra checks in main.
jnovak
New Member

Re: branching

One possible way is to move the command
echo "listing file"
inside the case statement. For example:

DISPLAY ()
{
echo "1. list file1"
echo "2. list file2"
echo "enter option? \c"
read input
case $input in
1) input_cmd="ls f1"
echo "Listing file" ;;
2) input_cmd="ls f2"
echo "Listing file" ;;
C) echo "cancelled"
return 0
;;
*) echo "incorrect option"
return 1
;;
esac
}

main script
DISPLAY
Peter Nikitka
Honored Contributor

Re: branching

Hi,

different from switch/case statements in C(++) or (t)csh, a 'case' in (k)sh does NOT move to the next 'case'-label but behaves like a

case 1:
...
breaksw
case 2:

to speak in csh language.
So presetting $RETURN like in Clay's response is the recommended way.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Dennis Handly
Acclaimed Contributor

Re: branching

>Peter: So presetting $RETURN

If you look very closely at the original example, the returns are meant to be exit. And the other case labels are just meant to fall out of DISPLAY with an unchecked exit status and the real "return" value is in input_cmd.
Peter Nikitka
Honored Contributor

Re: branching

Hi,

read this line as
... presetting the returnvalue via $FSTAT ...

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
lnl
Occasional Advisor

Re: branching

Clay's example meets my goals.
I wanted the case statement to return not to exit.

Clay,
10 points for you sir!

Thanks to everyone who participated in this thread!!!