I have been messing about with Bash programming now and then, but there are so many ways to accomplish the same thing, most notably in the aspect of branching.
And I'm pretty new to all of that, never done much Bash programming before, or any real shell scripting.
So the most common way to approach a branch is the if statement.
But the funky thing about bash is those && and || statements or stuff.
And it is so very tempting to use those constructs to make your code more terse, even if it makes it more hard to read:
The weird thing about Bash or sh in general perhaps is that booleans do not easily evaluate to true, and any integer variable that is set, reguarly evaluates to true, and only an empty string evaluates to false
So you get ugly code like "if [ $var == false ]; then" and the like??
I don't know, I am new to it, I just sometimes spend hours inside "man bash" ;-).
It is very easy to construct your scripting in a flat level style with lots of exits:
On the other hand, personally I prefer code with pathing and single points of exit, usually:
I find it hard to do these sort of constructs in Bash.
Currently I am attempting to ensure that the proper exit states (return codes, exit codes), make it to the appropriate function or script end:
Usually currently with my expertise this often means a lot of redundant extra checks that have been made before.
And I'm pretty new to all of that, never done much Bash programming before, or any real shell scripting.
So the most common way to approach a branch is the if statement.
Code:
if [ $result -eq 0 ]; then command fi
Code:
[ $result -eq 0 ] && iftrue || iffalse
Code:
some-command && connection="yes" || { things-to-do-when-there-is-no-link } [ $connection ] || { more-things-to-do-to-set-up-a-link }
So you get ugly code like "if [ $var == false ]; then" and the like??
I don't know, I am new to it, I just sometimes spend hours inside "man bash" ;-).
It is very easy to construct your scripting in a flat level style with lots of exits:
Code:
[ condition ] || exit 0 [ condition2 ] && exit 1 more-code-that-is-meant to-be-executed as-part-of-the-main-function-body or script-body
Code:
if [ condition1 ]; then if [ condition2 ]; then result=x else result=y fi else skip=true fi
Currently I am attempting to ensure that the proper exit states (return codes, exit codes), make it to the appropriate function or script end:
Code:
( exit $result; ) # this executes the exit in a subshell causing result to become the new exit status
Comment