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

If go here, else go there

Status
Not open for further replies.

BIS

Technical User
Jun 1, 2001
1,893
NL
Hallo,

How would I write a shellscript that

if (something true)
then (go to this step/line)
else (go to that step/line)
fi

Is this possible?

Or would I need something like

While (something not true)
do (something)
else (go to this step/line)

Can someone point me in the general direction?
 
hi

it depends how many times you want the task doing

if once use the if statement

if you need to do it until the user exits or enters a correct option then use the while loop

HTH
 
Ok I would use a function for this.

#!/usr/bin/ksh

Bugs() # first function
{
echo " Whats up Doc"
}
Porky() # Second function
{
echo " Thats all folks"
}

if [ true ]
then
Bugs
else
Porky
fi



Ok pretty basic but it should get u started.
lookup function and if
man function
man if
for more details.
Or you can ask for more details here.
Good Luck
Doug


 
what about 'case" ???
case ${aa:-$1} in abba) do this
;; *) do that
;; esac
exit 0
 
Mnay thanks all, that should get me going.
 
Hallo Folks,

Is it possible to call a function from within another function?

Bob()
{
echo "Bob"
}

Billy()
{
echo "Billy"
Bob
}





 
... or alternatively, is it possible to call two functions in an if/then statement

if
(someting)
then
Bob
else
Billy AND Bob
fi

 
I do not recall for all shells, but in Korn (ksh), functions calling functions are fine. In fact, I have several "utility" functions I copy into the top of scripts so I do not have to rewrite the code.
 
In answer to you questions on funtions.

name all your functions at the top of your script and then you can call them from anywhere.
From within a loop, from within another function, or just for fun anytime.

Here is a webpage I found most helpful

Good luck
Doug
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top