(?) The Answer Gang (!)


By James T. Dennis, tag@lists.linuxgazette.net
LinuxCare, https://www.linuxcare.com/


(?) shell script

From Peter Truong on Mon, 28 Aug 2000

Answered By: Ben Okopnik

(?) I have a directory consisting of:


test01.in   test05.in   test99.in  <-- in files
test01.out  test05.out  test99.out <-- out files

this is my code:


infile=`test[0-9][0-9]*.in`
outfile=`test[0-9][0-9]*.out`
(!) This is at least one of the reasons it doesn't work. What you seem to be trying to do here is create a list of files under the "$infile" and "$outfile" labels - but by putting the right side of the equation in backquotes, you're asking the shell to _execute_ it. That won't work; in fact, you should get an error message when you run this.
(?)
for input in $infile
do
  for output in $outfile
  a.out < $input > $output
(!) What this will do is execute "a.out" and use the current value of "$input" as the file for it to process, then output the result into the filename that is the current value of "$output" (overwriting whatever was in there originally). You didn't mention this part of the script in your explanation of what you want the script to do, but this _will_ wreck your "*.out" files. This "double" loop will also repeat the above process as many times as there are output files (if the original "list of files" equation worked) for each input file, i.e., if you have 50 "*.in/*.out" pairs, the inside loop will execute 2500 times - and the end result will be the "processed" value of the last file in the "input" list written to every file in the "output" list.
(?)
  cmp $input $output
(!) This part, of course, becomes useless after the above procedure: either "a.out" changes "$input", in which case it will always be different, or it does not change it, in which case it will always be identical.
(?)
  echo $?
done

but this however, doesn't work. what I want it to do is:

(!) All right; try this -
--------------------------------------------------------------------
#!/bin/bash
#
# "in_out" - compares all <fname>.in to <fname>.out files in the
# current directory

for n in *.in
do
  cmp $n $(basename $n in)out
done
--------------------------------------------------------------------
It's basic, but worth repeating: the "hash-bang" line comes first in any shell script and must be contiguous (no spaces). If the script requires input, write an error-checking routine and print usage instructions on an error; otherwise, as in this one, comments will help you remember what it does (you may remember it today, but not 3 years and 1,000 scripts down the road.) Next, the loop - for each "in" file, we generate the "out" filenames via "basename", by chopping off the "in" extension and tacking on an "out". Then we perform the comparison and print a message for every pair that fails: "cmp" exits silently on a "good" comparison, and prints a message on a bad one. If you want numeric output, you can use "cmp -s" (for "silent" output, i.e., it only sets the status flag) and "$?" to print the status value.
Good luck with your shell-scripting,
Ben Okopnik


Copyright © 2000, James T. Dennis
Published in the Linux Gazette Issue 57 September 2000
HTML transformation by Heather Stern of Tuxtops, Inc., https://www.tuxtops.com/


[ Answer Guy Current Index ] greetings   1   2   3   4   5   6   7 [ Index of Past Answers ]