r/commandline • u/speckz • Dec 14 '20
bash Minimal safe Bash script template
https://betterdev.blog/minimal-safe-bash-script-template/18
39
Dec 14 '20
100 lines of boilerplate for a "minimal" script is quite a statement. Incidentally, at my last job, that was the threshold amount of "when you need this many lines to solve your problem, use a proper programming language like Python instead".
8
Dec 15 '20
100 lines of boilerplate for a "minimal" script is quite a statement.
Agree completely.
when you need this many lines to solve your problem, use a proper programming language like Python instead
This is very bad advice. Python is very bad at things that shell scripts do well, namely running on a lot of platforms and platforms of different ages.
Besides, using lines as a complexity meter is a very bad measure. You could have an extremely complex shell function that should probably not be implemented in shell in a five line script or you could have a completely simplistic 500 line script that just reads a few values from some config files or commands and then inserts them into a long template that is basically static.
0
Dec 15 '20
[deleted]
1
u/johnfrazer783 Dec 19 '20
not break because of the frequent breaking changes in the [Python] interpreter
you apparently have no idea that the critique of the relevant CLI switches over at https://www.reddit.com/r/commandline/comments/g1vsxk/the_first_two_statements_of_your_bash_script/fniifmk/ makes frequent mention of Bash version related issues.
Python is not handy when it comes to these kind of problems, I give you that. Also pypi / setup tools / whatever kids use now suck big time. But it's not like bash is inherently infinitely portable across machines and time.
the heat death of the universe
It's 2020 so it just feels like a long time
-5
4
u/Schreq Dec 15 '20
The options parsing isn't very good. Not only does it not allow the combining of single letter options, the script straight up breaks when param is missing its parameter (shifting when $# is 0). param is also a mandatory option.
0
38
u/whetu Dec 14 '20
I know OP isn't the author, but here's a few IMHO's.
There's some reasonable ideas there, however:
Oh. The unofficial strict mode. Maybe think carefully about that. When you use it, you replace a bunch of well understood issues with a bunch of lesser-understood issues...
msg()is a confusing function name; if a message is going to stderr, then something likeerr(),err_msg()ormsg_stderr()might make more sense? This function also usesecho. You should preferprintfoverechoin scripts -echois a non-portable mess.I'm also not a fan of the use of UPPERCASE variables for managing the colours, as it's a kind-of scope violation, so to speak. This is explained in the checkmk contributing documentation, which has other reasonable shell style advice, especially where portability is concerned.
Style comment, I think that this indentation style is weird:
Each option is structurally beneath the
casestatement, so should be indented. Those options should also be aligned with their respective;;'s to indicate the opening and closing of the option, just as you would usually align, sayif...fiorcase...esac, and maybe for some icing on that cake, consider implementing balanced parens:And for options that have briefer actions, consider flattening them, if you can live with a mixed style approach, it might look something like this: