0

Alright, so this is my first bash program, and I'm really lost on this one. the summary test takes 0-2 args and returns 'same' or 'different' depending on the cmp exit status.

What I was to do is put all .outs into one variable and all .stds inside another. then I want to iterating the two variables for occurrences:

if I have a .out but not a .std by the same basename, I want to print 'missing file.std'

If I have all correspond file.out and file.std files contain the same contents I return 'all cases passed'

if cmp fails I count the number of times it fails (that is the number of times file.out and file.std are different)

If there is 1 argument $1 holds all .out and .std files

if there is 2 args $1 holds .out and $2 holds .std

otherwise we use current directory

The problem I'm having is my outputs are weird. my 0 arg outputs are like ./file different and my 1 arg outputs are file.out./* and my arg 2 says missing file.out/*.std Does anyone know what's going on?

T=*.out
S=*.std
if [[ $# = 1 ]]; then
   T=$1
   S=$1
fi
if [[ $# = 2 ]]; then
   T=$1
   S=$2
fi

N=0
fin=0
for i in $T/*; do
 count=0
 for j in $S/*; do
     if [[ ${i%.out} = ${j%.std} ]]; then
        if cmp -s $i $j; then
             echo ${i%.out} same
        else
             let N=$N+1
             echo ${i%.out} different
        fi
     else
         let count=$count+1
         if [[ $count=${#y} ]]; then
               let fin=$count
               F=${i%.out}.std
         fi
      fi
   done
done

if [[ $fin = 0 ]]; then
   if [[ N = 0 ]]; then
      echo all tests succeeded
   else
      echo $N tests failed
   fi
else
   echo missing file: $F
fi
user3466773
  • 176
  • 1
  • 2
  • 14
  • 3
    Can you cut down your script the minimum required to reproduce the problem? Also, I'd recommend running your script through http://www.shellcheck.net/, as there are some common errors that the tool will catch. – Tom Fenech Feb 16 '15 at 23:20
  • also try using `bash -x yourscript.sh` to do some debugging. It will help to show variables. And see: http://stackoverflow.com/questions/17804007/how-to-show-line-number-when-executing-bash-script – jmunsch Feb 16 '15 at 23:23
  • I don't really know how to cut down on this since this class is basically for 0 experience in shell programming, but thank you so much for letting me know about the shellcheck and also the debugger, that will really help me understand later programs – user3466773 Feb 16 '15 at 23:52
  • since this is _really_ a data-processing problem, and _not_ an admin problem, would you consider using an awk script? for starters: ls *.out *.std | awk '...' which uses associative arrays, which is what your problem needs. have you/ would you consider awk? – Marty McGowan Feb 17 '15 at 03:15
  • Which files and directories `*.out *.std` are present in the current directory? – Armali Aug 18 '15 at 08:36

1 Answers1

0
# copy to file: sameOrDiff, chmod +x sameOrDiff
# run:  $ sameOrDiff out std
# this will get you started, modify for your 
# precise output requirements
x=$1; y=$2
ls *.$x *.$y |
sed 's/\.[^\.]*$//' | sort -u | {
    nc=0
    while read a;
    do
        f=$a.$x
        g=$a.$y
        # echo $a $f $g
        set -- $(ls $f $g 2> /dev/null )
        # echo $# $*
        [[ $# -eq 2 ]] && {

                cmp $@ >/dev/null || { nc=$(expr $nc + 1); }

            } || {

                [[ -f $f ]] && { echo missing $g; }
                [[ -f $g ]] && { echo missing $f; }
            }
    done
    [[ $nc -lt 1 ]] && echo all cases passed
    echo NC $nc
}
Marty McGowan
  • 356
  • 2
  • 10