Comment Re:None, I have given up bash scripting - mostly (Score 1) 411
For anything complex I tend to use a real programming language, just to avoid the script problems with strange characters in file names.
Personally I favor avoid shell scripts unless I totally control the incoming (file) name arguments scripts see. Using scripts where users provide input has proven deadly too many times as spaces are just mild annoying problems compared when ', $, ", and other fun characters shells treat as metacharacters, even within double quotes, are in file names.
for i in *.txt; do
ugly and certain bug if really sufficiently evil chars are in file names
done
My rules for safe shell follow:
- Always enclose variables in double quotes (doesn't work on some specials).
Corollary: understand the difference between "$*" and "$@". - When you can, always use options to end file names in "\0" rather than end-of-line.
Corollary: always write any real programs that accept or spit file names to support -0 options of some form. - Always allow scripts to be driven by other scripts. 100 % keyboard input is not good beyond throwaway scripts.
- Use find to get file names rather than "*" or "?" wild cards.
- KISS: if it's easier to write in another language, than use that language if at all possible. I would rather customers and users be safe than shelled.
A few straight jazz notes:
- case "$in_point" in
xyx*?) echo "You can actually use file name wild cards here";;
esac - function die () { echo 2>&1 "DIE: $*"; exit 1; } #Easy way to quit with message
- Command || die "Oops: already nominated by someone else, but let me add die"
- First && Second && echo "Chain commands to stop" || die "Any failure dies"
trap ERR "x = $?; echo 'BYE BYE'; exit $x"; #aggressively find errors & keep $?
(Not neat visual jazz, but anything that helps me find problems quicker is music to my ears, but don't use things like above "die" within trap).