Re: [SLUG] Bash Help Needed

From: Eben King (eben01@verizon.net)
Date: Sat May 17 2008 - 17:39:57 EDT


On Sat, 17 May 2008, Michael Fisher wrote:

> On Sat, May 17, 2008 at 5:15 PM, Eben King <eben01@verizon.net> wrote:
>
>> On Sat, 17 May 2008, Michael Fisher wrote:
>>
>> Thanks for all the help....here what I happened on and got it to work:
>>>
>>> if [ "$#" != "1" ]; then
>>>
>>> cnt=10
>>> printf "is blank\n"
>>>
>>> else
>>> cnt=$1
>>> printf "is not blank\n"
>>>
>>> fi
>>>
>>> Basically it is checking arguments and if there is not one only, it is
>>> considered blank.
>>>
>>
>> For a numeric test such as that, there's no need for quotes ($# is not
>> user-supplied, so there's no easy way to make it have a space) you should do
>>
>> if [ $# -ne 1 ] ; then
>> cnt=10
>> printf "is blank\n"
>> else
>> cnt="$1" # always quote untrusted data:
>> # foo "15 ; if [ `id` -eq 0 ] ; then rm -r / ; fi"
>> printf "is not blank\n"
>> fi
>>
>> You know that if $# == 2, it reports "is blank", right?
>>
> Your way is cleaner so I ended up using it. Yes having two arguments reports
> blank, but that ends up being more of a check for the user to let them no
> one one is allowed

No need for two checks:

if [ $# -eq 0 ] ; then
   cnt=10
   printf "is blank\n"
else
   cnt="$1"
   printf "is not blank\n"
fi

Of course, if you forget the syntax and do "foo -h" you'll probably get an
error.

-- 
-eben      QebWenE01R@vTerYizUonI.nOetP      royalty.mine.nu:81
VIRGO:  All Virgos are extremely friendly and intelligent - except
for you.  Expect a big surprise today when you wind up with your
head impaled upon a stick. -- Weird Al, _Your Horoscope for Today_
-----------------------------------------------------------------------
This list is provided as an unmoderated internet service by Networked
Knowledge Systems (NKS).  Views and opinions expressed in messages
posted are those of the author and do not necessarily reflect the
official policy or position of NKS or any of its employees.



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 15:51:52 EDT