Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
how to test for empty parameter string ?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
javeree
Apprentice
Apprentice


Joined: 29 Jan 2006
Posts: 278

PostPosted: Tue Oct 09, 2012 12:22 pm    Post subject: how to test for empty parameter string ? Reply with quote

I want to test if the parameter list that is given to my scritpt is empty:

Code:
#! /bin/sh

STRING="hello world"
if [ -z "$STRING" ] ; then
        echo "variable STRING is empty"
else
        echo "STRING($STRING) is not empty"
fi

if  [ -z "$@" ] ; then
        echo "parameter string is empty"
else
        echo "parameter string("$@") is not empty"
fi

I run "test.sh hello world" and get
Quote:
STRING(hello world) is not empty
test.sh: line 10: [: hello: binary operator expected
parameter string(hello world) is not empty

Why doesnt "$@" behave like any other quoted variable ?
I don't get how I should quote $@ to be able to use -z as a test
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 7403
Location: Somewhere over Atlanta, Georgia

PostPosted: Tue Oct 09, 2012 12:59 pm    Post subject: Reply with quote

If the parameters are
Code:
hello world
then "$@" expands to
Code:
"hello" "world"
which produces a syntax error. What you're looking for is "$*". However, if "$@" (and, for that matter "$*") is non-empty, "$1" will be too, so I typically just use
Code:
if [ -z "$1" ]
instead. :wink:

- John
_________________
This space intentionally left blank.
Back to top
View user's profile Send private message
khayyam
Veteran
Veteran


Joined: 07 Jun 2012
Posts: 1545

PostPosted: Tue Oct 09, 2012 9:58 pm    Post subject: Reply with quote

javeree ...

$@ is an array, and as John stated each item in the array would be treated seperately, and -z tests the truth of a string.

Code:
count=1

for param in "$@" ; do
  echo "parameter #$count = $param"
  let "count+=1"
done

Also, in your test "parameter string is empty" is somewhat meaningless becuase the "$@" would not equate to $1 (existant) $2 (empty) $3 (existant). If you need at least three parameters:

Code:
if [[ "$#" -lt "3" ]] ; then

Anyhow, difficult to see what your aiming at with the above, but hopefully this helps in some way

best ... khay
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 7403
Location: Somewhere over Atlanta, Georgia

PostPosted: Wed Oct 10, 2012 12:27 am    Post subject: Reply with quote

khayyam wrote:
$@ is an array, ...
Well, yes and no. Specifically, no. :wink: You can prove this to yourself, if you like, by trying to subscript it. It's one of those funky special Bash things.

- John
_________________
This space intentionally left blank.
Back to top
View user's profile Send private message
khayyam
Veteran
Veteran


Joined: 07 Jun 2012
Posts: 1545

PostPosted: Wed Oct 10, 2012 5:46 am    Post subject: Reply with quote

John R. Graham wrote:
khayyam wrote:
$@ is an array, ...

Well, yes and no. Specifically, no. :wink: You can prove this to yourself, if you like, by trying to subscript it. It's one of those funky special Bash things.

John ... hmmm, well I would understand it as an array, "[t]he parameters *, @ and argv are arrays containing all the positional parameters; thus `$argv[n]', etc., is equivalent to simply `$n'." (The Z Shell Manual, Parameters) ... but yes, '$@[n]' doesn't return the nth of the array, but I assume this is becuase '$n' exists for this purpose.

best ... khay
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 7403
Location: Somewhere over Atlanta, Georgia

PostPosted: Wed Oct 10, 2012 10:32 am    Post subject: Reply with quote

You know, I'd have to say that the zsh manual is simply wrong on that point (or, at best, confusing and badly written). It's somewhat nonsensical to say in this context that it's an array (broader definition) that merely can't be accessed with bash/zsh array (the proper contextual definition) syntax. (You also know by now that I love to argue. ;))

- John
_________________
This space intentionally left blank.
Back to top
View user's profile Send private message
khayyam
Veteran
Veteran


Joined: 07 Jun 2012
Posts: 1545

PostPosted: Wed Oct 10, 2012 12:10 pm    Post subject: Reply with quote

John R. Graham wrote:
You know, I'd have to say that the zsh manual is simply wrong on that point (or, at best, confusing). It's somewhat nonsensical to say in this context that it's an array (broader definition) that merely can't be accessed with bash/zsh array (narrower contextual definition) syntax. (You also know by now that I love to argue. ;)

