Read files in shell script code and run a C program on those files

HI,

I am trying to implement a simple shell script program that does not make use of ls or find commands as they are quite expensive on very large sets of files. So, I am trying to generate the file list myself. What I am trying to do is this:

  1. Generate a file name using shell script, for example, 1.dat, 2.dat,..., so on.

  2. Call a C program within the for-loop in that shell program. The C program reads the file name in argv [ 1 ], which is defined within the program.

  3. Loop until all files have been read by the C program.

This is what I have done.

My original shell script was this:

ls -1 *.dat | while read page
do
  c_program.out $page>$page.txt
done
rename .dat.txt .dat *.dat.txt

But the above script is very slow when the number of files is really really big. SO I am now trying to do this:

for i in {1..5}
do
        var=`$i.dat`
        c_program.out $var > $i.txt
done
rename .txt .dat *.txt

But when I run the shell script, I get the following errors:

shelll_script.sh: line 3: ./1.dat: Permission denied
shelll_script.sh: line 3: ./2.dat: Permission denied
shelll_script.sh: line 3: ./3.dat: Permission denied
shelll_script.sh: line 3: ./4.dat: Permission denied
shelll_script.sh: line 3: ./5.dat: Permission denied

I am using Linux with BASH.

Your problem is var=`$i.dat` . Remove the quotes to make it work. The quotes are used to evaluate what's inside.

1 Like

Yes. It indeed works.