BASH 脚本中的模糊重定向包含 2 个函数,采用 3 个参数

Ambigous redirect in a BASH script consisting 2 functions, taking 3 parameters

我在我的 bash 脚本中发现了一个错误,我不知道如何修复它。

我有一个主脚本,在某个时候我正在调用另一个脚本。

另一个 time_diff.sh 脚本由 2 个函数组成,它们看起来像这样:

#!/bin/bash
function timeDiff() {
    local time1="$(head -n 1 "")"
    local time2="$(head -n 1 "")"
    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   >> 
    fi
}

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

recuDiff  

这个脚本从主脚本调用是这样的:

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

直到 timeDiff() 函数转到 if-elif 语句的部分,脚本什么都不做,打印出一切正常 - 一切正常。

今天我发现当 if-elif 语句转到最后一部分时,它应该 运行 文件上的差异并将其保存到文件中,我得到:

time_diff.sh: line 34: : ambiguous redirect

第 34 行:

diff   >> 

并且从未创建该文件。

有什么问题吗?我应该 touch DIFF_TIME.TXT 在 运行 宁 time_diff.sh 脚本之前吗?

还有一件事 - 很难重现这个错误,运行 完成整个 Jenkins 工作需要一个小时,而且我绝对不能保证这个脚本正在测试的其中一个东西会运行 比它应该的要长,所以我还没有尝试任何解决方案。

看来问题只是</code>没有定义</p> <p>观察:</p> <pre><code>echo foo >> "" bash: : ambiguous redirect

请注意,位置参数 (</code> ... <code>) 不共享或继承。每个 shell 函数都有自己的一组位置参数。

您使用三个参数调用了脚本。但是你只用两个参数调用了函数。这就是为什么 </code> 在函数内部未定义的原因。</p> <p>观察:</p> <pre><code>$ cat foo.sh echo "" "" "" func() { echo "" "" "" } func "" "" $ bash foo.sh a b c a b c a b

同样作为一般规则:在所有变量引用两边加上引号。

在 shell 函数中,</code>、<code> 等指的是传递给函数的参数,而不是传递给脚本的参数。在你的情况下,你正在调用

timeDiff {,}/"$each"

将两个参数传递给 timeDiff (在参数中可能有空格的情况之外 - 您可能需要重新考虑一些引用位),但 timeDiff() 指的是 ,这将是未定义的。