I see a couple of compatibility problems with the script. There are at least a couple of bash/ksh-specific bits in it.
Remember that if you use #!/bin/sh
as the shebang, the script is processed in posix-compatible mode. If your sh-parser is actually bash
, then bash-only features are still valid, as long as they don't conflict with posix specifications. But if you use a different shell, like dash
, then these lines will generate errors.
Specifically, I noticed the substitution parameter substitution here:
This variation isn't available in posix. You have to run it through tr or sed or something instead.
Second are the comparison tests like this:
if [ "$1" == "-h" ]
Again, "==" is not valid in the posix-based test. You have to use "=".
Alternately, switch the shebang to #!/bin/bash
and make it bash-specific. In which case you should take advantage of all its more advanced features like the extended "[[
" test keyword.
See here for what to avoid when aiming for posix compatibility:
BTW, since functions run in the current environment, there should never be any need to export anything just for that purpose. But of course it's necessary if any of the external processes executed by the function (or the script) need those values in their
Finally, you need to make sure that all
of your variable expansions are properly double-quoted. Those unquoted $D
variables will result in broken commands if any string stored in them happens to include whitespace or globbing characters.
I also recommend adding tests to ensure that the directories and files actually exist (and are readable/writable, etc.) before use in any of the commands that depend on them.