[Gllug] Yes or no?

Nix nix at esperi.org.uk
Tue Nov 29 10:43:08 UTC 2005


On Mon, 28 Nov 2005, Matthew King stated:
> [ "${answer:0:1}" = Y -o  "${answer:0:1}" = y ] && true_function || false_function
> [ "`echo ${answer:0:1} | tr Y y" = y ] && true_function || false_function

Oh, yuck. case is *very* much easier to read in this situation.

(People keep forgetting about case: I know I used to. But it's very
useful 'cos it's a shell builtin and so can do all sorts of
filename-style globbing really rather fast on non-filenames.)

(e.g. this fragment:

        case $_VARIABLE in
         # Nested expansion. Cut the variable name up and expand the middle bit.
         # This set of regular expressions should perhaps win some kind of award
         # for unreadability; matching ${} constructions is not pretty.
         *\${*\${*}*}*)
             typeset _VPBEGIN=`echo $_VARIABLE | sed 's/\(^[^\$]*\${[^\$]*\).*$/\1/'`
             typeset _VPMIDDLE=`echo $_VARIABLE | sed 's/^[^\$]*\${[^\$]*\(\${.*}\)[^}]*}[^}]*$/\1/'`
             typeset _VPEND=`echo $_VARIABLE | sed 's/^.*}\([^}]*}[^}]*\)$/\1/'`
             _VARIABLE="$_VPBEGIN"`expand_variable "$_VPMIDDLE"`"$_VPEND";;
         # Conventional expansion.
         *\$*) _VARIABLE=`eval echo $_VARIABLE`;;
         # Expansions completed.
         *) break;;
        esac


ok, so perhaps this is not a good example of readable /bin/sh...)

> And, of course maybe spawning tr is just *too* much extra processing
> power...

Since you can do it in a shell builtin, yes! :)

-- 
`Y'know, London's nice at this time of year. If you like your cities
 freezing cold and full of surly gits.' --- David Damerell

-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list