LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   shell script to compile (https://www.linuxquestions.org/questions/linux-newbie-8/shell-script-to-compile-389507/)

manojg 12-05-2005 12:54 PM

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

mjrich 12-05-2005 01:33 PM

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.

manojg 12-05-2005 01:50 PM

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.

mjrich 12-05-2005 02:06 PM

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.

manojg 12-05-2005 03:33 PM

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.

mjrich 12-05-2005 03:55 PM

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

manojg 12-05-2005 04:09 PM

Thanks a lot.

manojg 12-13-2005 12:52 PM

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

mjrich 12-13-2005 05:30 PM

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

timmeke 12-16-2005 10:00 AM

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

manojg 12-17-2005 10:22 AM

Thanks a lot guys.


All times are GMT -5. The time now is 10:51 PM.