bash versus sh test builtin

Karl Vogel vogelke+unix at pobox.com
Mon Jan 12 11:34:45 PST 2009


>> On Sun, 11 Jan 2009 20:08:18 -0600, 
>> Jeffrey Goldberg <jeffrey at goldmark.org> said:

J> if [ $UID -ne 0 ] ; then ...
J> Does anyone have a recommendation of how to run this simple test in /bin/sh
J> and how to write tests reasonably portably?

   I think your best bet for comparisons like this is to use case.  I started
   doing that back in the days when '[' would start a separate program:

     case "$UID" in
       0) echo am root ;;
       *) echo not root ;;
     esac

   I use "test" for things relating to file access:

     die() {
         echo "$@" >& 2
         exit 1
     }

     test -f /etc/passwd || die "Your system is seriously hosed"

-- 
Karl Vogel                      I don't speak for the USAF or my company

If men ruled the world #12: Instead of wasting money on an expensive
engagement ring, your fiancee would get a giant foam hand that said,
"You're #1!"


More information about the freebsd-questions mailing list