Find the answer to your Linux question:
Results 1 to 3 of 3
I am doing a for loop in a bash script that automates compilation for a few packages. But I do not know how to make bash stop its loop when ...
  1. #1
    Just Joined!
    Join Date
    Nov 2007
    Posts
    7

    stopping on error in a bash loop

    I am doing a for loop in a bash script that automates compilation for a few packages. But I do not know how to make bash stop its loop when an error is spawned.

    How to do that in a bash's for loop ?

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Code:
    #!/bin/bash
    
    for ((jndex=0;jndex<10;jndex++))
    do
      date
      sleep 1
    
      # Simulate an error after a few times through the loop.
    
      if [[ $jndex -ge 4 ]]
      then
        asdf
      fi
    
      # Here we check for error and exit the loop.
    
      if [ $? -ne 0 ]
      then
        break
      fi
    done
    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined!
    Join Date
    Nov 2007
    Posts
    7
    this works perflectly, thnx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...