I was updating a ksh shell script today (ksh88 since it only runs on a server running Solaris 10 at the moment) and figured I'd try some of the suggestions in the Shell Script Style Guide. (Using the current draft at http://www.opensolaris.org/os/project/shell/shellstyle/ )
In doing so, I found some things to improve - many of these are simple typo/wording nits, others are places where it could be made more clear to people like me without the extensive experience in shell internals that Roland & the other authors have, but here's my suggestions/notes/questions: - [ksh] [perf] Use builtin commands if the shell provides them Does ksh88 provide any? Is there a way to find out which your shell provides other than digging out the arc cases? - [perf] Only use external filters like grep/sed/awk/etc. if you want to process lots of data with them Should this be marked ksh93 only? Can you add a link to a page for beginners on using the ksh93 equivalent constructs? (Almost all shell man pages are way too large to easily find information like this by just doing "man ksh" and trying to guess what to search for - even saying "See the FOOBAR section of ksh93(1)" to give a easily found search string is much better.) - Be carefull with using TABS in script code, they are not portable between editors or platforms This seems to not quite match the advice in the first section about using tabs for indenting. Also, s/carefull/careful/g . - [ksh] Think about using $ set +o unset # by default Looks like this should be ksh93, not ksh, since S10 /bin/ksh says: ksh: unset: bad option(s) - Avoid using eval unless absolutely necessary s/Suble/Subtle/ - [ksh93] Use the string/array concaternation operator += s/concaternation/concatenation/ s/manual adding/manually adding/ - Set the message locale if you process output of tools which may be localised s/chanche/chance/ - Use a propper exit code s/propper/proper/ - Use functions to break up you code s/you/your/ - [ksh] [perf] Use ksh-style function Does the discussion in here need to be updated for the new function static variables typeset in the latest ksh93 update? It looks like, at least in ksh88, functions have to appear in the shell script before they're called - is this generally true? (I've written many more perl scripts than ksh, so am used to the idiom of having the main script body at the top, followed by all the subs/functions.) - test Builtin Should this mention this is for readability & consistency? - [ksh] Use "(( ... ))" for arithmetric expressions s/arithmetric/arithmetic/ - [ksh] [perf] Compare exit code using arithmetic expressions expressions s/expressions expressions/expressions/ - I/O section: temporary files Should add a rule that when running on Solaris 10 or later, temporary files should be created with mktemp(1) for protection against race conditions. - [ksh93] Print compound variables using printf "%B\n" varname s/expension/expansion/ - [ksh] [perf] Use builtin arithmetic expressions instead of external applications s/arthmetrict/arithmetic/ - [ksh93] Use printf "%a" when passing floating-point values s/rounting/rounding/ - [ksh93] Set LC_NUMERIC when using floating-point constrants s/constrants/constants/ - Put [${LINENO}] in your PS4 Is this in the shell script or your dotfiles? Looks like it needs to be specified with single quotes, such as: PS4='[${LINENO}] +' to avoid expanding the LINENO in the setting line. It also looks like there's cases where it prints $PS4 multiple times on the same line - in my ksh88 script, this produces more readable output: PS4='\ [${LINENO}] +' -- -Alan Coopersmith- alan.coopersmith at sun.com Sun Microsystems, Inc. - X Window System Engineering