Find the answer to your Linux question:
Results 1 to 4 of 4
Hi, I have written a bash script to compile multiple programs. The script runs a loop and at a certain point calls the compiler. However as soon as the first ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2011
    Posts
    3

    Need help with bash script looping


    Hi,

    I have written a bash script to compile multiple programs. The script runs a loop and at a certain point calls the compiler. However as soon as the first compilation fails, it exits the loop. What I want is, the compilation failure should be informed (written in an error file) but the script should complete all compilations.

    If I remove the erroneous program, the loop completes. However as soon as a compilation error occurs, the script exits.

    Someone please help.

    Regards,
    Alok
    Last edited by khandaa; 04-08-2011 at 09:43 AM. Reason: typing mistakes

  2. #2
    Linux User
    Join Date
    Nov 2008
    Location
    Tokyo, Japan
    Posts
    260
    Is your script Bash, or GNU Make?
    Which programming language are you using?
    Which compiler are you using?

    I'd like to see some example code to better understand your problem.

    Here is how I would write a compile loop using Bash.
    Code:
    #!/bin/bash
    
    cd my/source/directory
    
    # Assuming "$EXT" is a variable with the value set to
    # the file extension of the source code you want to compile,
    # for example: "c", "py", or "ml".
    
    for CODE_FILE in *".$EXT"
    do
      ERROR_FILE=${CODE_FILE%%"$EXT"}.error
      if compile $CODE_FILE 2>$ERROR_FILE
      then rm -f $ERROR_FILE
      else echo "Compiling \"$CODE_FILE\" failed:"; cat $ERROR_FILE
      fi
    done

  3. #3
    Linux Newbie Ziplock's Avatar
    Join Date
    Jan 2009
    Location
    Adelaide
    Posts
    169
    Hi there,

    I have had this type of trouble before building RPMs. My solution was the following, although other people may have better ideas:

    Code:
    command || /bin/true
    This let the build script I was writing continue when something broke, I hope it helps!

  4. #4
    Just Joined!
    Join Date
    Apr 2011
    Posts
    3

    Need help with bash script looping

    Hello Ramin and Ziplock,

    Thanks for your feedback. I tried both options and the one suggested by Ziplock worked. Your help has been a lifesaver for me. Thankyou very much.

    btw, Ramin, my script is bash script, programming language is C and compiler gcc.

    regards,
    Alok

Posting Permissions

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