I have a wrapper script for a CI pipeline which works great, but it always returns with 0 even though subcommands in a for loop fails. Here is an example:
#!/bin/bash
file_list=("file1 file2 file_nonexistant file3")
for file in $file_list
do
cat $file
done
>./listfiles.sh
file1 contents
file2 contents
cat: file_nonexistant: No such file or directory
file3 contents
>echo $?
>0
Since the last iteration of the loop is successfull the entire script exits with 0. What i want is for the loop to continue on fail and for the script to exit 1 if any of the loop iterations returned errors.
What i have tried so far:
set -ebut it halts the loop and exits when an iteration fails- replaced
donewithdone || exit 1- no effect - replaced
cat $filewithcat $file || continue- no effect
done || exit 1file_list=("file1 file2 file_nonexistant file3")This is an array with one element... Why is it an array at all? Check your scripts with shellcheck.