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
wmv678Asked:
Who is Participating?
 
_corey_Commented:
I believe something like:

#!/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/ACTF60.err
done

I would replace the space indentation with a tab.

corey
0
 
wmv678Author Commented:
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.
0
 
wmv678Author Commented:
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.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
wmv678Author Commented:
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
0
 
koppchaCommented:
Simple korn shell script ..This should do..


#!/bin/ksh
for fileName in *.c
do
newFileName=${fileName%.c}.err
g++ -I ../comh $fileName  2>errors/$newFileName
done
0
 
_corey_Commented:
The same will work for bash.

corey
0
 
wmv678Author Commented:
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.
0
 
koppchaCommented:
It should work ...anyways you got it done
Keep up the good work :)
0
 
_corey_Commented:
#!/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
0
 
koppchaCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.