[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Testing for null positional parameters.



> 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

There are a few options.

You can test for the number of positional parameters with  $#
So, if you pass the script three parameters,   $#  will be set to "3".
If you don't pass it any parameters,  $#  will be "0".   etc.


Or you can check like you are trying to do above like:

    if [ -n "$3" ]; then
	echo yes
    else 
	echo no
    fi

The "-n" checks for non-zero length, or "-z" checks for zero length.


Or you can substitute non-set parameters to another value with:

${3:-value}

  if [ ${3:-isnotset} = "isnotset" ]; then
      echo parameter 3 is not set
  else
      echo parameter 3 is set to: $3
  fi


And there are a lot of other ways, but I posted my favorites.

--------------------------------------------
Bruce Smith                bruce@armintl.com
System Administrator / Network Administrator
Armstrong International, Inc.
Three Rivers, Michigan  49093  USA
http://www.armstrong-intl.com/
--------------------------------------------