John ... I'd have to say your right, its confusing, nonsensical, etc, but then what would we call it? In the above 'for param in "$@"' each item in the 'thingy' is treated separately, if we replaced "$@" with "$argv" (bash/zsh) then this would return 'parameter #1 = foo bar baz', a single parameter. So, "$@" is an array, only accessed via the abbreviated $[n], its an array (of sorts) but the subscript is abstracted into an abbreviated form.

I'm open to suggestions, or further arguments ;)

best ... khay
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 7403
Location: Somewhere over Atlanta, Georgia

PostPosted: Wed Oct 10, 2012 2:29 pm    Post subject: Reply with quote

Actually, I think a technically clear explanation is possible: $@ is a parameter that expands to a list of quoted words. I still contend that $@ is not an array, regardless of whether or not its initialization source is an array, because it can't be used with shell array syntax (principally subscripting).

But, according to you, "$@" is an array because, in a for loop it expands to multiple words even when quoted, but then since "$argv" does not so expand, is argv then not an array? Seems like a reasonable conclusion to me. :wink:

- John
_________________
This space intentionally left blank.
Back to top
View user's profile Send private message
Hu
Watchman
Watchman


Joined: 06 Mar 2007
Posts: 8595

PostPosted: Thu Oct 11, 2012 1:34 am    Post subject: Reply with quote

John R. Graham wrote:
However, if "$@" (and, for that matter "$*") is non-empty, "$1" will be too, so I typically just use
Code:
if [ -z "$1" ]
instead. :wink:
Although generally true, this is not guaranteed. Consider:
jrg.sh:
#!/bin/sh

echo 1="$1"+
echo 2="$2"+
echo @="$@"+
if [ -z "$1" ]; then
    echo '1 is empty'
fi
if [ -z "$2" ]; then
    echo '2 is empty'
fi
if [ -z "$*" ]; then
    echo '* is empty'
fi
if [ -z "$@" ]; then
    echo '@ is empty'
