test '-v' - associative vs. normal array discrepancy - a bug ?

2014-11-19 Thread Jason Vas Dias
Good day - Please could anyone explain why the first command below produces no output: $ ( declare -A a=([a]=1); if [ -v a ]; then echo yes; fi ) $ ( declare -a a=([0]=1); if [ -v a ]; then echo yes; fi ) yes $ There does not appear to be any documentation about different behaviour of -v for as

Re: test '-v' - associative vs. normal array discrepancy - a bug ?

2014-11-19 Thread Greg Wooledge
On Wed, Nov 19, 2014 at 04:20:51PM +, Jason Vas Dias wrote: > Good day - > Please could anyone explain why the first command below produces no output: > $ ( declare -A a=([a]=1); if [ -v a ]; then echo yes; fi ) > $ ( declare -a a=([0]=1); if [ -v a ]; then echo yes; fi ) > yes In a lot of

Re: test '-v' - associative vs. normal array discrepancy - a bug ?

2014-11-19 Thread Piotr Grzybowski
Hi, it is here: test.c: 642t = assoc_reference (assoc_cell (v), "0"); mostly what Greg said. do you think we should return true there for non-empty associative arrays? cheers, pg On Wed, Nov 19, 2014 at 5:20 PM, Jason Vas Dias wrote: > Good day - > Please could anyone explain why th

Re: test '-v' - associative vs. normal array discrepancy - a bug ?

