Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

case script cannot exit

Status
Not open for further replies.

westwood01

Technical User
Dec 28, 2003
41
US
Below is an example of a case script I'm using. The script works as expected, except for the q (quit/exit) option; it won't exit. Actually the script does exit properly if I choose q before choosing any other option. Unlike if I choose 2, then try and q, it wont work. I believe it has something to do with the select statement I'm using, but I'm not entirely sure. Any ideas? Thanks.

#!/bin/ksh

TODAY=`date +%m/%d/%y`
TIME=`date +%H:%M:%S`

while true
do

echo "
1) View ALL Failed Save Sets
2) Create Tickets for SERVER1 Failed Save Sets
q) Quit"
echo ""

read input
case $input in
1) clear
echo ""
echo "Listing of All Failed Save Sets:"
echo ""
awk '{print $0}' data.dat;./script.ksh;;
2) clear
grep server1 data.dat > tmp.server1
echo "Select Failed Save Set to Create Remedy Ticket"
echo "Listing of Failed Save Sets:"
echo ""
IFS="
"
select line in $(<tmp2.server1);do
echo ""
echo "You Selected $REPLY for:
$line"
echo "Continue y/n?"
read reply1
if [ "$reply1" = "y" ]
then
echo "Working . . ."
echo "$TODAY $TIME : $line" >> submitted.log
./maketicket;./script.ksh
else
echo "Request Cancelled";sleep 1
./script.ksh
fi
done;;
q) echo "Exiting . . . ";sleep 1;exit;;
*) echo "Please Make Another Selection";sleep 1;clear;./script.ksh;;
esac
done
 
I'm presuming this is called script.ksh? In which case the problem is that the script is calling itself recursively... so when you quit from one, it just returns to the previous one. Presumably if you hit 'q' a few times in succession it eventually quits.

Why do you make the script call itself? You already have a while true loop, so it should not be necessary?

Annihilannic.
 
Replace this:
./script.ksh
with this:
exec ./script.ksh

Another way is to pay attention to break in the ksh man page.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I removed the lines of the script calling itself and it works just the same - you were right they were unnecessary entries. However, I still cannot quit from the menu. I will look into the break command unless you guys have another suggestion.
 
I was playing around with adding the break. Do I need one break per while true or one per select?
 
Sorry to butt in, as you've already received good advice on this issue. I was bored and had time to play.

You said that you've removed the "./script.ksh" commands, so this may not reflect your current script exactly, but I think you can resolve your problem with this simple change:

Before:

else
echo "Request Cancelled";sleep 1
./script.ksh
fi
done;;


After:
else
echo "Request Cancelled";sleep 1
break
fi
done;;



"Proof that there is intelligent life in Oregon. Well, Life anyway.
 
man ksh said:
break [level]
break exits the levelth inner most for, select,
until, or while loop. level defaults to 1.

You only need one break since you only have one while loop.


Annihilannic.
 
But he has a select loop also (in case 2), so I guess there needs to be a break there as well to break out of the select loop?



HTH,

p5wizard
 
How else would you break out of the "select in do done" loop?

I'd suggest this change:

...
echo "Continue y/n?"
read reply1
if [ "$reply1" = "y" ]
then
echo "Working . . ."
echo "$TODAY $TIME : $line" >> submitted.log
./maketicket
else
echo "Request Cancelled";sleep 1
break
fi
...


HTH,

p5wizard
 
What about using continue

Mike

Unix *is* user friendly. It's just selective about who its friends are.
 
Well, can't say that "select in do done" is one of my favorites either. I hardly ever use it.


HTH,

p5wizard
 
Well...I tried the suggestions you all noted above, but I am still unable to exit from the script back to a prompt. I also had a coworker look at it and he too was not able to get it to exit.

I will post my script again on the hopes of generating some new ideas. Thanks.

#!/bin/ksh

while true
do

echo "
1) View ALL Failed Save Sets
2) Create Tickets for SERVER1 Failed Save Sets
q) Quit"
echo ""

