Comments (7)
Like, my boilerplate header in Plushu is
set -eo pipefail; [[ -n "$PLUSHU_TRACE" ]] && set -x
and right now I'm considering changing that, because if the rest of the file is empty and tracing isn't enabled, this will crash its caller. (And then you have a wonderful Heisenbug where the problem only manifests itself when debugging isn't on!)
from bashstyle.
If the rest of the file is empty? When would you have a script that has nothing but this? Otherwise, it seems totally reasonable to understand this behavior. I can imagine maybe a tip to remind people that this happens, but I don't think it's worth a rule against conditional expressions.
from bashstyle.
If the rest of the file is empty? When would you have a script that has nothing but this?
When I'm creating stub scripts before filling them in.
When would you have a script that has nothing but this? Otherwise, it seems totally reasonable to understand this behavior. I can imagine maybe a tip to remind people that this happens, but I don't think it's worth a rule against conditional expressions.
Okay, so in the middle of responding to a long-standing issue that's been sitting in the queue for weeks/months/years, you're refactoring a file that does this at the end:
[[ -f "$EXTRA_STUFF_FILE" ]] && cat "$EXTRA_STUFF_FILE"
printf "FINAL_VAR=%q\n" "$(finalization_code)"
To do this instead:
printf "FINAL_VAR=%q\n" "$(finalization_code)"
[[ -f "$EXTRA_STUFF_FILE" ]] && cat "$EXTRA_STUFF_FILE"
Is this specific combination of caveats (that Bash exits with the last exit code normally, and that set -e
doesn't exit with &&
ed expressions), which you last thought about four months ago, going to occur to you right now, in this exact moment where you're preoccupied with a design refactor? Or is it going to come to you later, after an hour of debugging with various env vars cleared and set, clobbering configs, turning it off and back on again, spinning it upside down and shaking it, flipping the "magic" switch to "more magic", and just generally not recognizing why the calling script is suddenly tanking out of nowhere?
(Hint: I went through this 36 hours ago. It's the second one.)
from bashstyle.
Curious about your debugging process for this ... if you had tracing on, wouldn't that lead to this line? I guess not if your outer scripts suppress the nonzero exit? I'm imagining that -f
failed causing it to exit, bubbling up to stop the command there, and if you're tracing it would show you that line and you'd realize what's going on.
from bashstyle.
Except tracing doesn't show any difference between tests inside conditions and tests outside them, so all you see is a series of -f
, -e
, -z
, and/or -n
tests, that suddenly ends in the middle of the thread of execution for no apparent reason. And then, yeah, you can go into the file, and step backwards through your logic to find the exact combination of circumstances that led to this code path being taken, and then see that this was the last command that was executed and maybe understand that this is what happened if you're already super well versed in every weird thing Bash does, including a behavior you will never otherwise see because set -e
doesn't normally allow expressions that return non-zero.
Or you could just take the ten extra keystrokes it takes to type if ; then ; fi
in the first place and never even see this problem because all your mid-script exit codes are 0.
from bashstyle.
Not to mention always using if
is, stylistically, more consistent (and isn't consistent style the name of the game here?)
from bashstyle.
Okay, I'm starting to see the light...
from bashstyle.
Related Issues (20)
- Tabs are not evil! HOT 11
- Use curly braces for all variable references HOT 6
- Use [[ -n "$whatever" ]] rather than [[ "$whatever" ]] HOT 2
- Use `command -v` instead of `which` &c HOT 5
- Locale independence HOT 1
- Throwing `set -a` and `set +a` around a variable pair list HOT 2
- Using defaults and "${VAR:?}" HOT 3
- Ownership and modes HOT 1
- Tracing and Debugging HOT 2
- Mention shellcheck HOT 3
- support for differences when considering zsh/sh compatibility? HOT 2
- Guidance on documenting functions HOT 7
- set -u HOT 1
- Example of "This can be queried/extracted with a simple function using reflection." HOT 2
- Google's Bash styleguide link is broken HOT 2
- [discuss] Suffix all bash scripts with .sh
- Article reference link appears broken.
- Add license information
- Coverage
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bashstyle.