2014-11-19 Thread Piotr Grzybowski
hi, the following makes -v return true for non-empty associative arrays, what do you think? diff --git a/test.c b/test.c index ab7bec7..8a91d1e 100644 --- a/test.c +++ b/test.c @@ -638,9 +638,13 @@ unary_test (op, arg) } else if (v && invisible_p (v) == 0 && assoc_p (v)) {

Re: test '-v' - associative vs. normal array discrepancy - a bug ?

2014-11-19 Thread Chet Ramey
On 11/19/14, 11:20 AM, Jason Vas Dias wrote: > Good day - > Please could anyone explain why the first command below produces no output: > $ ( declare -A a=([a]=1); if [ -v a ]; then echo yes; fi ) > $ ( declare -a a=([0]=1); if [ -v a ]; then echo yes; fi ) > yes > $ Dereferencing an array wit

Re: test '-v' - associative vs. normal array discrepancy - a bug ?

2014-11-19 Thread Chet Ramey
On 11/19/14, 2:14 PM, Piotr Grzybowski wrote: > hi, > > the following makes -v return true for non-empty associative arrays, > what do you think? No. There's a way to test for a non-zero number of elements directly. -- ``The lyf so short, the craft so long to lerne.'' - Chaucer

Re: test '-v' - associative vs. normal array discrepancy - a bug ?

2014-11-19 Thread Piotr Grzybowski
On Wed, Nov 19, 2014 at 8:25 PM, Chet Ramey wrote: > > No. There's a way to test for a non-zero number of elements directly. > Sure thing, I just had a feeling that Jason wants to use -v instead. I can turn it into -E VAR True if the shell variable VAR is an empty array ;-) cheers, pg

Re: test '-v' - associative vs. normal array discrepancy - a bug ?

2014-11-19 Thread Jason Vas Dias
Thanks to all who replied. I would really like -v to do as it documented to do : " -v True if the shell variable varname is set (has been assigned a value) " To me, the fact that -v does not return true if the variable is an array and does not have element 0 - or element '0' in the case of asso

Re: test '-v' - associative vs. normal array discrepancy - a bug ?

2014-11-19 Thread Greg Wooledge
On Wed, Nov 19, 2014 at 07:59:42PM +, Jason Vas Dias wrote: > [ ${#v[@] -gt 0 ] does not work > if $v is a normal variable. Doesn't it? It seems to work for me (once you fix the typo): imadev:~$ unset v; v=''; test "${#v[@]}" -gt 0 && echo yes yes imadev:~$ unset v; v='x'; test "${#v[@]}" -

Re: test '-v' - associative vs. normal array discrepancy - a bug ?

2014-11-19 Thread Piotr Grzybowski
On Wed, Nov 19, 2014 at 8:59 PM, Jason Vas Dias wrote: > [..] > It would be nice if -v could mean 'has any non-empty member' for > both associative and normal arrays - I think Piotr's suggestion > to make it do this is a good one. just as you were writing this, I kind of agreed that using ${#a[*

Re: test '-v' - associative vs. normal array discrepancy - a bug ?

2014-11-19 Thread Eduardo A . Bustamante López
People, are we forgetting that this is supposed to work in a function, by passing the name of a variable? i.e. it has to look like this: a= b=x is_defined a -> yes is_defined b -> yes is_defined c -> no The 'length of array' expansion doesn't work when you're given a name. So far, the best I

Re: test '-v' - associative vs. normal array discrepancy - a bug ?

2014-11-19 Thread Greg Wooledge
On Wed, Nov 19, 2014 at 02:41:13PM -0600, Eduardo A. Bustamante López wrote: > People, are we forgetting that this is supposed to work in a function, by > passing the name of a variable? Passing the name of a variable has NEVER worked in ANY function for ANY purpose in bash. It only works in ksh9

Re: test '-v' - associative vs. normal array discrepancy - a bug ?

2014-11-19 Thread Piotr Grzybowski
now I wonder, maybe this: $#a should return number of elements in array, if a is an array, and strlen($a) for scalars? cheers, pg On Wed, Nov 19, 2014 at 9:47 PM, Greg Wooledge wrote: > On Wed, Nov 19, 2014 at 02:41:13PM -0600, Eduardo A. Bustamante López wrote: >> People, are we forgett

Re: test '-v' - associative vs. normal array discrepancy - a bug ?

2014-11-19 Thread Greg Wooledge
On Wed, Nov 19, 2014 at 09:52:39PM +0100, Piotr Grzybowski wrote: > now I wonder, maybe this: > > $#a > > should return number of elements in array, if a is an array, and > strlen($a) for scalars? That would break existing scripts. $#a already means "argc followed by the letter a". If you mea

Re: test '-v' - associative vs. normal array discrepancy - a bug ?

2014-11-19 Thread Piotr Grzybowski
On Wed, Nov 19, 2014 at 9:56 PM, Greg Wooledge wrote: > > That would break existing scripts. $#a already means "argc followed by > the letter a". > > If you meant ${#a}, that would *still* break existing scripts, because > ${#a} when a is an array currently means "the length of a[0]". yes, righ

Re: test '-v' - associative vs. normal array discrepancy - a bug ?

2014-11-19 Thread Greg Wooledge
On Wed, Nov 19, 2014 at 09:59:38PM +0100, Piotr Grzybowski wrote: > On Wed, Nov 19, 2014 at 9:56 PM, Greg Wooledge wrote: > > > > That would break existing scripts. $#a already means "argc followed by > > the letter a". > > > > If you meant ${#a}, that would *still* break existing scripts, becaus

Re: test '-v' - associative vs. normal array discrepancy - a bug ?

2014-11-19 Thread Piotr Grzybowski
Greg, I swear to you, to anyone, this was not about writing libraries in bash. I would never dare to write to you about reusable code. From the code perspective, the information about what kind of object the identifier points to is present. And I use words "object" and "identifier" not to make yo

Re: test '-v' - associative vs. normal array discrepancy - a bug ?

2014-11-19 Thread Chet Ramey
On 11/19/14, 2:59 PM, Jason Vas Dias wrote: > Thanks to all who replied. > > I would really like -v to do as it documented to do : > " -v >True if the shell variable varname is set (has been assigned a value) > " > To me, the fact that -v does not return true if the variable is an array > and

Re: test '-v' - associative vs. normal array discrepancy - a bug ?

2014-11-19 Thread Greg Wooledge
On Wed, Nov 19, 2014 at 10:32:52PM +0100, Piotr Grzybowski wrote: > I do understand your point. Dont you understand the need to check in > a simple (I know, nothing works, nothing is simple ;-)) way that given > variable has value? no matter what it is? No, I don't. If I'm writing in C, and I wa

Re: test '-v' - associative vs. normal array discrepancy - a bug ?

2014-11-19 Thread Eduardo A . Bustamante López
On Wed, Nov 19, 2014 at 10:32:52PM +0100, Piotr Grzybowski wrote: > I do understand your point. Dont you understand the need to check in > a simple (I know, nothing works, nothing is simple ;-)) way that given > variable has value? no matter what it is? there: is_defined3() { { declare -p --

Re: test '-v' - associative vs. normal array discrepancy - a bug ?

2014-11-19 Thread Piotr Grzybowski
On Wed, Nov 19, 2014 at 10:48 PM, Greg Wooledge wrote: > > No, I don't. I completely respect that. > > If I'm writing in C, and I want to know whether one of my variables is > "empty", I have to know what type of variable it is. The code to check > whether a string (char x[10]) is "empty" is v

Re: test '-v' - associative vs. normal array discrepancy - a bug ?

2014-11-19 Thread Chet Ramey
On 11/19/14, 5:03 PM, Piotr Grzybowski wrote: > exactly. why is that? you touched the most important thing: types. > thats why in script languages, where you do not define variables with > types as such, you have the meaning of "empty", a="" makes a empty, > declare -A b makes b empty. You're us

Re: test '-v' - associative vs. normal array discrepancy - a bug ?

2014-11-19 Thread Piotr Grzybowski
On Wed, Nov 19, 2014 at 10:51 PM, Eduardo A. Bustamante López wrote: > On Wed, Nov 19, 2014 at 10:32:52PM +0100, Piotr Grzybowski wrote: >> I do understand your point. Dont you understand the need to check in >> a simple (I know, nothing works, nothing is simple ;-)) way that given >> variable ha

Re: test '-v' - associative vs. normal array discrepancy - a bug ?

2014-11-19 Thread Piotr Grzybowski
On Wed, Nov 19, 2014 at 11:05 PM, Chet Ramey wrote: > > You're using `empty' imprecisely. In your example, a is set, while b > is not. one could say that if it is not set, it is probably empty? yes, but it was on purpose, not to enter the distinction between hash table with no keys vs. uninitia

Re: test '-v' - associative vs. normal array discrepancy - a bug ?

2014-11-19 Thread Greg Wooledge
On Wed, Nov 19, 2014 at 11:05:51PM +0100, Piotr Grzybowski wrote: > On Wed, Nov 19, 2014 at 10:51 PM, Eduardo A. Bustamante López > wrote: > > is_defined3() { > > { declare -p -- "$1" && ! declare -fp -- "$1"; } 2>/dev/null >&2 > > } > > I dont know, but looks like a reusable code to me. No

Re: test '-v' - associative vs. normal array discrepancy - a bug ?

2014-11-19 Thread Piotr Grzybowski
On Wed, Nov 19, 2014 at 11:16 PM, Greg Wooledge wrote: > So far, so good. But > > imadev:~$ foo() { echo foo; } > imadev:~$ foo=bar > imadev:~$ is_defined3 foo ; echo $? > 1 Greg, does that mean, that "nothing works correctly"? pg

Re: test '-v' - associative vs. normal array discrepancy - a bug ?

2014-11-19 Thread Greg Wooledge
On Wed, Nov 19, 2014 at 11:22:48PM +0100, Piotr Grzybowski wrote: > On Wed, Nov 19, 2014 at 11:16 PM, Greg Wooledge wrote: > > So far, so good. But > > > > imadev:~$ foo() { echo foo; } > > imadev:~$ foo=bar > > imadev:~$ is_defined3 foo ; echo $? > > 1 > > Greg, does that mean, that "nothi

Re: test '-v' - associative vs. normal array discrepancy - a bug ?

2014-11-19 Thread Piotr Grzybowski
On Wed, Nov 19, 2014 at 11:30 PM, Greg Wooledge wrote: > > It demonstrates how easy it is to do things wrong in bash. Eduardo is > an experienced bash programmer, and even after several tries, he still > got it wrong. actually he said that it works for arrays, and variables, so basically you ar

Re: test '-v' - associative vs. normal array discrepancy - a bug ?

2014-11-19 Thread Eduardo A . Bustamante López
> So far, so good. But > > imadev:~$ foo() { echo foo; } > imadev:~$ foo=bar > imadev:~$ is_defined3 foo ; echo $? > 1 Ouch! Last try: is_defined4() { declare -p -- "$1" 2>/dev/null >&2 } But I agree with you, the shell is tricky.