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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there something like "goto" in ksh? 1

Status
Not open for further replies.

Jalr2003

Technical User
May 15, 2003
26
0
0
US
Is there a way to use labels and "goto" in ksh? how do I get a similar results with ksh?

Thanks for your help

jalr2003
 
No goto in ksh (and IMHO no need) but a lot of structured instructions.
In the result of a
Code:
 man ksh
command, pay attention to case, while, until, if, function, {, ( ...


Hope This Help
PH.
 
PH,

Actually what I am trying to do is to echo a menu with some selections, read the keystroke and save it into a variable. Then I use a case statement to perform several actions according to what was selected.

The problem is when the user enters something wrong. I want the scrip to show an error message and go back to the menu.

Something like this:

echo " Select option

1) Car
2) Plane
3) Boat
"
read keypress
case $keypress in
1) echo "You have selected Car "
db=cars
((c_flag=1));;
2) echo "You have selected Plane "
db=planes
((p_flag=1));;
3) echo "You have selected boat "
db=boats
((b_flag=1));;
*) echo "
!!!WRONG SELECTION. TRY AGAIN
"

And Here I would like to make the scrip display the menu again and wait for the user input. Is it possible? any better ideas?

;;
esac

Thanks for your help,

Jalr2003
 
Try something like this:
Code:
while echo "  Select option\n
  1) Car
  2) Plane
  3) Boat
"; do
  read keypress
  case $keypress in
  1) echo "You have selected Car "
     db=cars 
     ((c_flag=1));;
  2) echo "You have selected Plane "
     db=planes
     ((p_flag=1));;
  3) echo "You have selected boat "
     db=boats 
     ((b_flag=1));;
  *) echo "
          !!!WRONG SELECTION.  TRY AGAIN
"; continue;;
  esac
  break
done

Hope This Help
PH.
 
Another way:
Code:
PS3=$(echo "Q) Quit\nSelect option  ")
select X in Car Plane Boat; do
  echo "You have selected $X"
  case $REPLY in
  1) db=cars;((c_flag=1));break;;
  2) db=planes;((p_flag=1));break;;
  3) db=boats;((b_flag=1));break;;
[qQ]*) exit;;
  *) echo "
          !!!WRONG SELECTION.  TRY AGAIN
";;
  esac
done
echo "db='$db'"

Hope This Help
PH.
 
It worked.

Thank you very much,

Jalr2003
 
I encounter a similar dilemma some time back and learned about the use of a function.
Here is a much better description than i could give.


I now use funtions in all my shell scripts as this helps to keep them neat and clean. Plus then each function becomes a peice of code I can eaisly re-use in other scripts.

For example I use a funtion to test the login ID and if they are DCE authenticated, I re-use this a lot.

Good luck


As always we thank you for your support
Doug
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top