Operating System - HP-UX
1820260 Members
2986 Online
109622 Solutions
New Discussion юеВ

"goto" equivalent in shell script

 
SOLVED
Go to solution
Maciej Szewczykowski
Frequent Advisor

"goto" equivalent in shell script

hello everybody,
my question is: is there an operation allowed in shell script language that has the same output as "goto" in C++? lets assume that i've written a command, its output is either 0 or 1, in case it's 1, i'd like to continue running the script from line of a certain number - that's what i need "goto" for.
thanks in advance.
8 REPLIES 8
Geoff Wild
Honored Contributor

Re: "goto" equivalent in shell script

several ways....you could do it with functions:

function task1{
echo "hello world"
done
}

##########################################################################
# Main Program #
##########################################################################

case $1 in

hello ) task1 ;;

esac

Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
T G Manikandan
Honored Contributor

Re: "goto" equivalent in shell script

There is no equ. of goto in shell.

check for the 'break' statement which can be used for the same.

Thanks
Maciej Szewczykowski
Frequent Advisor

Re: "goto" equivalent in shell script

thanks, but can you propose some other way to proceed it?
John Meissner
Esteemed Contributor
Solution

Re: "goto" equivalent in shell script

I would use functions (as Geoff mentioned above)

function myfunction {
script stuff
}

while blah blah blah....
do
myfunction
done

if [ blah blah ]
then
myfunction
fi

you can call that same function at any time in the script which will work as a "goto" of sorts.

the case (as also mentioned above) will let you do a similar thing as the function... but call various things.... I use this when creating a menu in a script.
The below excert from one of my scripts is written as a function... but also has a case statement used.

function user_grp {
clear
echo " Enter the user you wish to assign to a group:\c"
read user
clear
echo " Assign $user into a group"
echo " ${BOLD}1${NORMAL} - ART-ISC"
echo " ${BOLD}2${NORMAL} - GTS"
echo " ${BOLD}3${NORMAL} - SAP"
echo " ${BOLD}4${NORMAL} - Telecoms"

until [ "$group" = "1" ] || [ "$group" = "2" ] ||
[ "$group" = "3" ] || [ "$group" = "4" ] ; do
echo "Please select one of the menu options"

read group
done
case $group in
1)
grp=ART-ISC
;;
2)
grp=GTS
;;
3)
grp=SAP
;;
4)
grp=Telecoms
;;
esac
echo "$user:$grp:n:*" >> ~/scmauth/auth
mxauth -a -f ~/scmauth/auth
echo "authorization complete"
echo "$user added to $grp on all servers"
echo "$user was added to $grp on all servers by `logname` on `date` " >> ~/scmauth/auth.added.log
more auth >> auth.granted
rm auth
touch auth
sleep 5
~/scmauth/scm_menu
}


All paths lead to destiny
Darren Prior
Honored Contributor

Re: "goto" equivalent in shell script

Hi,

Although there may be a goto statement it's not a particularly nice way to program!

How about something like:

your_user_command
if [[ $? -eq 0 ]]
then
do stuff for command outputting 0
else
do stuff for command outputting 1
fi

or you could use functions to separate it out. You may need to rethink the script; but it will be more structured and easier to follow.

regards,

Darren.
Calm down. It's only ones and zeros...
John Meissner
Esteemed Contributor

Re: "goto" equivalent in shell script

dating back to my Basic programming class I took - goto is used to go to a specific line number. probably more advanced now - but that's what I remember. Scripting uses logic and flow control to process such things.... if then statements.... functions.... variables..... exit codes.... case statements....
There is a bit to learn to change the way you approach this problem. That's why we're here :) glad to help with any scripting questions you may have.
All paths lead to destiny
Maciej Szewczykowski
Frequent Advisor

Re: "goto" equivalent in shell script

great thanks john, thank you all guys.
Steve Steel
Honored Contributor

Re: "goto" equivalent in shell script

Hi


Strange thing with scripting.

. script

Will not fork but continue at the same level.

Thus you can even


if [[ $? -eq 0 ]]
then
. scriptres0
else
. scriptres1
fi

And insert small scripts into your script depending on the result.


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)