Checking argument in ksh

Checking argument in ksh

am 15.02.2006 11:38:33 von herbert

Hello,

I have a question to check argument in the script.
I'd like to check an argument as follows:

If there is no argument,

if [ $1 == " " ]
then
echo " error, no input data "
exit 1
fi

But if [ $1 == " " ] didn't work. What is the correct expression
please?

Many thanks

Re: Checking argument in ksh

am 15.02.2006 12:01:22 von Klaus Alexander Seistrup

Herbert wrote:

> I have a question to check argument in the script.
> I'd like to check an argument as follows:
>
> If there is no argument,
>
> if [ $1 == " " ]
> then
> echo " error, no input data "
> exit 1
> fi
>
> But if [ $1 == " " ] didn't work. What is the correct
> expression please?

You could use

if [ $# = 0 ]

or

if [ -z "$1" ]


Cheers,

--
Klaus Alexander Seistrup
SubZeroNet, Copenhagen, Denmark
http://magnetic-ink.dk/

Re: Checking argument in ksh

am 15.02.2006 12:03:44 von Stephane CHAZELAS

On 15 Feb 2006 02:38:33 -0800, Herbert wrote:
> Hello,
>
> I have a question to check argument in the script.
> I'd like to check an argument as follows:
>
> If there is no argument,
>
> if [ $1 == " " ]
> then
> echo " error, no input data "
> exit 1
> fi
>
> But if [ $1 == " " ] didn't work. What is the correct expression
> please?
[...]

"==" is incorrect, only supported by some shells.

if [ $1 = " " ]

is bogus because you didn't quote $1

if [ "$1" = " " ]

checks whether the first argument is a space

if [ "$1" = "" ]
or [ -z "$1" ]

checks whether the first argument is empty (or unset).

if [ "$#" -eq 0 ]

checks if there are any arguments provided.

--
Stephane