Arithmetic Evaluation and Pipes in BASH

Reply

Join Date: Mar 2005
Posts: 1
Reputation: chrissi is an unknown quantity at this point 
Solved Threads: 0
chrissi chrissi is offline Offline
Newbie Poster

Arithmetic Evaluation and Pipes in BASH

 
0
  #1
Mar 13th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 1
Reputation: atucsprof is an unknown quantity at this point 
Solved Threads: 0
atucsprof atucsprof is offline Offline
Newbie Poster

Re: Arithmetic Evaluation and Pipes in BASH

 
0
  #2
Jun 26th, 2009
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.


Originally Posted by chrissi View 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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC