-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add check for set e and operator #2237
base: master
Are you sure you want to change the base?
Conversation
Part of what it does is to forego exiting when a failing command is used as a condition, such as with
Is the expectation that the user will rewrite AND-lists into |
Thanks for the response! Agreed that
I could think of a couple remediations given this warning:
If we wanted to make this more user friendly, we could not warn if the line ends with I'm not particularly tied to any specific implementation - just feel that there is an opportunity here to help users avoid a footgun. However we go about that is 👍 |
If the
then suggest rewriting it via Obviously this increases the complexity a bit and is not fair to ask of this small PR, but let me know if you'd still like to give it a whack. All the information can be deduced from walking the list of parent elements ( |
I'm on vacation, might as well mess around with haskell. I'll take a stab at it over the next week and see what happens. |
This check would be quite useful, the more I thought about it the more confused I got, so I tested out the whole truth table of common logical operators: evaluate_boolean() {
local expression="$1"
local output
output=$(bash -c "set -e; $expression; echo end")
local rc
eval "$expression"
rc=$?
if [[ "$output" == "end" ]]; then
echo "$expression returns ${rc} and continues"
else
echo "$expression returns ${rc} and exits"
fi
}
while read -r expression; do
evaluate_boolean "$expression"
done <<EOF
false && true
false || true
true && false
true || false
true && true
true || true
false && false
false || false
! true
! false
true
false
EOF false && true returns 1 and continues
false || true returns 0 and continues
true && false returns 1 and exits
true || false returns 0 and continues
true && true returns 0 and continues
true || true returns 0 and continues
false && false returns 1 and continues
false || false returns 1 and exits
! true returns 1 and continues
! false returns 0 and continues
true returns 0 and continues
false returns 1 and exits With My highlight is These results are very confusing so I'd love my script to have some bug I've missed, but I've tested the most egregious results in a plain shell and could reproduce them. |
The documentation for |
Add a new rule to check for
&&
usage withset -e
set.Recently I got burned by https://unix.stackexchange.com/questions/312631/bash-script-with-set-e-doesnt-stop-on-command/318412#318412
I'm pretty new to Haskell so I figured I'd start small. In the future, we could expand this to include
if
,while
,||
, etc.While this rule is very broad-reaching, I believe it to be helpful.
Looking for advice on:
info
on?For some backstory on why this is useful:
I had a script like:
This actually prints
command exited 1
, while I'd expect it to not even make it to theecho
.The following script does not make it to the echo:
To me this was extremely unintuitive and I wish shellcheck would have warned me.