[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re[2]: Testing for null positional parameters.
Hello Bruce,
Thank you. I believe I will use the first one. I hadn't thought
about using if/then because I hadn't flipped two pages back in the
book.
Thanks again.
---
Tim
IT Technician
ADAC Plastics, Inc.
---
Wednesday, January 10, 2001, 10:27:25 AM, you wrote:
>> I am writing a script that I will use to sort files, sometime making
>> hard links in multiple directories. That part that I'm stuck on is
>> the subject of this message, testing for null positional parameters.
>>
>> if
>> test $n "is not null" (where n is the number of the position)
>> then
>> command
>> fi
BS> There are a few options.
BS> You can test for the number of positional parameters with $#
BS> So, if you pass the script three parameters, $# will be set to "3".
BS> If you don't pass it any parameters, $# will be "0". etc.
BS> Or you can check like you are trying to do above like:
BS> if [ -n "$3" ]; then
BS> echo yes
BS> else
BS> echo no
BS> fi
BS> The "-n" checks for non-zero length, or "-z" checks for zero length.
BS> Or you can substitute non-set parameters to another value with:
BS> ${3:-value}
BS> if [ ${3:-isnotset} = "isnotset" ]; then
BS> echo parameter 3 is not set
BS> else
BS> echo parameter 3 is set to: $3
BS> fi
BS> And there are a lot of other ways, but I posted my favorites.
BS> --------------------------------------------
BS> Bruce Smith bruce@armintl.com
BS> System Administrator / Network Administrator
BS> Armstrong International, Inc.
BS> Three Rivers, Michigan 49093 USA
BS> http://www.armstrong-intl.com/
BS> --------------------------------------------