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.

おすすめ記事