0

I found a bug in my bash script, and I have no idea how to fix it.

I have a main script in which at a certain point I'm calling the other script.

The other time_diff.sh script consists of 2 functions, and they look like this:

#!/bin/bash
function timeDiff() {
    local time1="$(head -n 1 "$1")"
    local time2="$(head -n 1 "$2")"
    pc=$(( $time2 - $time1 ))
    timediff=${pc#-}

    pc=$(perl $BVTDIR/engine/math.pl $pc $time1)
    pc=${pc#-}

    echo "Prozentdiffertenz = "$pc"%"
    echo "Laufzeitdiffertenz = "$timediff" milisekunden"

    if (( "$timediff" < "$MAX_TIME_DIFF")) && (( "$pc" < "$MAX_PERC_DIFF" )); then

# the script is doing nothing, just echoing that everything is fine

     elif (( "$timediff" > "$MAX_TIME_DIFF")) && (( "$pc" < "$MAX_PERC_DIFF" )); then

# the script is doing nothing, just echoing that everything is fine

     elif (( "$timediff" < "$MAX_TIME_DIFF")) && (( "$pc" > "$MAX_PERC_DIFF" )); then

# the script is doing nothing, just echoing that everything is fine

     elif (( "$timediff" > "$MAX_TIME_DIFF")) && (( "$pc" > "$MAX_PERC_DIFF" )); then

# the script is runing a diff and saving the output.

          diff $1 $2 >> $3
    fi
}

function recuDiff {
  find $1 -maxdepth 1 -mindepth 1 -type f -name \*.TIME.TXT -printf '%P\n' |
  while read each
  do
    if [ -e $2/"$each" ]
    then
      timeDiff {$1,$2}/"$each"
    fi
  done
}

recuDiff $1 $2

This script is called from the main script like this:

/bin/bash time_diff.sh $CURRENT_BUILD_DIR $PREVIOUS_BUILD_DIR $DIFF_DIR/DIFF_RUNTIMES.TXT

Until the timeDiff() function went to the parts of if-elif statements where the script was just doing nothing, printing out that everything is fine - everything WAS fine.

Today Ive found out that when the if-elif statement goes to the last section, where it should run the diff on the files and save it to the file, I get:

time_diff.sh: line 34: $3: ambiguous redirect

Line 34:

diff $1 $2 >> $3

And the file is never created.

What could be wrong? Should I touch DIFF_TIME.TXT before running the time_diff.sh script?

One more thing - it is VERY hard to reproduce this bug, it takes an hour to run throuhg the whole Jenkins job, and I have ABSOLUTELY no guarantee that one of the things this script is testing will run longer than it should, so I havent tried any solution yet.

2 Answers 2

3

It seems that the problem is simply that $3 is not defined.

Observe:

echo foo >> "$3"
bash: $3: ambiguous redirect

Note that positional parameters ($1 ... $9) are not shared or inherited. Each shell function has its own set of positional parameters.

You called your script with three arguments. But you called the function with only two arguments. That is why $3 is undefined inside the function.

Observe:

$ cat foo.sh 
echo "$1" "$2" "$3"
func() {
  echo "$1" "$2" "$3"
}
func "$1" "$2"

$ bash foo.sh a b c
a b c
a b

Also as a general rule: put quotes around all variable references.

3
  • That gives an understanding, but.../bin/bash time_diff.sh $CURRENT_BUILD_DIR $PREVIOUS_BUILD_DIR $DIFF_DIR/DIFF_RUNTIMES.TXT means that the $3 was initialitzed. I am calling the script with 3 arguments...
    – hc0re
    Feb 6, 2015 at 15:51
  • @dziki Yes, you are calling the script with 3 arguments. However, you are calling the timeDiff function with only two.
    – twalberg
    Feb 6, 2015 at 17:38
  • @dziki I overlooked to explain that part in my answer. please see updated answer.
    – Lesmana
    Feb 6, 2015 at 19:01
1

Within a shell function, $1, $2, etc. refer to the arguments passed to the function, not the ones passed to the script. In your case, you are calling

timeDiff {$1,$2}/"$each"

which passes two arguments to timeDiff (outside the case where you might have spaces in the arguments - you might need to reconsider some of your quoting bits), yet timeDiff() is referring to $3, which will be undefined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.