Looping structure to make up for lack of bash GOTO


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Looping structure to make up for lack of bash GOTO
# 1  
Old 06-02-2014
Looping structure to make up for lack of bash GOTO

Hello,

I am re-processing some files when a specific condition is met. The condition is read from the filename. Since files may need to be re-processed a number of times before they no longer meet the condition, I need to know when to stop re-processing. I am having trouble visualizing the proper control structure. What I had in mind was something like,
Code:
1: look through the list of files and find those that meet the re-process condition
2: increment a counter to track the number of files that meet the contrition
3: re-process files that meet the condition
4: GOTO 1 unless the counter==0

The above code would nicely keep looping until there were no compliment files found. The somewhat annoying lack of a GOTO statement makes takes this option off of the table, so I need a replacement.

I guess that some kind of a while loop would be the best alternative, but I don't know what bash has to offer for loops other than a do loop.

Can someone make a suggestion for this. I can post more code if that would help.

---------- Post updated at 10:25 PM ---------- Previous update was at 08:44 PM ----------

At the moment, I am trying something like this,
Code:
# zero stop condition
STOP=0

 # keep looping until stop condition is met
 while [ "$STOP" -ne "1" ] 
 do

   ###create list of current files
   $FILES

   ### check list for re-process condition and add filename to array
   for FILENAME in $FILES
   do
      if [ "$VALUE1" == "$VALUE2" ]; then
         FILENAMES_TO_PROCESS=("${FILENAMES_TO_PROCESS[@]}" "$FILENAME")
      fi
   done

   ### check if any files were found to re-process
   # if no files are found that meet the re-process condition
   if [ "${#FILENAMES_TO_PROCESS[@]}" == "0" ]; then
      echo "no files require reprocessing"
      # set condition to exit while loop
      STOP=1
   # if files are found, re-process files on the list
   else
      # rm file that is being reprocessed
      # code to reprocess files that need it
      ...
   if

   # do statistics on re-processed files, this will create an updated filename for any re-processed file
   # filenames of re-processed files will be re-checked on next loop

done

As far as I can tell, this will re-process all files that need it. Re-processing will create a new set of file names. The code will loop back to check the new set of file names to see if anything still needs to be re-processed. The code will keep looping until no file names are found that need to be re-processed.

I still seem to be incapable of writing code without multiple nested loops.

Is there anything wrong with this that so far?

LMHmedchem
# 2  
Old 06-03-2014
Why not try
Code:
while [ counter -gt 0 ]
    do ( your steps 1 .. 3 )
    done
echo "no files require reprocessing"

# 3  
Old 06-03-2014
Not having a GOTO is actually very good for structured programming. Think of the process you are trying to achieve and then split every action into simpler steps, then repeat until you can go no smaller.

Each final action should be either in a sequence, a condition or a loop. Using GOTO allows you to write spaghetti that is difficult to follow when you need to trace an error. It's lazy really.

If you write functions for the section of code you may or may not need, then a simple loop at the end actually makes it easier.
Code:
#!/bin/bash
function1()
{
 echo this is function 1
}
function2()
{
 echo this is function 2
}
function3()
{
 echo this is function 3
}

f=1
while [ $f -le 3 ]
do
   function$f
   ((f=$f+1))
done

or
Code:
:
:
while true
do
   function$f
done

with which you need to call exit from one of the functions.

The shells sh, ksh, bash etc. are not limited. They work as designed and deny the lazy use of GOTO which can cause more problems than the minor inconvenience it leaves.
# 4  
Old 06-03-2014
I too felt the absence of 'goto' at first but mostly because I didn't realize at the time the possibilities its loop structures give you.

Try the break statement, which immediately stops the innermost loop.
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. UNIX for Beginners Questions & Answers

How to make paste -d second file print down while looping?

]I would like to make the second file label 'b' print down the first file label 'a', like shifting down the file creating new lines I want it to print all the way down until the first line of the second file hit the last line of the first file. Would I have to put this into a file itself or could I... (24 Replies)
Discussion started by: bigvito19
24 Replies

3. Shell Programming and Scripting

JSON structure to table form in awk, bash

Hello guys, I want to parse a JSON file in order to get the data in a table form. My JSON file is like this: { "document":{ "page": }, { "column": } ] }, { ... (6 Replies)
Discussion started by: Gescad
6 Replies

4. Shell Programming and Scripting

Bash to goto specific line/function and start processing if user response is yes

In the bash below I am trying to run the script entire script including the ....(which is a bunch of code) and then in the run function if the user response is y (line in bold). then start processing from execute function. Basically, goto the # extract folder for variable filename line and start... (4 Replies)
Discussion started by: cmccabe
4 Replies

5. Shell Programming and Scripting

Bash Script Looping all the time

Hello, I have a database file, named data.txt, and a shell script (convert.sh) to convert data.txt from columns to row. Output file name will be column_to_row.txt In this example data.txt has only four rows. Format of data.txt is: info name surname telefon_nr Data.txt info boris... (1 Reply)
Discussion started by: baris35
1 Replies

6. Shell Programming and Scripting

Looping Bash Script

Does anyone have a same of a bash script that cd to a directory and execute a cgi script then moves onto the next directory then executes another cgi ? (3 Replies)
Discussion started by: Virusbot
3 Replies

7. Shell Programming and Scripting

how to make your bash script run on a machine with csh and bash

hi, i have a script that runs on bash and would like to run it on a machine that has csh and bash. the default setting on that machine is csh. i dont want to change my code to run it with a csh shell. is there any way i can run the script (written in bash) on this machine? in other words is there... (3 Replies)
Discussion started by: npatwardhan
3 Replies

8. Shell Programming and Scripting

looping in hexadecimal with bash

i want to write a script that creates a directory tree named in hexadecimal but i'm stuck at the hexadecimal part ... here is my code (incase i was dealing with intergers ...using bash) max=39 for((i=1;i<=max;i++)) do mkdir $i cd $i done i have tried using "typeset i16 i" but... (7 Replies)
Discussion started by: soba
7 Replies

9. UNIX for Dummies Questions & Answers

looping in hexadecimal with bash

hello every one this is my first post ... well i'm new to linux .... i've been enjoying shell scripting tutorials and i'm new to writting scripts i want to write a script that creates a directory tree named in hexadecimal but i'm stuck at the hexadecimal part ... here is my code (incase i... (2 Replies)
Discussion started by: soba
2 Replies

10. Shell Programming and Scripting

Need script to make big directory structure

Hi, I'm a novice and I'd like to make a directory structure with a hundred or so folders. I've tried mkdir /foo/foo1/etc... mkdir /foo/foo2/etc mkdir /foo/foo3/etc mkdir /foo/foo4/etc ...but it appends '@' to each folder name and then fails on the subdirectories. Is it better to use a... (2 Replies)
Discussion started by: kamur
2 Replies
Login or Register to Ask a Question