0

I am trying to run multiple loops in bash. For example there are 3 file formats f1=*.txt f2=*.png and f3=*.jpg

and i am trying to give the three different commands for each as

for f1 in *.txt; do echo $f1; command 
exit 0

for f2 in *.png; do echo $f2; command
exit 0

for f3 in *.jpg; do echo $f3; command
exit 0 

but no success, I am getting a broken syntax. I replaced exit 0 with && but no success. Any idea?

1
  • 1
    I still don't understand what you are trying here Commented Mar 26, 2014 at 23:34

2 Answers 2

3

Loops in bash terminate with done, not exit.

for f1 in *.txt; do  # loop starts here
  echo $f1
  command 
done                 # loop ends here

# or keep everything on the same line
for f2 in *.png; do echo $f2; command; done

You can still use exit to exit from your script completely, but done is the expected way to signal the end of a loop begun with for, while, or until.

Other shell constructs have their own terminator, like fi to terminate if, and esac to terminate case.

See more about loop syntax here.

Sign up to request clarification or add additional context in comments.

Comments

2

You should be using:

for file in *.txt; do echo $file; echo command $file; done
for file in *.png; do echo $file; echo command $file; done
for file in *.jpg; do echo $file; echo command $file; done

Or variations on that theme. You can use f1, f2, and f3 if you prefer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.