read input
case $input in
1) clear
echo ""
echo "Listing of All Failed Save Sets:"
echo ""
awk '{print $0}' data.dat;./script.ksh;;
2) clear
grep server1 data.dat > tmp.server1
echo "Select Failed Save Set to Create Ticket"
echo "Listing of Failed Save Sets:"
echo ""
IFS="
"
select line in $(<tmp2.server1);do
echo ""
echo "You Selected $REPLY for:
$line"
echo "Continue y/n?"
read reply1
if [ "$reply1" = "y" ]
then
echo "Working . . ."
./maketicket
else
echo "Request Cancelled";sleep 1
fi
done;;
q) echo "Exiting . . . ";sleep 1;exit;;
*) echo "Please Make Another Selection";sleep 1;;
esac
done
 
You're still recursively calling the same script...

#!/bin/ksh

while true
do

echo "
1) View ALL Failed Save Sets
2) Create Tickets for SERVER1 Failed Save Sets
q) Quit"
echo ""

read input
case $input in
1) clear
echo ""
echo "Listing of All Failed Save Sets:"
echo ""
awk '{print $0}' data.dat;./script.ksh;;
2) clear
grep server1 data.dat > tmp.server1
echo "Select Failed Save Set to Create Ticket"
echo "Listing of Failed Save Sets:"
echo ""
IFS="
"
select line in $(<tmp2.server1);do
echo ""
echo "You Selected $REPLY for:
$line"
echo "Continue y/n?"
read reply1
if [ "$reply1" = "y" ]
then
echo "Working . . ."
./maketicket
else
echo "Request Cancelled";sleep 1
fi
done;;
q) echo "Exiting . . . ";sleep 1;exit;;
*) echo "Please Make Another Selection";sleep 1;;
esac
done

Also, once you are in the select in do done loop, you don't seem to have coded a way out of that inner loop...


HTH,

p5wizard
 
And where did you put the "break"?

Code:
2) clear
    grep server1 data.dat > tmp.server1
    echo "Select Failed Save Set to Create Ticket"
    echo "Listing of  Failed Save Sets:"
    echo ""
    IFS="
"
    select line in $(<tmp2.server1);do
    echo ""
    echo "You Selected $REPLY for:
$line"
    echo "Continue y/n?"
    read reply1
    if [ "$reply1" = "y" ]
    then
    echo "Working . . ."
    ./maketicket
    else
    echo "Request Cancelled";sleep 1
    [COLOR=red]break[/color]
    fi
    done;;




"Proof that there is intelligent life in Oregon. Well, Life anyway.
 
I had removed the calls of ./script as well as add a break, but it did not work, so I just went back to my original code. I will try your suggestions again, post the edited code and tell you the results. thanks.
 
Edited code:

#!/bin/ksh

while true
do

echo "
1) View ALL Failed Save Sets
2) Create Tickets for SERVER1 Failed Save Sets
q) Quit"
echo ""

read input
case $input in
1) clear
echo ""
echo "Listing of All Failed Save Sets:"
echo ""
awk '{print $0}' data.dat;;
2) clear
grep server1 data.dat > tmp.server1
echo "Select Failed Save Set to Create Ticket"
echo "Listing of Failed Save Sets:"
echo ""
IFS="
"
select line in $(<tmp2.server1);do
echo ""
echo "You Selected $REPLY for:
$line"
echo "Continue y/n?"
read reply1
if [ "$reply1" = "y" ]
then
echo "Working . . ."
./maketicket2
else
echo "Request Cancelled";sleep 1
break
fi
done;;
q) echo "Exiting . . . ";sleep 1;exit;;
*) echo "Please Make Another Selection";sleep 1;;
esac
done

Results:
After chosing option 2 and doing work, the script returns to the main menu as expected. I then hit q and I am thrown into the select prompt like:

q
Exiting . . .
#?
 
I'd put the break in the inner loop here, so that you break out of it in whatever you choose (not) to do in the select loop... (I also modified the indentation a bit, which makes it more understandable to me)


2) clear
grep server1 data.dat > tmp.server1
echo "Select Failed Save Set to Create Ticket"
echo "Listing of Failed Save Sets:"
echo ""
IFS="
"
select line in $(<tmp2.server1)
do
echo ""
echo "You Selected $REPLY for:
$line"
echo "Continue y/n?"
read reply1
if [ "$reply1" = "y" ]
then
echo "Working . . ."
./maketicket2
else
echo "Request Cancelled"
sleep 1
fi
break
done;;


Q: In option 2 you prepare a file tmp.server1, then you use a file tmp2.server1 in the select loop? A typo error perhaps?


HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top