On Sat, 2021-03-20 at 07:09 -0400, Scott Andrews wrote: > On Fri, 19 Mar 2021 23:26:01 +0100 > Pierre Labastie <pierre.labas...@neuf.fr> wrote: > > > On Fri, 2021-03-19 at 07:24 -0400, Scott Andrews wrote: > > > On Fri, 19 Mar 2021 07:09:06 +0100 > > > Pierre Labastie <pierre.labas...@neuf.fr> wrote: > > > > > > > > > > > Using brackets depends on "something". If something is a > > > > predicate, you need brackets. If something is a (compound) > > > > command, no brackets. I've not looked in details at the "if" in > > > > the boot scripts, but they must be correct in this respect, > > > > since they seem to work... > > > > > > > > > > By brackets are you talking about [ ] or [[ ]]? > > > > > > if [ something ]; then this; fi, > > > > > > Really means > > > > > > if test something; then this; fi > > > > > > [ is actually test, and the [ must be followed by ] > > > > > > [[ ]] is an expression > > > > > > See the gnu bash reference manual for clarification. > > > > > > > Here is an experiment: > > --- > > pierre [ ~ ]$ toto () { > > > return 1 > > > } > > pierre [ ~ ]$ if toto; then echo true; else echo false; fi > > false > > pierre [ ~ ]$ if [ toto ]; then echo true; else echo false; fi > > true > > --- > > > > Pierre > > > Here is a more complete test: > > > toto () { > return 1 > } > gogo () { > return 0 > } > > echo "toto test" > > if toto; then echo true; else echo false; fi > > if [ toto ]; then echo true; else echo false; fi > > if test toto; then echo true; else echo false; fi > > if [[ toto ]]; then echo true; else echo false; fi > > echo "gogo test" > > if gogo; then echo true; else echo false; fi > > if [ gogo ]; then echo true; else echo false; fi > > if test gogo; then echo true; else echo false; fi > > if [[ gogo ]]; then echo true; else echo false; fi > > Results: > toto test > false > true > true > true > gogo test > true > true > true > true > > Here is what the gnu bash manual states: > > test > [ > test expr > Evaluate a conditional expression expr and return a status of 0 > (true) or 1 (false). Each operator and operand must be a separate > argument. Expressions are composed of the primaries described below > in Section 6.4 [Bash Conditional Expressions], page 92. test does not > accept any options, nor does it accept and ignore an argument of -- > as signifying the end of options. When the [ form is used, the last > argument to the command must be a ].
Conclusion: test/[ must be used with a conditional expression (what I called a "predicate", which is not the right word, sorry about that). To add to the test, try with an undefined variable/function: if [ undefined ]; then echo true; else echo false; fi (returns true: reason, a non empty string is always true as a conditional expression) if undefined; then echo true; else echo false; fi (returns an error "bash: undefined: command not found". Could be anticipated) and with an empty variable: empty= if [ $empty ]; then echo true; else echo false; fi (returns false: an empty string is false as a conditional) if $empty; then echo true; else echo false; fi (returns true! the empty command (typing just enter) has exit status equal to 0) but: if ; then echo true; else echo false; fi (returns an error "bash: syntax error near unexpected token `;') and just typing: ""<enter> returns "bash: : command not found" Anyway, I hope you are now convinced that the right command for testing script status is: if ! check_script_status; then continue; fi Pierre -- http://lists.linuxfromscratch.org/listinfo/lfs-support FAQ: http://www.linuxfromscratch.org/blfs/faq.html Unsubscribe: See the above information page Do not top post on this list. A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing? A: Top-posting. Q: What is the most annoying thing in e-mail? http://en.wikipedia.org/wiki/Posting_style