Re: Q re: if test of command in bash script
Ken Irving wrote:
> The [ ... ] construct isn't needed for a command like mkdir, just
> for conditional tests of variables and strings (see CONDITIONAL
> EXPRESSIONS in bash(1)).
>
Ah, beginning to coalesce in my brain now.
> I'd tend to format the above code a little differently, but here's
> the same thing with the `if' using the mkdir command directly:
>
> if [ -d $targetDir ]; then
> echo -e "YES, '$targetDir exists!\n\n"
>
Ah, the "-e" enables things like newlines and the bell character. No
wonder my bell character never sounded in another part of my script; I
just assumed it was something turned off in my terminal settings.
> if mkdir -p $targetDir; then
> echo "Created $targetDir directory successfully!"
> else
> echo "Failed to create directory. Aborting."
> exit 1
> fi
>
I at first liked the format
if test
then
statement 1
else
statement 2
fi
because I don't have to remember the semi-colon (when it's used; when
it's not, etc), and because it sort of conceptually separates the test
from the consequences of the test, but I'm beginning to swing around to
your format of
if test; then
statement 1
else
statement 2
fi
Is there any advantage/disadvantage to one format over the other?
> I sympathise with finding understandable documentation or man pages for
> these things. Regarding return codes, they're often implicit and not
> documented; often the term `exit status' and others might be used. Shell
> commands always return an exit status if 0 -- meaning true -- if the command
> doesn't fail, and usually 1 otherwise, but some other exit values are used.
> See also EXIT STATUS in bash(1).
>
>
Thanks!
--
Kent West <*)))><
http://kentwest.blogspot.com
Reply to: