Link to home
Start Free TrialLog in
Avatar of wmv678
wmv678

asked on

Script to compile multiple .c files and output errors to their own files

The task is to write a simple Linux shell script that will do the following:

For each .c file that exists in the directory
  - Compile the code, code_1.c
  - Output to a file, code_1.err
  (command i use for one file: g++ -I ../include_files code_1.c 2>code.err)
Next .c file

Requirements:
1.  It must do these two things for every .c file that exists in the directory
2.  The .err file must have the same name as the original .c file

I've never written a shell script before, so be detailed in your response.  Anyone who can just quickly write the code for me will receive 500 points immediately.  I need this ASAP.  Thanks in advance!!

Bill
Avatar of wmv678
wmv678

ASKER

So far, I can make this which works:

#!/bin/bash
# Compiles all .c files in the directory and outputs to a file

g++ -I ../comh ACTF60.c 2>errors/ACTF60.err
g++ -I ../comh aboa40.C 2>errors/aboa40.err


I just need to know how to make this work so that it loops through all .c files in the directory.  As there is over 500 of them, I don't want to type them all in like above.
ASKER CERTIFIED SOLUTION
Avatar of _corey_
_corey_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wmv678

ASKER

haven't tried it yet, but that looks good.  But I still need the error output for each compilation to go to the same file_name.err.  Your code, as I see it, would all go to the same file, ACTF60.err, using the example.
Avatar of wmv678

ASKER

to clarify, I want

code_1.c to output to code_1.err
code_2.c to output to code_2.err
...
code_x.c to output to code_x.err
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The same will work for bash.

corey
Avatar of wmv678

ASKER

Thanks guys.  I ended up with a modified version of corey's:

#!/bin/bash
# Compiles all .c files in the directory and outputs to a file

for CFILE in *.c
do
        g++ -I ../comh "$CFILE" 2>"errors/$CFILE.err"
done
echo "Done."
echo "Compile errors written to directory: errors/"


koppcha- i couldnt get yours to work, but thanks for the help.
It should work ...anyways you got it done
Keep up the good work :)
#!/bin/bash
# Compiles all .c files in the directory and outputs to a file

for CFILE in *.c
do
        EFILE=${CFILE%.c}.err
        g++ -I ../comh "$CFILE" 2>"errors/$EFILE"
done
echo "Done."
echo "Compile errors written to directory: errors/"

should work.

corey
You must follow the corey bash script to get the desired results.
The script provided by you doen't generate the outputfiles with the desired names. Please check it.