Smack yourself if you don't use generic shell script hooks
Liraz Siri - Mon, 2010/03/01 - 07:01 - 3 commentsSmack 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...
$ cat autologout # logout from the console if no activity in 300 seconds if [ -z "$DISPLAY" ]; then TMOUT=300 fi $ cat qemu QEMU_OPTIONS="-m 256" # vde switch to connect to QEMU_VDE=tap-bridged # anything you put here is used as the seed for choosing the mac address QEMU_MACADDR="" # examples: kvm qemu qemu-x86_64 QEMU_BIN=kvm function qemu() { if [ -n "$QEMU_VDE" ]; then sock=/var/run/vde.$QEMU_VDE if [ ! -d $sock ]; then echo "error: no such VDE interface ($QEMU_VDE)" return 1 fi nohup vdeq $QEMU_BIN \ -net vde,sock=$sock \ -net nic,macaddr=$(qmacaddr $QEMU_MACADDR) \ $QEMU_OPTIONS \ $@ else nohup $QEMU_BIN $QEMU_OPTIONS $@ fi } $ cat git export GIT_AUTHOR_NAME="Liraz Siri" export GIT_AUTHOR_EMAIL="liraz@sterilesecurity.com" export GIT_COMMITTER_NAME=$GIT_AUTHOR_NAME export GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL git_branch_bin=$(which git-branch 2>/dev/null) function git-branch() { local delete=no for arg; do [ "$arg" == "-D" ] && delete=yes done if [ "$delete" == "yes" ]; then $git_branch_bin -v $* else $git_branch_bin -v -a $* fi }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
Post new comment