Visit Our UNIX and Linux User Community


"goto" like command in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting "goto" like command in UNIX
# 1  
Old 03-05-2014
"goto" like command in UNIX

Hi,

Code:
 
echo "yes or no?"
read ans
 
case $ans in
 
[yY]*)
echo "yes"
;;
 
[nN]*)
echo "no"
;;
 
*)
echo "yes or no only"
#here, if the answer is not "Y" or "N", I want to go back to asking "yes or no?"
;;
esac

Thank you.
# 2  
Old 03-05-2014
Code:
while [ 1 ]
do
    echo "yes or no?"
    read ans
    case $ans in
    [yY]es)
        echo "yes"
        # more statements
        break
        ;;
         
    [nN]o)
        echo "no"
        # more statements
        break
        ;;
    *)
        echo "yes or no only"
        ;;
    esac
done

These 2 Users Gave Thanks to balajesuri For This Post:
# 3  
Old 03-05-2014
Shell scripting has no concept of GOTO.

Try using functions for your "yes/no" results and run your "case" routine inside a "while"
loop remembering to call you functions...
Example:-
Code:
yes()
{
        echo "Do something."
}
no()
{
        echo "Do something else."
}
while true
do
        # Your case code, remembering to call "yes" or "no" as commands...
        # Remember also to give yourself a getout clause...
done

Off to work...

Last edited by wisecracker; 03-05-2014 at 04:18 AM.. Reason: Typo...
This User Gave Thanks to wisecracker For This Post:
# 4  
Old 03-05-2014
Perhaps a little background could be useful Goto considered harmful (Edsger Dijkstra)
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 03-05-2014
For endless loops consider
Code:
while :

rather than
Code:
while [ 1 ]

Saves you 4 keystrokes (and a couple of CPU cycles with each iteration).
# 6  
Old 03-05-2014
Quote:
Shell scripting has no concept of GOTO.
Sure it does. The break and continue statements are limited gotos. And are always translated to jumps in assembly language.
This User Gave Thanks to fpmurphy For This Post:
# 7  
Old 03-05-2014
GOTO style statements are a bad idea. Try to work out the functions of your process. I learnt this on Sinclair BASIC of all things. Using GOTO is easy to understand and GOSUB seemed excessive.

Eventually I was enlightened at school. The overall target of your code should be to perform a process. That process was split into major components, and if necessary a selection made as to which parts actually ran. For each of these major parts, we repeated the approach, and again until we could not refine it further. Each leg/relationship was either a loop, a condition or a single sequence.

Effectively coding up the lowest level respective the relationships would build your application. GOTO was not a recommended even then. We had to make subroutine and then manage the flow. Drawing up a flowchart type design with all sorts of branches quickly becomes unworkable and is a nightmare to decipher, let alone alter, evidenced by plenty of our COBOL code and C-shell scripts that I've had to extract us from in the past.

We have more ksh scripts to pick apart soon with all sorts of mad things like:-
Code:
while true
do
  some-process........
  if it worked
  then
     break        # End the while true loop
  fi
  another-process......
  if it worked
  then
     continue     # Re-start while true loop
  fi
  echo "Failed both things"
done              # Back to while true loop

echo "Finished"

It's just a mess when you end up with a script running to over 500 lines with one main while true loop and a huge case statement to have sections that should or shouldn't run.


Using functions helps organise your thoughts to build more structured code making it easier to understand later (when the documentation is usually long gone)


I have a long project to dismantle it all to more easily maintained code as we move the application to a replacement server. Deep joy fills me soul with the prospect. Smilie




Robin

Last edited by rbatte1; 03-05-2014 at 10:40 AM.. Reason: Emboldening

Previous Thread | Next Thread
Test Your Knowledge in Computers #461
Difficulty: Medium
Simple Network Time Protocol (SNTP) is a less complex implementation of NTP, using the same protocol but without requiring the storage of state over extended periods of time.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing To e-mail address and column 3 contains cc e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. UNIX for Dummies Questions & Answers

Unix "look" Command "File too large" Error Message

I am trying to find lines in a text file larger than 3 Gb that start with a given string. My command looks like this: $ look "string" "/home/patrick/filename.txt" However, this gives me the following message: "look: /home/patrick/filename.txt: File too large" So, I have two... (14 Replies)
Discussion started by: shishong
14 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

6. UNIX for Advanced & Expert Users

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

7. UNIX for Dummies Questions & Answers

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

8. UNIX for Dummies Questions & Answers

Unix "at" / "Cron" Command New Problem...Need help

Hi All, I am trying to schedule a one time job using the at command with the help of shell script for my project. The shell script should take a parameter as a command line argument from the at command itself. Is it possible to take a command line parameter for a shell script in the command... (3 Replies)
Discussion started by: Mohanraj
3 Replies

9. UNIX for Dummies Questions & Answers

Is there a "goto" command for AIX?

I have a korn script that needs to check for an empty file -- if the file is empty, I want the script to execute a cleanup step before exiting. Here's what I have: if ] then echo " Input file has no records -- skipping to step 9." goto cleanup fi ... ... (3 Replies)
Discussion started by: jderr
3 Replies

10. UNIX for Advanced & Expert Users

Commands on Digital Unix equivalent to for "top" and "sar" on other Unix flavour

Hi, We have a DEC Alpha 4100 Server with OSF1 Digital Unix 4.0. Can any one tell me, if there are any commands on this Unix which are equivalent to "top" and "sar" on HP-UX or Sun Solaris ? I am particularly interested in knowing the CPU Load, what process is running on which CPU, etc. ... (1 Reply)
Discussion started by: sameerdes
1 Replies