Redirecting help bash unix

Advertisements

I am trying to make a bash script which loops through the current working directory and runs a program for each testfile. It reads from the testfile, processes the data in the program, writes to new files, and then compares the results to what the actual result is supposed to be.

I wanted it to be laid out in a specific way. The problem is, I’m new to bash so I’m not really sure how to do the syntax for these redirections correctly.

for filename in ${FILES} ; do
  9         if [[ $filename == test* ]];
 10         then
 11                 echo "File starting: ${filename}"
 12                 #count
 13                 count < $filename
 14                 count > newOut
 15                 count 2> newErr
 16                 count >> newOut
 17                 exCount < $filename
 18                 exCount > exOut
 19                 exCount 2> exErr
 20                 exCount >> exOut
 21                 diff1=$(diff myOut exOut)
 22                 diff2=$(diff myErr exErr)
 23                 echo "Test File ${filename}"
 24                 echo "stdout diffs:"
 25                 echo $diff1
 26                 echo "stder diffs:"
 27                 echo $diff2
 28         fi

This is supposed to put the file inputs to the program count, then redirect stdout to newOut, then redirect stderr to newErr, then finally append the return code to newOut. Once that is done, the same thing is done to the other program, and finally I compare the two differences.

However the only thing that actually works is the looping and the first redirection, which correctly puts $filename inputs into count. I’m not sure what I’m doing wrong. I think it might have something to do with the fact that filename is only referenced in the first redirection, but other than that no clue. Can anyone help? Thanks!

>Solution :

From your description you want to invoke count once. You are invoking count 4 times. You need to combine the first 3 into one invocation.

The final count invocation (count >> newOut) should be removed; you already wrote the output of count into newOut, and you don’t want to append it again, right? What you want to do is capture the return status of the most recent command with the special parameter $?.

count < $filename > newOut 2> newErr
echo $? >> newOut

Do the same thing for exCount.

Leave a ReplyCancel reply