[Initng] for ALL developers of shell scripts!

Eric MSP Veith eveith at wwweb-library.net
Mon Feb 5 17:05:47 CET 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello,

nice memo. :-)

However, doesn't [ "$foo" = "$bar" ] invoke the "test" program, which 
perfectly understands those expressions?

	Eric

- ------ Original Message ------
Sender: Denis Knauf <denis.knauf at gmail.com>
Recipient: initng at initng.org
Date: Monday 05 February 2007 16:49
Subject: [Initng] for ALL developers of shell scripts!

> hello,
>
> i see, many scripts aren't posix-compatible. you should test all your
> scripts in dash. dash is a full posix-compatible-shell and doesn't know any
> extensions.
> some known mistakes in shell scripts:
> # [[ $a = $b ]]
> 	this is bash, not posix. in posix:
> 	# [ "X$a" = "X$b" ]
> 	this 2 X are because $a can include a "-" as first char.
> 	this is a problem in bash too, but nobody thinks about it.
>
> # [ expresion1 ] && [ expression2 ]
> 	faster:
> 	# [ expression1 -a expression2 ]
> 	you can use ( ) too. example:
> 	# [ expression1 -a \( expression2 -o expression3 \) ]
>
> # [ "" = "`grep expr /path/file`" ]
> 	the right way:
> 	# grep -q expr /path/file
> 	you don't need any [ ].
> 		`echo` is the same like $(echo)
>
> # program_that_can_false para1 para2 ...
> # if [ $? = 0 ] ; then E1 ; fi
> 	what do you think, how "if" works? right:
> 	# if program_that_can_false para1 para2 ...
> 	# then E1
> 	# fi
>
> 	and the oposite, if you want to know, if it false:
> 	# if ! program_that_can_false para1 para2 ...
> 	# then E2
> 	# fi
>
> don't forget:
> a string can contain whitespaces!
> filenames can contain whitespaces too!!!
> in /etc/initng somebody can create a file like "oops.i no.i". if you
> process something on all i-files, this is a problem:
> # files=$(ls /etc/initng)
> # for f in ${files} ; do process $f ; done
> this won't work!
> this is right:
> # ls /etc/initng | while read f ; do process "$f" ; done
> these >"< are important!
> but this won't work if there's a newline in filename. :-/ but this is very,
> very rare.
>
> there're many ways to make it wrong. if i see this, i rewrite these lines.
> but now i can't rewrite all these many lines.
> please think about it and write it right!
> thanks
>
> denis knauf
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFFx1XgMpEdE19y46cRAtb4AKCFLKjF1oCOnylsD8zpMWCCGtohpwCgrCkE
MLNRuB8E9mESadA4JNqPkhM=
=Qj3O
-----END PGP SIGNATURE-----


More information about the Initng mailing list