Take a cue from the Autotools: stick to the lowest common denominator of Bourne and POSIX shell — possibly augmented by sed
— if you have to write something that must work everywhere. There may exist systems where something breaks, but you can work around such problems by rewriting.
For example, some ancient systems have problems with expansion errors in test
, a.k.a. [
:
if [ $foo = bar ] ; then...
so the Autoconf practice is to rewrite it in double quotes with a single character prefix, like so:
if [ x"$foo" = "xbar" ] ; then...
You could also use "x$foo"
here. This guards against the possibility that $foo
could be a valid option to test(1)
, and since [
is an alias for test
, it could misinterpret the expression. The solution is to set up a situation where the unknown argument to [
always begins with x
, which means it cannot have special meaning to [
.
(Autoconf also recommends using test
instead of [
, but that advice comes about as a reaction to possible conflicts with M4, which also uses [
in its syntax.)
awk is POSIX, so theoretically it is available everywhere. It is even in Busybox, so you will have an awk
implementation even in some very restrictive embedded Linux systems. Still, I'd be less surprised to come across a system without awk
than sed
. I suppose it comes down to complexity: simpler tools are more likely to survive aggressive triage.
Perl isn't part of any widespread standard, POSIX or otherwise, so you simply cannot count on it if you know nothing in advance about the target environment. Perl is not installed by default in:
- Cygwin
- FreeBSD and NetBSD
- "minimal" installs for some Linuxes, including Slackware
- many embedded Linuxes that rely primarily on Busybox for their userland
The Autoconf manual has a chapter on portable shell programming which should be helpful to you. The last section covers tools like sed
, awk
, and many others.
sh
,awk
andsed
for example and my busybox-based NAS also has perl. – terdon Apr 04 '14 at 15:31