While 循环不循环文件内容

While 循环不循环文件内容

我有一个功能

function abc{
 a=$1
 b=$2
 if [[ $a == $b ]]then;
  return 0
 else
  return 1
 fi
}

我正在 while 循环中使用该函数

 check="mystring"
 while IFS= read -r val; do
   echo "-----------------------${val}"
   if ! abc "${val}" "${check}"; then
     echo "${val} Failure " >> $OUTPUT_LOG_FILE
   else
     echo "${val} Success " >> $OUTPUT_LOG_FILE
   fi
 done <my_list.txt

我的list.txt内容如下:

somestring
otherstring
mystring

问题是它只是循环第一个变量而不循环其他变量。

答案1

您的脚本抛出了 7 个标记的错误shellcheck,因此这不是您正在运行的脚本。每个错误代码都有详细的描述,例如:github.com/koalaman/shellcheck/wiki/SC1095

另外,其中的所有扩展都abc应该被引用。

我不清楚abc给脚本增加了什么价值。内联编写测试甚至比调用函数还要短。

$ shellcheck -s bash -
function abc{
 a=$1
 b=$2
 if [[ $a == $b ]]then;
  return 0
 else
  return 1
 fi
}
check="mystring"
 while IFS= read -r val; do
   echo "-----------------------${val}"
   if ! abc "${val}" "${check}"; then
     echo "${val} Failure " >> $OUTPUT_LOG_FILE
   else
     echo "${val} Success " >> $OUTPUT_LOG_FILE
   fi
 done <my_list.txt

In - line 1:
function abc{
            ^-- SC1095: You need a space or linefeed between the function name and body.
            ^-- SC1009: The mentioned parser error was in this brace group.


In - line 4:
 if [[ $a == $b ]]then;
 ^-- SC1049: Did you forget the 'then' for this 'if'?
 ^-- SC1073: Couldn't parse this if expression.
                  ^-- SC1010: Use semicolon or linefeed before 'then' (or quote to make it literal).

In - line 6:
 else
 ^-- SC1050: Expected 'then'.
     ^-- SC1072: Unexpected keyword/token. Fix any mentioned problems and try again.

相关内容