You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
clean-up: move all shell script code to a function and use a "main" #740
This a very generic, clean-up task. Its main purpose is to document this good practice: almost all the code in a shell script should be in a function. In other words, the smallest script should look like this:
main()
{
do stuff
}
# The braces and bottom position let you safely edit the script while it's running# https://hachyderm.io/@simontatham/114511220670677410# (XKCD 303)
{
main "$@";exit"$?"
}
Rationales:
More variables can be local. This reduces the risk of accidentally overwriting some user environment variables (side effects)
Functions can be defined in any order, they don't need to be defined before being used
It provides more line numbers in the FUNCNAME stack on failure
moving code around functions does not change the indentation level which helps with git diff, rebase, cherry-pick, merge...
it's very easy to comment out the last, main "$@" line and source the entire file without running it to test individual functions interactively (almost like the usual Python's __main__ trick)
Similarly, it's very easy to copy/paste functions into an interactive shell and test them.
It's easy to temporarily turn off an entire function call for local testing purposes by commenting out a single line.
https://en.wikipedia.org/wiki/Margin_(typography)#The_Digital_Page "Although margin-less web pages do still exist, today it is generally understood that having wide enough margins to provide adequate white space around text is important to the usability and readability of digital text" (unverified)
And last but not least, stating the obvious:
A sequence of calls to well named functions is much more readable than a giant and anonymous concatenation of all these functions.
changed the title [-]clean-up: move all shell script code to a function[/-][+]clean-up: move all shell script code to a function and use a "main"[/+]on Apr 24, 2024
With a normal shell script, it's dangerous to edit the script file while an instance of the shell is still running it, because the shell will read from the modified version of the file starting at the file position it had got to in the old one, perhaps reading a partial command or the wrong command and doing something you didn't want.
But with a script in this style, the shell finishes reading the script before it does anything, so it's safe to edit.
{
main "$@";exit$?
}
The last, compound statement { } forces bash to read it entirely before running it.
Activity
scripts/run-mocks.sh: major refactor
verify-kernel-boot-log: move all the code to functions
verify-kernel-boot-log: move all the code to functions
[-]clean-up: move all shell scripts code to a function[/-][+]clean-up: move all shell script code to a function[/+]check-suspend-resume: new main() and move more code into a function
check-suspend-resume: new main() and move more code into a function
[-]clean-up: move all shell script code to a function[/-][+]clean-up: move all shell script code to a function and use a "main"[/+]run_qemu.sh: extract new set_qmp() function and delay
run_qemu.sh: extract new set_qmp() function and delay
marc-hb commentedon May 15, 2025
Simon Tatham agrees with one more point
https://hachyderm.io/@simontatham/114511220670677410
{ main "$@"; exit $? }The last, compound statement
{ }forces bash to read it entirely before running it.marc-hb commentedon May 15, 2025
https://google.github.io/styleguide/shellguide.html#function-location
run_qemu.sh: extract new set_qmp() function and delay