[colug-432] Shell Tests

Matthew Hyclak hyclak at gmail.com
Tue Oct 6 17:28:19 EDT 2015


I believe it was significant when /bin/sh was really /bin/sh and not bash
in sh mode

For example:

root at hyclak:~ # ls -l /bin/sh

lrwxrwxrwx. 1 root root 4 Sep 25 17:01 /bin/sh -> bash

root at hyclak:~ # cat /etc/redhat-release

CentOS release 6.7 (Final)

On Tue, Oct 6, 2015 at 5:21 PM, <jep200404 at columbus.rr.com> wrote:

> Which shell test would you choose? Why?
> Would you choose a test not shown below? Why?
>
> I saw a shell test like [ "z${1}" = "z" ],
> which got me wondering, why bother with the 'z'?
> Is there some subtle behavior, perhaps when there
> is no "${1}" argument? So I experimented.
>
> These tests worked the same:
>
>     [ "${1:+is_set}" != "is_set" ]
>     [ "z${1}" = "z" ]
>     [ "${1}" = "" ]
>
> The last of those above is curious.
> Since it works the same as with 'z', why bother with the 'z'?
>
> Only [ "${#}" -lt 1 ] was different,
> and seems more strictly correct
> unless an empty first argument is to be treated the
> same as no first argument.
>
>     [user at colug ~]$ cat boo.sh
>     #!/bin/sh
>
>     a="${1:+is_set}"
>     echo "a is >$a<"
>
>     if [ "${1:+is_set}" != "is_set" ]; then
>         echo one
>     fi
>
>     b="z${1}"
>     echo "b is >$b<"
>
>     if [ "z${1}" = "z" ]; then
>         echo two
>     fi
>
>     c="${1}"
>     echo "c is >$c<"
>
>     if [ "${1}" = "" ]; then
>         echo tree
>     fi
>
>     d="${#}"
>     echo "d is >$d<"
>
>     if [ "${#}" -lt 1 ]; then
>         echo for
>     fi
>     [user at colug ~]$ ./boo.sh
>     a is ><
>     one
>     b is >z<
>     two
>     c is ><
>     tree
>     d is >0<
>     for
>     [user at colug ~]$ ./boo.sh ''
>     a is ><
>     one
>     b is >z<
>     two
>     c is ><
>     tree
>     d is >1<
>     [user at colug ~]$ ./boo.sh ' '
>     a is >is_set<
>     b is >z <
>     c is > <
>     d is >1<
>     [user at colug ~]$ ./boo.sh '' world
>     a is ><
>     one
>     b is >z<
>     two
>     c is ><
>     tree
>     d is >2<
>     [user at colug ~]$ ./boo.sh ' ' world
>     a is >is_set<
>     b is >z <
>     c is > <
>     d is >2<
>     [user at colug ~]$ ./boo.sh hello world
>     a is >is_set<
>     b is >zhello<
>     c is >hello<
>     d is >2<
>     [user at colug ~]$
> _______________________________________________
> colug-432 mailing list
> colug-432 at colug.net
> http://lists.colug.net/mailman/listinfo/colug-432
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.colug.net/pipermail/colug-432/attachments/20151006/cf7acd4b/attachment.html 


More information about the colug-432 mailing list