Fri, 03 Mar 2006

Writing Robust Shell Scripts Article

I've just finished writing an article on tips for making your shell scripts more robust. Comments welcome.

[, , ] | # Read Comments (6) |

Related Products

Comments

Another technique I use to handle whitespace on shell scripts is to put each value on a line and then use read inside while loops:

echo "$LINES" | while read value; do
  echo "The value is '$value'";
done

Depending on what you want to do it is quite efective, but keep in mind that this usage seems to create a subshell for the while and the variables used inside the while are not available outside it.
Posted by Sergio Talens-Oliag at Fri Mar 3 12:53:34 2006
Useful article. Linked from URL given.
Posted by MJR/slef at Fri Mar 3 12:59:07 2006
if [ "$filename" == "foo" ];

contains 2 bugs (1 if you use set -u);
if $filename is unset, this will fail,
and == is a bashism, use = instead, like this:

if [ x"$filename" = x"foo" ];


function foo { for i in "$@"; do echo $i; done }

should preferably be written

foo() { for i in "$@"; do echo $i; done }

instead, since function is also a bashism.
Posted by David weinehall at Fri Mar 3 16:14:16 2006
The title is "Writing Robust Bash Shell Scripts". I think bashisms are allowed in that context. :)
Posted by JD at Fri Mar 3 16:28:33 2006
If writing bash shellscripts, I suggest using [[ and ]] instead of [ ].

Then,

  if [[ $a = $b ]]; ...; fi

won't produce an error if a/b are empty or contain whitespace.
Posted by madduck at Thu Mar 16 09:03:20 2006
Only problem is that [[ isn't posix:

mimsy david% dash
$ if [[ "a" = "b" ]]; then echo "true"; fi
dash: [[: not found
$ if [ "a" = "b" ]; then echo "true"; fi
Posted by JD at Thu Mar 16 09:15:46 2006

Name:


E-mail:


URL:


Comment:


Please enter "fudge" to prove you are a human