by ohiovr · 4y ago · 0 comments
If you are wondering what in the world I'm talking about and I've missed your question entirely, sorry about that. My feet are tired.
This is the expectation, and the article is about why it's not that simple.
On the one hand it is nice to have your interactive interface and your scripts be the same language. on the other it is a bit horrifying the way all those convenient interactive features make your programs so prone to failure.
I wouldn't mind it if all scripting in the shell had to be offloaded to a completely separate language that was less error prone and more consistent.
Seems kind of logical, the following c code behaves the same way.
#include <stdio.h>
int test(void) {
int i=0;
return i++;
}
void main(void) {
printf("i = %d\n", test());
}
'let i++' evaluates to / returns 0 before increasing i, just like c would. if cond; then x; else y; fi
does not do the same thing as if ! cond; then y; else x; fi
despite the the absence of any operator overloading. $ if false; then :; else (if [ $? -eq 0 ]; then echo msg; fi); fi
$ if ! false; then (if [ $? -eq 0 ]; then echo msg; fi); else :; fi
msgOf course
cond = false
if cond; then x; else (if cond; then echo msg); fi
does not do the same thing as cond = true
if cond; then (if cond; then echo msg); else x; fi
Bash is not somehow strange in this regard.You should do
if ARG=false
Then use $ARG not $? in the if statement.
false the command returns 1, ! the command turns a 0 return code into a 1 and anything other than a zero into a 0, if is vaguely backwards from most languages in that it deals in return codes a rc of 0 is true and anything else is false
in the first case false has a rc of 1 this will execute the else list then the test command 1 is equal to 0(rc = 1 false list) so "msg" does not print
in the second case false(rc 1), !(rc 0), if(true list) now you check if rc is 0, it is and "msg" is printed.
/joke