I have the following script:
#!/bin/bash
pstdout="PASS"
fstdout="FAIL"
error_string_check="ERROR"
stdout_result(){
result="$1"
shift
des="$1"
shift
reason="$1"
shift
arr_error_context=("$@")
echo -e "[$result] $des"
echo " \`$reason"
for i in "${arr_error_context[@]}"; do
echo " - $i"
done
}
main(){
for i in $(seq 1 3); do
for j in $(seq 1 10); do
echo "Entry $j, ERROR somethings broken." >> "testfile$i"
done
for k in $(seq 11 20); do
echo "Entry $k, INFO everythings good." >> "testfile$i"
done
done
found_files="$(find ./ | grep testfile)"
while IFS= read -r file_to_check; do
found_error_entry="$(cat $file_to_check | grep "$error_string_check")"
arr_errors_found=()
if [ -n "$found_error_entry" ]; then
arr_errors_found+=("$found_error_entry")
fi
if [ ${#arr_errors_found[@]} -eq 0 ]; then
stdout_result "$pstdout" "check for no \"$error_string_check\"in $file_to_check of all time."
else
stdout_result "$fstdout" "check for no \"$error_string_check\" in $file_to_check of all time." "error\(s\) were found:" "${arr_errors_found[@]}"
fi
done <<< "$found_files"
}
main
rm -f ./testfile1
rm -f ./testfile2
rm -f ./testfile3
However my output is this
[FAIL] check for no "ERROR" in ./testfile1 of all time.
`1 error\(s\) were found:
- Entry 1, ERROR somethings broken.
Entry 2, ERROR somethings broken.
Entry 3, ERROR somethings broken.
Entry 4, ERROR somethings broken.
Entry 5, ERROR somethings broken.
Entry 6, ERROR somethings broken.
Entry 7, ERROR somethings broken.
Entry 8, ERROR somethings broken.
Entry 9, ERROR somethings broken.
Entry 10, ERROR somethings broken.
[FAIL] check for no "ERROR" in ./testfile3 of all time.
`1 error\(s\) were found:
- Entry 1, ERROR somethings broken.
Entry 2, ERROR somethings broken.
Entry 3, ERROR somethings broken.
Entry 4, ERROR somethings broken.
Entry 5, ERROR somethings broken.
Entry 6, ERROR somethings broken.
Entry 7, ERROR somethings broken.
Entry 8, ERROR somethings broken.
Entry 9, ERROR somethings broken.
Entry 10, ERROR somethings broken.
[FAIL] check for no "ERROR" in ./testfile2 of all time.
`1 error\(s\) were found:
- Entry 1, ERROR somethings broken.
Entry 2, ERROR somethings broken.
Entry 3, ERROR somethings broken.
Entry 4, ERROR somethings broken.
Entry 5, ERROR somethings broken.
Entry 6, ERROR somethings broken.
Entry 7, ERROR somethings broken.
Entry 8, ERROR somethings broken.
Entry 9, ERROR somethings broken.
Entry 10, ERROR somethings broken.
As you can see the array does not keep its indexes. Shows only "1" error when there should be "10". I assume this is happening based on some subshell issue but I am not sure. I already tried to do what this post suggested, but it does not work.
Why does bash refuse to keep the indexes in the array? I need the number of errors printed, and every error printed with -
appended to it. How can I accomplish this?