4 ms·
My eyes bleed. So many unquoted expansions! APP_ROOT=`dirname $0` filename=`basename $filepath .html` should read: APP_ROOT=`dirname "$0"` filename=`
by mordae 13y ago
My eyes bleed. So many unquoted expansions!
APP_ROOT=`dirname $0`
filename=`basename $filepath .html`
should read:
APP_ROOT=`dirname "$0"`
filename=`basename "$filepath" .html`
Plus if your script supports both `--version` and `--help` with proper formats, you can easily generate a manual page with `help2man`. The help output example is far from that. Also, he does not mention getopt at all.
The magic line for that is:
eval "set -- $(getopt -o hV -l help,version -- "${@}")" || exit $?
echo "${*}"
It does this:
$ ./test foo bar --help --version
--help --version -- foo bar
Which can be parsed using a `while` loop with `shift`.
For other tips on shell scripting best practices I recommend Gentoo ebuilds. They tend to be quite well written. And always read scripts from other people before running them yourself.
- 616c 13y agoI always wanted a shell best practices book (for formatting, beyond the TLDP Advanced Bash Guide, or dare I even say the POSIX spec). I will definitely checkout the Gentoo ebuild scripts. I used to look at Debian in my youth to try and find someone to imitate. However, are there are any people writing lint or prettify or Perl Critic like progs or web services that will properly wrap and do things you mention? Would there be interest in such a thing? UPDATE: I found this stuff. Does anyone use these things? https://trac.id.ethz.ch/projects/bashcritic/ https://trac.id.ethz.ch/projects/bashcritic/ http://man.he.net/man1/checkbashisms http://man.he.net/man1/checkbashisms http://stackoverflow.com/questions/3668665/is-there-a-static-analysis-tool-like-lint-or-perlcritic-for-shell-scripts http://stackoverflow.com/questions/3668665/is-there-a-static...
- 616c 13y agoAlso found very cool meet me in the middle type projects, where you can interact with Shell in different programming languages, or "transpile" to it like CoffeeScript -> Javascript. The most interesting so far: Sh.py [0] (mentioned here before [1]) and plumbum [2], which is surprisingly extensive. [0] http://amoffat.github.io/sh/ http://amoffat.github.io/sh/ [1] http://news.ycombinator.com/item?id=4531645 http://news.ycombinator.com/item?id=4531645 [2] https://github.com/tomerfiliba/plumbum https://github.com/tomerfiliba/plumbum
- dmckeon 13y agoA (cheap) trick for formatting shell scripts is approximately: wrap script into a shell function source function definition diff defined function against script with something like: { printf 'dummy (){ ' cat script printf "; };\ntype -a dummy\n" } > dummy source dummy > defined rm dummy; unset -f dummy diff script defined Issues include: comments are absent from defined function listing trailing semicolons appear after each shell command / line the first semi in the trailing "; };" may be a syntax error The default function listing format is fairly generic, so personal formatting preferences must be left as an exercise for the reader. MHO: people who insist on specific code formats should provide tools that can convert arbitrary code into their preferred format.
- INTPenis 13y agoThank you for correcting this post for all us Bash gurus out here. Also I would like to point out that you shouldn't keep using capital letters in your scripts because it might clash with environment variables. Treat them just like any other programming language, underscores or camel case for example. I'd like to say "What I learned from idling in #bash on freenode" instead. That's a much better lesson.