Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

www.xxxcon, which comes first ob_start or session, wwwxxx/58/2010, xxxxdup, xxxxdup, mailx informatii, should producers of software-based services, such as atms, be held liable for economic injuries suffered when their systems fail?, mysql feiertage, bash netcat eof, 192.168.1.41:8000

Links

XODOX
Impressum

#1: Checking argument in ksh

Posted on 2006-02-15 11:38:33 by 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

Report this message

#2: Re: Checking argument in ksh

Posted on 2006-02-15 12:01:22 by 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/

Report this message

#3: Re: Checking argument in ksh

Posted on 2006-02-15 12:03:44 by 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

Report this message