goto statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting goto statement
# 1  
Old 02-20-2007
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"
# 2  
Old 02-20-2007
There is no goto statement in ksh. Other shells support it, for example, tcsh.
# 3  
Old 02-20-2007
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  
Old 02-20-2007
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 05:54 PM..
# 5  
Old 02-21-2007
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  
Old 02-21-2007
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

A dash to GOTO or a dash from GOTO, that is the question...

Well, guys I saw a question about GOTO for Python. So this gave me the inspiration to attempt a GOTO function for 'dash', (bash and ksh too). Machine: MBP OSX 10.14.3, default bash terminal, calling '#!/usr/local/bin/dash'... This is purely a fun project to see if it is possible in PURE... (3 Replies)
Discussion started by: wisecracker
3 Replies

2. Shell Programming and Scripting

Alternative for goto

#!/bin/sh label: echo sql poll v=`sqlplus -s <<! HR/HR set pages 0 echo off feed off select distinct status from emp where id=5; ! ` echo $v; echo it comes here after false if then echo if condition true sqlplus -l scott/tiger <<EOF select * from department; EXIT (2 Replies)
Discussion started by: kumaar1986
2 Replies

3. Shell Programming and Scripting

How to use GOTO stmt in Unix scripting?

my code does somthing like this: #!bin/ksh sqlplus / | While read id do temp=`echo $id` i = i+1 done j=0 while do --connecting to sql and executing a Stored proc for 1st id --checking for the status status = $? if error --need to... (1 Reply)
Discussion started by: RP09
1 Replies

4. Shell Programming and Scripting

Use awk/sed/grep with goto statement!

Hi, I have an array with characters and I am looking for specific character in that array and if those specific character not found than I use goto statment which is define somehwhere in the script. My code is: set a = (A B C D E F) @ i = 0 while ($i <= ${#a}) if ($a != "F" || $a != "D")... (3 Replies)
Discussion started by: dixits
3 Replies

5. Shell Programming and Scripting

Using A Goto Label?

Im trying to do something like this but I cant find any documentation. read X if then goto ThisLine fi OTHER CODE OTHER CODE Label: ThisLine echo "You entered 1" (5 Replies)
Discussion started by: Grizzly
5 Replies

6. Solaris

Goto last visted directory

Dear All, Can any one pls let me the command for how to goback to previous visited directory from the current working directory in SunOS ? In case of HP-UX; the same can be resolved through "cd -" command. Thanks in advance! Prasanth Babu. (6 Replies)
Discussion started by: prasanth_babu
6 Replies

7. UNIX for Dummies Questions & Answers

Stuck after typing goto

uname -a returns: SMP Tue May 17 17:52:23 EDT 2005 i686 athlon i386 GNU/Linux I have many aliases beginning with "goto" so... if I type goto and then hit return (oops) A goto prompt pops up and I cant exit from it(I tried MANY key seqs) The only way to exit is to kill the term window... (2 Replies)
Discussion started by: rairey
2 Replies

8. Shell Programming and Scripting

Use of GOTO statement in scripts

Hey Guys.. I just want to know how to use Goto statement in shell scripts. I know the basic use of statement. Goto Label The above statement will search for some label which must be defined in the script itself as: label: I tried these combinations but I didn't work out for me and I'm... (7 Replies)
Discussion started by: vikasduhan
7 Replies

9. Shell Programming and Scripting

Unix version of gosub or goto??

Is there a Unix (Solaris v8 ksh) version of the old basic command gosub or goto? I've researched the return command, but can't identify the command to return from. I am not trying to return from a function. Can someone give me a basic understanding of this please? Thanks so much in advance for... (3 Replies)
Discussion started by: gozer13
3 Replies

10. Shell Programming and Scripting

how to goto in ksh

Hi, I'm trying to use the goto in ksh but it does not appear to be a valid command. Is that only valid in csh? Anything similar in ksh that I can use? Appreciate any help you can provide. Thanks. geraldine (4 Replies)
Discussion started by: Geraldine
4 Replies
Login or Register to Ask a Question