?
Solved

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

Posted on 2005-04-27
10
Medium Priority
?
290 Views
Last Modified: 2010-04-21
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
0
Comment
Question by:wmv678
  • 4
  • 3
  • 3
10 Comments
 

Author Comment

by:wmv678
ID: 13879650
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
 
LVL 8

Accepted Solution

by:
_corey_ earned 1200 total points
ID: 13879736
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
 

Author Comment

by:wmv678
ID: 13879852
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:wmv678
ID: 13879862
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
 
LVL 8

Assisted Solution

by:koppcha
koppcha earned 300 total points
ID: 13880079
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
 
LVL 8

Expert Comment

by:_corey_
ID: 13880130
The same will work for bash.

corey
0
 

Author Comment

by:wmv678
ID: 13880461
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
 
LVL 8

Expert Comment

by:koppcha
ID: 13880497
It should work ...anyways you got it done
Keep up the good work :)
0
 
LVL 8

Expert Comment

by:_corey_
ID: 13880498
#!/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
 
LVL 8

Expert Comment

by:koppcha
ID: 13880590
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

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Integration Management Part 2
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question