fi
Code:
$ /bin/sh jrg.sh '' 2
1=+
2=2+
@= 2+
1 is empty
jrg.sh: line 15: [: : binary operator expected
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 7403
Location: Somewhere over Atlanta, Georgia

PostPosted: Thu Oct 11, 2012 1:46 am    Post subject: Reply with quote

Ahh, yes. Interesting corner case; thanks. :D

- John
_________________
This space intentionally left blank.
Back to top
View user's profile Send private message
khayyam
Veteran
Veteran


Joined: 07 Jun 2012
Posts: 1545

PostPosted: Thu Oct 11, 2012 5:42 am    Post subject: Reply with quote

John R. Graham wrote:
Actually, I think a technically clear explanation is possible: $@ is a parameter that expands to a list of quoted words. I still contend that $@ is not an array, regardless of whether or not its initialization source is an array, because it can't be used with shell array syntax (principally subscripting).

John ... ok, but it isn't exactly a "parameter" is it, or rather, its no more a parameter than it is an "array". Now, a "technically clear explanation" would need to provide a name, 'x, a thingy [...]', and while I'll admit that the absence of array syntax is "confusing" I'm at a loss as to how else I might describe it. If '$n' wasn't in place, then it would make sense to '$@[n]', but far less so due to the abreviations.

John R. Graham wrote:
[...] according to you, "$@" is an array because, in a for loop it expands to multiple words even when quoted, but then since "$argv" does not so expand, is argv then not an array? Seems like a reasonable conclusion to me. :wink:

hehe ... while I didn't actually say that I'm inclined to say I'm right :) ... had I wanted, I would have provided '$argv[@]' and it would have functioned identically to "$@" in the for loop, but I used "$argv" so that it'd function as a string. How about this "resonable conclusion": I'll grant that "$@" isn't an array, if you'll grant that "$argv[@]", which behaves identically, is also not? ;)

best ... khay
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 7403
Location: Somewhere over Atlanta, Georgia

PostPosted: Thu Oct 11, 2012 1:54 pm    Post subject: Reply with quote

Now you're just being silly. But then again, do we really understand what the word "silly" means in this context? Perhaps it warrants additional discussion.

- John
_________________
This space intentionally left blank.
Back to top
View user's profile Send private message
khayyam
Veteran
Veteran


Joined: 07 Jun 2012
Posts: 1545

PostPosted: Thu Oct 11, 2012 3:45 pm    Post subject: Reply with quote

John R. Graham wrote:
Now you're just being silly. But then again, do we really understand what the word "silly" means in this context? Perhaps it warrants additional discussion.

John ... yes, it was silly, obviously "$argv[@]" is an array, and I wouldn't say I was "right" in the conclusions you'd drawn from my previous post, however, I was serious wrt "$@" behaving identically to "$argv[@]", and similarly with being at a loss as to how else to describe it.

Anyow, to go back a bit in the discussion, in zsh "$@" actually is an array, and elements can be accessed in the usual manner:

Code:
% cat test.zsh
!#/bin/zsh
echo "$@[2]"
% ./test.zsh foo bar baz
bar

I didn't think to check at the time, but the manual would seem to be correct ITR.

For bash '$@' only seems to functions as shorthand for '$argv[@]', with '$n' as shorthand for the elements of the (virtual/pseudo) array. So, really, my initial description can be reduced to the lack of anything more exact with which to describe it, if you want to say, no its not an array, then I'm ok with that, but I probably wouldn't think twice about describing it as such.

best ... khay
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 7403
Location: Somewhere over Atlanta, Georgia

PostPosted: Thu Oct 11, 2012 4:11 pm    Post subject: Reply with quote

Hah! Touche! My primary domain knowledge comes from bash where $@ is not subscriptable and argv doesn't even exist.
Tom Duff[1] wrote:
Nobody really knows what the Bourne shell's grammar is. Even examination of the source code is little help.
- John

P.S.: I bet the OP is sorry he asked. :P

[1] - Tom Duff
_________________
This space intentionally left blank.
Back to top
View user's profile Send private message
khayyam
Veteran
Veteran


Joined: 07 Jun 2012
Posts: 1545

PostPosted: Thu Oct 11, 2012 5:23 pm    Post subject: Reply with quote

John R. Graham wrote:
Hah! Touche! My primary domain knowledge comes from bash where $@ is not subscriptable and argv doesn't even exist.

John ... oh, I'd assumed that bash had $argv, and I see it doesn't, so there are some assumptions above on my part.

Tom Duff wrote:
Nobody really knows what the Bourne shell's grammar is. Even examination of the source code is little help.

Thats because most of it was borrowed from ALGOL.

John R. Graham wrote:
P.S.: I bet the OP is sorry he asked. :P

If not ... they will be shortly :)

best ... khay
Back to top
View user's profile Send private message
steveL
Advocate
Advocate


Joined: 13 Sep 2006
Posts: 2160
Location: The Peanut Gallery

PostPosted: Thu Oct 11, 2012 5:49 pm    Post subject: Reply with quote

John R. Graham wrote:
What you're looking for is "$*".

Indeed.
Quote:
However, if "$@" (and, for that matter "$*") is non-empty, "$1" will be too

Er that's not true, though it might work for most interactive use.
Consider:
Code:
fubar "$var" "${list[@]}" foo

- where var is empty or unset.
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 7403
Location: Somewhere over Atlanta, Georgia

PostPosted: Thu Oct 11, 2012 6:19 pm    Post subject: Reply with quote

Yeah, TLDR: Hu beat you to it.

- John
_________________
This space intentionally left blank.
Back to top
View user's profile Send private message
mv
Advocate
Advocate


Joined: 20 Apr 2005
Posts: 3793

PostPosted: Thu Oct 11, 2012 7:00 pm    Post subject: Reply with quote

The description that "${@}" is an array is pretty correct: In fact, it is the only array that a POSIX shell does possess.
It is true that other bash and zsh arrays have a slgihtly different syntax, but it is obvious that this syntax was inspired by that (only) array: Actually the syntax is not that different as if this "parameter's array" would have the empty name. Syntactically it does not exactly behave like a bash or zsh array, but semantically it is without any doubt.
Back to top
View user's profile Send private message
steveL
Advocate
Advocate


Joined: 13 Sep 2006
Posts: 2160
Location: The Peanut Gallery

PostPosted: Fri Oct 12, 2012 10:06 am    Post subject: Reply with quote

John R. Graham wrote:
Yeah, TLDR: Hu beat you to it.

Oops, missed that, my bad.

Though his explanation was kinda freakish ;)
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum