1

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
5
  • 1
    ...btw, '/'$new_mounts'/' is very, very wrong -- it string-splits the contents of new_mounts rather 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. Commented Mar 7, 2016 at 18:19
  • ...similarly, any shell where echo -e does anything other than print -e on 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. Commented Mar 7, 2016 at 18:20
  • 1
    Tempted to flag as duplicate of stackoverflow.com/questions/35533501/… but I'll just repeat what I wrote in my answer there: use Awk. Commented Mar 7, 2016 at 18:25
  • @tripleee, ...that is, indeed, a very on-point answer for the larger problem (as opposed to the immediate language-level question). Commented Mar 7, 2016 at 22:16
  • @Max_il, ...btw, also, 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). Commented Mar 7, 2016 at 22:18

1 Answer 1

1

If you read the documentation for the language, immediately returning is what return is supposed to do. That's not unique to shell -- I actually can't think of a single language with a return construct where it doesn't behave this way.

If you want to set a value to be used as a return value later, use a variable:

yourfunc() {
  local retval=0

  for i in ...; do
    (( child_count >= 1 )) && retval=1
  done

  return "$retval"
}
Sign up to request clarification or add additional context in comments.

2 Comments

Haskell's return doesn't :)
@chepner, Haskell guarantees order of operation between statements at all? That's not as purely functional as I'd expect. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.