zsh-users
 help / color / mirror / code / Atom feed
* Strange behavior with "for i in .."
@ 2011-09-23 10:29 Volodya Khomchak
  2011-09-23 10:41 ` Mikael Magnusson
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Volodya Khomchak @ 2011-09-23 10:29 UTC (permalink / raw)
  To: zsh-users

Hi,

I faced with strange behavior with "for i in ..".
So the problem is next:
   # for i in /etc/profile.d/*.sh;do echo $i;done
   # zsh: bad math expression: operand expected at `/etc/profi...'
But if I change "i" to "file" it would work:
   # for file in /etc/profile.d/*.sh;do echo $file;done
   # /etc/profile.d/1.sh
   # /etc/profile.d/2.sh

zsh --version
zsh 4.3.10 (x86_64)

So what is going on here ?

Thanks,
Volodya


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Strange behavior with "for i in .."
  2011-09-23 10:29 Strange behavior with "for i in .." Volodya Khomchak
@ 2011-09-23 10:41 ` Mikael Magnusson
  2011-09-23 10:51   ` Volodya Khomchak
  2011-09-23 10:41 ` Peter Stephenson
  2011-09-23 10:43 ` Oliver Kiddle
  2 siblings, 1 reply; 5+ messages in thread
From: Mikael Magnusson @ 2011-09-23 10:41 UTC (permalink / raw)
  To: Volodya Khomchak; +Cc: zsh-users

On 23 September 2011 12:29, Volodya Khomchak <kolombo.inc@gmail.com> wrote:
> Hi,
>
> I faced with strange behavior with "for i in ..".
> So the problem is next:
>  # for i in /etc/profile.d/*.sh;do echo $i;done
>  # zsh: bad math expression: operand expected at `/etc/profi...'
> But if I change "i" to "file" it would work:
>  # for file in /etc/profile.d/*.sh;do echo $file;done
>  # /etc/profile.d/1.sh
>  # /etc/profile.d/2.sh
>
> zsh --version
> zsh 4.3.10 (x86_64)
>
> So what is going on here ?

At some point you've done 'integer i', so when you assign to i it will
be evaluated as a math expression. Try "echo ${(t)i}". "unset i"
before the loop should fix it.

-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Strange behavior with "for i in .."
  2011-09-23 10:29 Strange behavior with "for i in .." Volodya Khomchak
  2011-09-23 10:41 ` Mikael Magnusson
@ 2011-09-23 10:41 ` Peter Stephenson
  2011-09-23 10:43 ` Oliver Kiddle
  2 siblings, 0 replies; 5+ messages in thread
From: Peter Stephenson @ 2011-09-23 10:41 UTC (permalink / raw)
  To: zsh-users

On Fri, 23 Sep 2011 13:29:49 +0300
Volodya Khomchak <kolombo.inc@gmail.com> wrote:
> I faced with strange behavior with "for i in ..".
> So the problem is next:
>    # for i in /etc/profile.d/*.sh;do echo $i;done
>    # zsh: bad math expression: operand expected at `/etc/profi...'
> But if I change "i" to "file" it would work:
>    # for file in /etc/profile.d/*.sh;do echo $file;done
>    # /etc/profile.d/1.sh
>    # /etc/profile.d/2.sh
> 
> zsh --version
> zsh 4.3.10 (x86_64)
> 
> So what is going on here ?

Probably the first time i is referred to in the shell was in something like

  for (( i = 0; i < stuff; i++ )); ...

or, in fact, any arithmetic involving "i".  This has caused "i" to be
implicitly typed as an integer.  This only happens if i doesn't exist at
that point.

There are various things you can do;

  typeset i

before you use i the first time will ensure it's a scalar (i.e. string)
value even if you use it in arithmetic subsequently.

Or,

  typeset +i i

before you use it for any other purpose than arithmetic.

Or, if the original use was in initialisation code and you don't want i
to be exposed by it, simply

  unset i

after that would be good enough.

Or, of course, you can keep i for integers and use another variable for
strings, which is in effect what you tried.

-- 
Peter Stephenson <pws@csr.com>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom
More information can be found at www.csr.com. Follow CSR on Twitter at http://twitter.com/CSR_PLC and read our blog at www.csr.com/blog


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Strange behavior with "for i in .."
  2011-09-23 10:29 Strange behavior with "for i in .." Volodya Khomchak
  2011-09-23 10:41 ` Mikael Magnusson
  2011-09-23 10:41 ` Peter Stephenson
@ 2011-09-23 10:43 ` Oliver Kiddle
  2 siblings, 0 replies; 5+ messages in thread
From: Oliver Kiddle @ 2011-09-23 10:43 UTC (permalink / raw)
  To: zsh-users

Volodya Khomchak wrote:
> I faced with strange behavior with "for i in ..".
> So the problem is next:
>    # for i in /etc/profile.d/*.sh;do echo $i;done
>    # zsh: bad math expression: operand expected at `/etc/profi...'

the i variable has somewhere been declared as an integer type. For
example with a command such as:
  typeset -i i
or more likely in a math expression such as:
  (( i = 1 ))

You can verify this by doing:
  typeset -p i
or if you prefer:
  echo ${(t)i}

You can use typeset +i i to remove the integer type. However, you may
want to check where it is being set. There might be a shell function
that should have declared i local.

Oliver


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Strange behavior with "for i in .."
  2011-09-23 10:41 ` Mikael Magnusson
@ 2011-09-23 10:51   ` Volodya Khomchak
  0 siblings, 0 replies; 5+ messages in thread
From: Volodya Khomchak @ 2011-09-23 10:51 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: zsh-users

Yes, you are right :)
Thanks.

On 09/23/2011 01:41 PM, Mikael Magnusson wrote:
> On 23 September 2011 12:29, Volodya Khomchak<kolombo.inc@gmail.com>  wrote:
>> Hi,
>>
>> I faced with strange behavior with "for i in ..".
>> So the problem is next:
>>   # for i in /etc/profile.d/*.sh;do echo $i;done
>>   # zsh: bad math expression: operand expected at `/etc/profi...'
>> But if I change "i" to "file" it would work:
>>   # for file in /etc/profile.d/*.sh;do echo $file;done
>>   # /etc/profile.d/1.sh
>>   # /etc/profile.d/2.sh
>>
>> zsh --version
>> zsh 4.3.10 (x86_64)
>>
>> So what is going on here ?
>
> At some point you've done 'integer i', so when you assign to i it will
> be evaluated as a math expression. Try "echo ${(t)i}". "unset i"
> before the loop should fix it.
>


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2011-09-23 11:15 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-23 10:29 Strange behavior with "for i in .." Volodya Khomchak
2011-09-23 10:41 ` Mikael Magnusson
2011-09-23 10:51   ` Volodya Khomchak
2011-09-23 10:41 ` Peter Stephenson
2011-09-23 10:43 ` Oliver Kiddle

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).