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 
5
  • 3
    Can you cut down your script the minimum required to reproduce the problem? Also, I'd recommend running your script through shellcheck.net, as there are some common errors that the tool will catch. Commented Feb 16, 2015 at 23:20
  • also try using bash -x yourscript.sh to do some debugging. It will help to show variables. And see: stackoverflow.com/questions/17804007/… Commented Feb 16, 2015 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 Commented Feb 16, 2015 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? Commented Feb 17, 2015 at 3:15
  • Which files and directories *.out *.std are present in the current directory? Commented Aug 18, 2015 at 8:36

1 Answer 1

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 } 
Sign up to request clarification or add additional context in comments.

2 Comments

i've also tested a solution w/o the "set -- " idiom; it is cleaner.
Lots of problems here: parsing ls, lacks of quotes, use of expr. Needless to say, code is definitely broken.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.