Hacker News new | ask | show | jobs
by spelufo 1039 days ago
I like this dispatch trick:

build() { ... }

run() { ... }

cmd="$1"

shift

$cmd "$@"

1 comments

"$@" would have worked just as well there, instead of the final three lines: you pulled cmd off the front just to... put it back. (I wouldn't recommend doing either of these, though, as it is weird that random other commands are valid there, such as "ls"; you should use a case statement.)
Yes, I think I used to check if `$cmd` was in a space separated list of valid commands, but I don't anymore because I only every use this to quickly get some commands down into a project's directory so as not to forget them.

As soon as any complexity or validation is needed I move to python, which is usually a better fit, and arguably even more portable.

I suppose it's safer that way because it will only execute those specific functions, and wouldn't, for example, execute a statement like "rm -rf /”