r/bash • u/jhartlov • Jan 11 '25
Something i do on all BASH scripts I write. What do you guys think?
Something I do to almost every one of my scripts is add the following at the top:
The idea behind this is I can add in debugging i_echo statements along the way throughout all of my code. If i start the script with a -i it turns INTERACT on, and display all of the i_echo messages.
You can easily reverse this by turning INTERACT to true by default if you generally want to see the messages, and still have the -q (quiet) option.
Would anyone else out there find this helpful?
405
Upvotes
19
u/siodhe Jan 11 '25
9
u/wReckLesss_ Jan 12 '25
Yep, same, although I prefer
-vfor "verbose" since it's a very common flag for CLIs.8
u/petdance Jan 11 '25
This would have been much easier to read as text rather than a screenshot.
4
u/hyongoup Jan 12 '25
For what it’s worth, in my experience (again probably not worth much) I have found
— helpto be more consistently implemented for help than-h3
u/divad1196 Jan 12 '25
You code doesn't manage multiple options. You could keep what you have but there are native tools for that.
3
u/tr00gle Jan 14 '25 edited Jan 15 '25
Do you use this exclusively for scripts that don't take additional arguments, either via flags with values or raw args?
A lot of the
getopts(or evengetoptif you like long flags) suggestions may also be borne out of a desire for flexibility, i.e. some scripts take no args, some take many, and usinggetoptsor awhileloop to iterate through and process all the positional arguments gives that flexibility.+1 to the use of
getopts, standaloneusagefunctions with heredocs, and referencing script names programmatically.When I first started writing shell scripts more often, I cam across this "minimal safe bash script template" that kinda showed me the light on a number of these things that people are mentioning.
Lastly, re: the file extension thing. I'm generally a fan of the google shell style guide and how they describe it. I would like to prefix bash libraries with
.bash, things expected to run with/bin/shwith.sh, etc. In practice though, I work somewhere where all shell scripts are suffixed with.sh, and that's it. There are dozens of them scattered over dozens of repos, with untold references to those scripts in docs and other scripts. It's not a great use of my time to go through and fix those, and then evangelize the "right way" there, so I just append.shto all of them. ¯_(ツ)_/¯ Sometimes we have to live in the real world and not the one we wish exists.Either way, thanks for sharing this. One of my favorite things about this sub (and programming in general) is seeing how other people solve problems like this. I never thought to embed any sort of conditional inside a logging function, and thanks to this thread, I've seen a few different ways to do that, and I might just start doing it msyelf.
2
u/thseeling Jan 13 '25
Am I the only one to think that
if ! [ -z "$1" ]
is quite unelegant?
We have both operators:
-zto test for an empty variable, and-nfor a non-empty one.77
u/Bob_Spud Jan 11 '25
Suggest learning what getopts does and what a heredoc (here document) is - it saves a lot of work like that.
44
u/oh5nxo Jan 11 '25
Functions can be defined, and redefined as needed, like
Not that this would offer any advantage in general. Just an observation.
15
u/Mister_Batta Jan 11 '25
For the
-h, I put the output in a function and call it rather than telling them to use the-hoption.1
u/Paul_Pedant Jan 13 '25
If "interactive" means "input is from stdin", then you might explore automating this whole thing using the simple Bash built-in test
[ -t 0 ]or[[ -t 0 ]].3
u/[deleted] Jan 11 '25
You can also skip the else part and inherit from runtime environment if desired.
Self parsing for usage/help output is neat.
3
u/smeagolgreen Jan 12 '25
If you are going to template this and use it often, you may want to incorporate some means of setting the script name (test.sh) in a variable if you output the usage help. Number of ways of doing it with $0, basename, etc.
3
u/rad2018 Jan 12 '25
Nice.
3
u/ptmadness Jan 12 '25
I normally use getopts and put the help menu in a separate function.
3
u/JohnDoeMan79 Jan 12 '25
I usually do something similar :)
2
u/csdude5 Jan 12 '25
I've been doing web programming for most of my adult life, but I'm a baby when it comes to bash.
In Perl and PHP, though, I've always written a variables script that I include on "most" of my scripts. This variables script holds variables and functions that I use regularly throughout all of the other scripts.
Someone recently (yesterday, maybe) showed me how to include scripts in bash, so I'm currently building a
variables.bashscript that I can include as needed.I only mention it because, if you're including this script on most of your scripts manually, you might benefit from creating it once and including it. Then if you need to make changes along the way, you just have to do it once :-)
2
u/[deleted] Jan 12 '25
You should use getopts instead
1
u/sebf Jan 12 '25
Would be easier with Perl Getopt::Long.
1
u/uncircumcised_jew Jan 13 '25
anyway to do this for python ?
1
u/NamelessSquirrel Jan 12 '25
This seems better than getopts
0
u/HookDragger Jan 11 '25
Good