LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-05-2005, 12:54 PM   #1
manojg
Member
 
Registered: May 2004
Posts: 78

Rep: Reputation: 15
shell script to compile


Hi,

I using some data analysis. I have divided the a big data file into 300 smaller files. I gave the name of the files in numeric order (1.dat, 2.dat ....). From my code, I will get output data files (1.out, 2.out ....) corresponding to each data file. During compilation I have to put the name of the input data file and output file. Because I have 300 files, so I have to put the name of the data files 300 times and compile 300 times. It is very time comsuming.

I want to write a shell script to do this, if possible. It will save the time and energy.

Do you guys have any idea how to do this.

Thank you very much.

Manoj
 
Old 12-05-2005, 01:33 PM   #2
mjrich
Senior Member
 
Registered: Dec 2001
Location: New Zealand
Distribution: Debian
Posts: 1,046

Rep: Reputation: 45
Sounds dangerously like homework...

Anyway, from the sounds of it you'll want something of the form
Code:
#!/bin/bash

for f in *.dat; do
     gcc -o `foobar` $f
done
You'll need to replace `foobar` with something more useful -- man cut for some hints, and use google to read up on some Bourne Shell Scripting !

Cheers.
 
Old 12-05-2005, 01:50 PM   #3
manojg
Member
 
Registered: May 2004
Posts: 78

Original Poster
Rep: Reputation: 15
No, it is not a homework.

Actually, my problem is how to put the name of the data file and output file inside the analysis code using shell script.
 
Old 12-05-2005, 02:06 PM   #4
mjrich
Senior Member
 
Registered: Dec 2001
Location: New Zealand
Distribution: Debian
Posts: 1,046

Rep: Reputation: 45
I'm still a bit unclear about what you want to do. Do you want to process your data files using a shell script, or do you want to use a shell script as a wrapper to call a pre-compiled c/c++ binary, or do you want to use a shell script as a wrapper to compile 300 different versions of a c/c++ binary ?

Cheers.
 
Old 12-05-2005, 03:33 PM   #5
manojg
Member
 
Registered: May 2004
Posts: 78

Original Poster
Rep: Reputation: 15
Ok, Here is an example of my code myCode.C:

main()
{
ifstream in("1.dat");
ifstream out("1.out");
......
..
...
return 0
}

so, to compile it, I have to put the name of the input file (here "1.dat") and output file (here "1.out"). Then I just do g++ myCode.C.

But there are 300 data files. So, I have to write input data file 300 times in the code and compile 300 times.

Like:

main()
{
ifstream in("1.dat");
ifstream out("1.out");
......
..
...
return 0
}

g++ myCode.C

main()
{
ifstream in("2.dat");
ifstream out("2.out");
......
..
...
return 0
}

g++ myCode.C

and so on.
The compilation is not a problem because simple shell script can do it by looping over 300 times. The problem is how to write the name of the input data files and output files 300 times by shell script.
 
Old 12-05-2005, 03:55 PM   #6
mjrich
Senior Member
 
Registered: Dec 2001
Location: New Zealand
Distribution: Debian
Posts: 1,046

Rep: Reputation: 45
Righto -- makes a bit more sense now.

If the format of all of the files is the same, and the c++ code for each main() is the same, then there really isn't much point in hard-coding the filenames into 300 different binaries. Just pass the input and output filenames as arguments to a single binary, eg.
Code:
main(int argc)
{
filenum=1; // input file is first argument
fp = fopen(argv[filenum],"r+");
 .
 .
 .
return 0
}
The exact syntax you'd be better off getting from a good reference book (or one of the code gurus on these forums) -- mine is a bit rough. Anyway, you would then simply run the program as ./myprogram infile.dat outfile.dat, through a shell script if you wished.

Cheers,

mj
 
Old 12-05-2005, 04:09 PM   #7
manojg
Member
 
Registered: May 2004
Posts: 78

Original Poster
Rep: Reputation: 15
Thanks a lot.
 
Old 12-13-2005, 12:52 PM   #8
manojg
Member
 
Registered: May 2004
Posts: 78

Original Poster
Rep: Reputation: 15
I have another question about the shell script so I thought to add in this title.

I have two files: "a.dat" and "b.dat". I want to compare them and if they are not equal (or equal) echo massage. The code is like this:

----------------------------
#! /bin/csh -f

set a = "a.dat"
set b = "b.dat"

if(cmp $a $b) then
echo "files are not equal"
endif
-----------------------------

In the if condition I put "cmp $a $b" as a bool condition because if the files are not equal it gives some output. But it does not work. It shows some error.

I want to add one more question.

How can I assign the output from "cmp $a $b" to a variable, like

-------------------------------------
set c = cmp $a $b // It does not work.
-------------------------------------

Any body has idea?

Thanks.

Manoj

Last edited by manojg; 12-13-2005 at 12:59 PM.
 
Old 12-13-2005, 05:30 PM   #9
mjrich
Senior Member
 
Registered: Dec 2001
Location: New Zealand
Distribution: Debian
Posts: 1,046

Rep: Reputation: 45
I'm not up on the various features of the csh shell, but under Bash it would be
Code:
#!/bin/bash

set a="a.dat"
set b="b.dat"

if [ "`cmp $a $b`" = "" ]; then
        echo Files are equal
else
        echo Files are not equal
fi
One bug/feature in the above is that it doesn't handle zero length files very well, thus you may want to add an "elif" to check for an empty file first. The forward single quotes ensure that anything between them is executed, and the results presented to the shell. You can also use this to assign the output from a command to a variable, eg.
Code:
#!/bin/bash

a="a.dat"
b="b.dat"
c=`cmp $a $b`

echo Variable c is: $c
Cheers,

Michael
 
Old 12-16-2005, 10:00 AM   #10
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Actually, cmp prints on stdout (standard output channel, in your case, your terminal) only if it finds differences. However, if you read the man pages of cmp, you should see that you shouldn't need to rely on this.
Instead, it's a better idea to rely on the return code of the cmp command, via $? or $status (or the status
command). See man <your_shell> for more info.

In Bash/sh, you would do this like:
a="a.dat"
b="b.dat"
cmp $a $b #you can ignore it's output by adding " >/dev/null 2>&1" at the end
#=> $? now holds the result for "cmp", which is typically 0 if files are identical, 1 if files are different
#and 2 if there was an error
if (( $? == 0 )); then
echo "files $a and $b are equal"
fi;

A quick look in "man csh" showed that you may be able to use if { cmp $a $b } (notice: curly braces) as well.

timmeke
 
Old 12-17-2005, 10:22 AM   #11
manojg
Member
 
Registered: May 2004
Posts: 78

Original Poster
Rep: Reputation: 15
Thanks a lot guys.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script inside shell script treotan Linux - General 4 02-19-2009 06:34 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
shell script to compile java czy11421 Programming 4 04-30-2004 09:42 AM
[SHELL SCRIPT] Write at the right of the shell window Creak Linux - General 2 04-02-2004 03:00 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration