Bad Shell: Lazyweb replies

Yesterday I posted about some bad
shell
code I had found and posted an improved version. Part of the
reason for posting it was that I hoping someone could point out any
errors in the version I posted. Fortunately Neil Moore emailed me some
improvements.

  1. If the script returns more than one line they will be removed by the
    $(…) expansion when it is split into words. The solution there is to
    surround it in double quotes.
  2. The next problem Neil pointed out was that $@ should be
    surrounded by quotes in pretty much every case, otherwise parameters
    with spaces in will get split into separate parameters.
  3. The final problem is that if the script includes a return statement, it
    will stop the inner most function or sourced script, but not during
    eval.The solution is to enclose it in a function:

    
    dummy() { eval "$(perl "$CONF_DIR/project.pl" "$@")"; } dummy "$@"
    
    

Since making the post, I discovered that Solaris’ /bin/sh
doesn’t like $(...), so it’s probably better to use backticks
instead if you want to be portable. As I know the output from the script
I’m not worried about return statements, so I’ve ended up with:


eval "`perl "$CONF_DIR/project.pl" "$@"`"

Leave a Reply

Your email address will not be published. Required fields are marked *


Time limit is exhausted. Please reload CAPTCHA.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>