I am writing bash script to check the fstab structure.
In a for loop, I have placed a return statement to use the exit code later in the script, but it seems that the return statement is breaking the loop after printing the first requested output
How can I assign a return code of 1 without breaking the loop so I will get all the results and not just the first?
for i in $(printf "$child"|awk '/'$new_mounts'/'); do
chid_count=$(printf "$child"|awk '/'$new_mounts'/'|wc -l)
if [[ $chid_count -ge 1 ]]; then
echo -e "\e[96mfstab order check failed: there are child mounts before parent mount:\e[0m"
echo -e "\e[31mError: \e[0m "$child"\e[31m mount point, comes before \e[0m $mounts \e[31m on fstab\e[0m"
return 1
else
return 0
fi
done
'/'$new_mounts'/'is very, very wrong -- it string-splits the contents ofnew_mountsrather than passing it to awk as part of a single argument."/$new_mounts/"would be an improvement. Consider passing your code through shellcheck.net, and reading mywiki.wooledge.org/Quotes closely.echo -edoes anything other than print-eon its output is out of compliance with the POSIX sh standard -- see the POSIX spec for echo at pubs.opengroup.org/onlinepubs/009604599/utilities/echo.html. Consider using printf instead, which allows a standards-compliant, portable tool for comparable tasks.for i in $(...)is actually not the best tool for the job when your goal is to iterate over lines of output. See mywiki.wooledge.org/DontReadLinesWithFor, and/or mywiki.wooledge.org/BashFAQ/001 (for something with a focus on the best-practice approach).