You are here
Smack yourself if you don't use generic shell script hooks
Smack yourself in the forehead if you don't use the following snippet (or an equivalent) in all custom shell scripts that could benefit from a hooking mechanism:
run_scripts()
{
for script in $1/*; do
# skip non-executable snippets
[ -x "$script" ] || continue
# execute $script in the context of the current shell
. $script
done
}
run_scripts path/to/hooks.d
I've found this pattern useful in nearly every sufficiently complex shell script I maintain, and I even use it to help manage my bashrc and xsession configurations:
$ ls .bashrc.d/
paths git editor pager autologout qemu tmpdirs scratch
$ ls .xsession.d/
10-lang 10-tmpdir 90-bell 90-kbdrate 91-xscreensaver 99-fvwm-conf
Features:
-
Modular: add or remove code without having to edit a big monolithic file.
-
Order of execution is determined by the filename. Changing the order is as simple as changing a number prefix.
-
Disable execution by removing execution bit:
chmod -x .bashrc.d/git
These days breaking down configurations into separate modular files like this is common in the Linux world, so by now I expect many experienced users are wondering why I'm channeling Captain Obvious. Just keep in mind that many Linux newbies haven't yet learned all our best practices and some lessons are worth reteaching.
Why not share your own tricks? Post a comment!
Comments
Care to share your functions in .bashrc.d/ ?
paths CDPATHs?
git see [1] for mine
editor export EDITOR=vi
pager emport PAGER=less
autologout ??
qemu ??
tmpdirs ??
scratch ??
[1] My git functions:
function gvc { git commit -m "$*" }
function gam { git commit -am "$*" }
My shell cruft
It's all kind of boring and specific to my workload which is why I didn't share, but since you asked...
Oh and sorry for the late reply. I've been offline for most of these last two weeks. Too bad you didn't create an account because then you would get email notification on reply. I find that very useful myself in keeping track of the discussion (especially when it's late!).
I think Shell scripts allow
I think Shell scripts allow several commands that would be entered manually at a command line interface to be executed automatically, and without having to wait for a user to trigger each stage of the sequence. For example, in a directory with three C source code files, rather than manually running the four commands required to build the final program from them, one could instead create a C shell script, here named build and kept in the directory with them, which would compile them automatically.
curt
CA
I am smacking my forehead
I am smacking my forehead now. Good work on this. Also, glad you defined "Scratch."
I had trouble adding extra
I had trouble adding extra hooks while configuring Ssh session. I was trying to add extra stuff to alter the environment but they conflicted with some libraries and functions. I stumbled across your post recently and I am now trying to add generic hooks. So far it worked well. I changed the order and it so damn easy! Removing codes without the need to go through the whole of it and altering here and there. It is such a big blessing.
Shortcut
run-parts util, it's installed by default in all majors distributions.
-a pass all parameters to script
-- end for parameters
DIR directory with all hooks scripts
run-parts
run-parts won't run the scripts in your current shell environment and so can't be used to alter that environment.
hooks
i know this thread is quite old, but i wanted to mention.. i took this a bit further and created ${HOME}/.rc.d. in there, i have FXX-name and AXX-name, F for functions, A for aliases. loads very nicely =)
What's the rational for separating functions and aliases?
Hi Kevin, thanks for sharing. I never thought to separate shell hooks by function/alias. What's your rational for doing that?
Pages
Add new comment