The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Goto last visted directory prasanth_babu SUN Solaris 6 08-11-2008 12:17 AM
Stuck after typing goto rairey UNIX for Dummies Questions & Answers 2 09-20-2007 10:19 AM
Use of GOTO statement in scripts vikasduhan Shell Programming and Scripting 7 02-02-2006 08:50 AM
GOTO LOOP in KORNE SHELL DeepakXavier Shell Programming and Scripting 1 11-10-2005 08:47 AM
how to goto in ksh Geraldine Shell Programming and Scripting 4 03-12-2004 04:35 AM

Closed Thread
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-20-2007
Registered User
 

Join Date: Dec 2006
Location: CA,United States
Posts: 174
Stumble this Post!
goto statement

I have a test script for using goto statement but its not working. please help

i tried both in linux and hp-ux it's not working please help

#! /bin/ksh
t=`ps -ef|grep ti.sh|grep -v grep`
if [ $? -eq 0 ]; then
goto start
else
goto stop
fi
start:
echo "start"
stop:
echo "stop"
Forum Sponsor
  #2 (permalink)  
Old 02-20-2007
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,846
Stumble this Post!
There is no goto statement in ksh. Other shells support it, for example, tcsh.
  #3 (permalink)  
Old 02-20-2007
Registered User
 

Join Date: Jan 2007
Posts: 366
Stumble this Post!
Quote:
Originally Posted by Krrishv
I have a test script for using goto statement but its not working. please help

i tried both in linux and hp-ux it's not working please help

#! /bin/ksh
t=`ps -ef|grep ti.sh|grep -v grep`
if [ $? -eq 0 ]; then
goto start
else
goto stop
fi
start:
echo "start"
stop:
echo "stop"
With all respect, a goto statement is for those who cannot program. It is a "dirty" way to jump from 1 place in the code to another.

As already mentioned, a goto statement doesnt exist in a Korn Shell.

Instead you could define 2 functions.

Code:
#! /bin/ksh

start()
{
  echo "start"
}

stop()
{
  echo "stop"
}

t=`ps -ef|grep t[i].sh`
if [ $? -eq 0 ]
then
  start
else
  stop
fi
By putting "[]" around 1 letter of the search pattern in the grep command there is no more need for the "grep -v grep" command.

The "grep -v grep" was wrong in this scenario anyway.
Because when you check for the return code "$?", you actually check the return code of the command "grep -v grep" and not of the command "grep ti.sh".
  #4 (permalink)  
Old 02-20-2007
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,536
Stumble this Post!
Or:

Code:
ps -ef|grep [t]i.sh>&-&&echo start||echo stop
With some shells you'll get an error with the above operation (closing fd1),
so redirecting fd1 to /dev/null will be more portable,
and, of course, with GNU grep you can use the "q" option.

Last edited by radoulov; 02-20-2007 at 01:54 PM.
  #5 (permalink)  
Old 02-21-2007
Moderator
 

Join Date: Feb 2007
Posts: 1,953
Stumble this Post!
Quote:
Originally Posted by sb008
With all respect, a goto statement is for those who cannot program. It is a "dirty" way to jump from 1 place in the code to another.
Never seen the Linux Kernel source code?
Sometimes goto statements can make the code more efficient, cleaner and concise.

Linus about the use of goto's:

Quote:
I think goto's are fine, and they are often more readable than large
amounts of indentation. That's _especially_ true if the code flow isn't
actually naturally indented (in this case it is, so I don't think using
goto is in any way _clearer_ than not, but in general goto's can be quite
good for readability).

Of course, in stupid languages like Pascal, where labels cannot be
descriptive, goto's can be bad. But that's not the fault of the goto,
that's the braindamage of the language designer.

Linus
Regards
  #6 (permalink)  
Old 02-21-2007
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,444
Stumble this Post!
According to the rules:

(8) No BSD vs. Linux vs. Windows or similar threads.

And that applies to arguing about goto's as well. The question has been answered so I will close this thread.
Google The UNIX and Linux Forums
Closed Thread

Tags
linux

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:41 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0