- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- branching
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-04-2006 01:18 PM
тАО12-04-2006 01:18 PM
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"
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-04-2006 01:36 PM
тАО12-04-2006 01:36 PM
SolutionDISPLAY ()
{
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-04-2006 03:40 PM
тАО12-04-2006 03:40 PM
Re: branching
Or in Clay's example, you'll need extra checks in main.
- Tags:
- exit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-04-2006 07:43 PM
тАО12-04-2006 07:43 PM
Re: branching
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-04-2006 07:56 PM
тАО12-04-2006 07:56 PM
Re: branching
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-04-2006 08:04 PM
тАО12-04-2006 08:04 PM
Re: branching
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-04-2006 08:07 PM
тАО12-04-2006 08:07 PM
Re: branching
read this line as
... presetting the returnvalue via $FSTAT ...
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО12-05-2006 09:57 AM
тАО12-05-2006 09:57 AM
Re: branching
I wanted the case statement to return not to exit.
Clay,
10 points for you sir!
Thanks to everyone who participated in this thread!!!