3 ms·
The bash example should have set -e or #!/bin/bash -e so that it will exit on error. That or the rest of the languages also shouldn't bother to check for error
by icefox 11y ago
The bash example should have set -e or #!/bin/bash -e so that it will exit on error. That or the rest of the languages also shouldn't bother to check for errors. Also rather than invoking another shell use let and do let STATE=1-$STATE just like all the other programs. And why does bash need to have comments. Maybe it was a subtle joke that C doesn't get comments, but bash does :D in which case well played.
#!/bin/bash -e
STATE=1
while : do
echo $STATE > /sys/class/leds/beaglebone\:green\:usr0/brightness
let STATE=1-$STATE
sleep 2
done
While all of the programs can be made smaller in size the bash script is probably the default way that the script would be tossed together. Bash has a ton of problems, but sadly between it and the standard unix commands you can get the job done too quickly and easily.
- pdkl95 11y ago> invoking another shell That wasn't a subshell - $(( )) is Arithmetic Expansion. (you may be thinking of $( ) for Command Substitution) $ echo $BASH_SUBSHELL $((BASH_SUBSHELL)) $(echo $BASH_SUBSHELL) 0 0 1 Now, using the result of a boolean is a bit strange; I would have used your math with the modern expansion style: # the new style allows spaces STATE=$(( 1 - $STATE ))
- icefox 11y agoAh, thats great learned a new bash trick
- barrkel 11y agoShorthand for $(( )) is $[ ] In predicate contexts (which means return code in bash) like if, while or the left hand side of '&&', consider using (( )) - this has an exit code of 0 if the arithmetic expression is true (non-zero) and 1 otherwise. Use it like this: let x=3*3 if (( x > 5 )); then echo greater than five else echo less than or equal to five end