943,813 Members | Top Members by Rank

Ad:
Mar 13th, 2005
0

Arithmetic Evaluation and Pipes in BASH

Expand Post »
Hi,

I had some irritation with a "while"- loop, which turned out to be endless.
Anyway, i was able to find the problem and reduce it to be shown on the commandline:

I "typeset" (and initialize) an array and an integer variable:
Shell Scripting Syntax (Toggle Plain Text)
  1. $ typeset -a arr; arr=(bourne again shell)
  2. $ typeset -i ind=0

Everything works as expected when doing:
Shell Scripting Syntax (Toggle Plain Text)
  1. $ echo ${arr[((ind++))]}
  2. bourne
  3. $ echo ${arr[((ind++))]}
  4. again
  5. $ echo ${arr[((ind++))]}
  6. shell

But when i use the "echo" command in a pipe, (like:)
Shell Scripting Syntax (Toggle Plain Text)
  1. $ typeset -i ind=0
  2. $ echo ${arr[((ind++))]} | grep e
  3. bourne

the postincrement of "ind" is not evaluated, so the next time i also get:
Shell Scripting Syntax (Toggle Plain Text)
  1. $ echo ${arr[((ind++))]} | grep e
  2. bourne
  3. $ echo ${arr[((ind++))]} | grep e
  4. bourne

Well, this is not really a problem for i can do it in two lines, but i wonder, if it's possible to make it work in one line.

Anywone who can give a comment?

Thanks for reading and trying to help!
Bye,
Christoph
Similar Threads
Reputation Points: 10
Solved Threads: 0
Unverified User
chrissi is offline Offline
1 posts
since Mar 2005
Jun 26th, 2009
0

Re: Arithmetic Evaluation and Pipes in BASH

Christoph,

The answer is that every member of a pipeline is turned into a separate process with its own environment. So changing the variable in a pipeline changes it in a subshell and not the current shell.

You'll have to change the value of the variable after invoking the pipe.


Click to Expand / Collapse  Quote originally posted by chrissi ...
Hi,

I had some irritation with a "while"- loop, which turned out to be endless.
Anyway, i was able to find the problem and reduce it to be shown on the commandline:

I "typeset" (and initialize) an array and an integer variable:
Shell Scripting Syntax (Toggle Plain Text)
  1. $ typeset -a arr; arr=(bourne again shell)
  2. $ typeset -i ind=0

Everything works as expected when doing:
Shell Scripting Syntax (Toggle Plain Text)
  1. $ echo ${arr[((ind++))]}
  2. bourne
  3. $ echo ${arr[((ind++))]}
  4. again
  5. $ echo ${arr[((ind++))]}
  6. shell

But when i use the "echo" command in a pipe, (like
Shell Scripting Syntax (Toggle Plain Text)
  1. $ typeset -i ind=0
  2. $ echo ${arr[((ind++))]} | grep e
  3. bourne

the postincrement of "ind" is not evaluated, so the next time i also get:
Shell Scripting Syntax (Toggle Plain Text)
  1. $ echo ${arr[((ind++))]} | grep e
  2. bourne
  3. $ echo ${arr[((ind++))]} | grep e
  4. bourne

Well, this is not really a problem for i can do it in two lines, but i wonder, if it's possible to make it work in one line.

Anywone who can give a comment?

Thanks for reading and trying to help!
Bye,
Christoph
Reputation Points: 10
Solved Threads: 0
Newbie Poster
atucsprof is offline Offline
1 posts
since Jun 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: Script to Change file permissions
Next Thread in Shell Scripting Forum Timeline: How to pass a shell variable to a perl command





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC