| | |
Arithmetic Evaluation and Pipes in BASH
Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2005
Posts: 1
Reputation:
Solved Threads: 0
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:
Everything works as expected when doing:
But when i use the "echo" command in a pipe, (like:)
the postincrement of "ind" is not evaluated, so the next time i also get:
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
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)
$ typeset -a arr; arr=(bourne again shell) $ typeset -i ind=0
Everything works as expected when doing:
Shell Scripting Syntax (Toggle Plain Text)
$ echo ${arr[((ind++))]} bourne $ echo ${arr[((ind++))]} again $ echo ${arr[((ind++))]} shell
But when i use the "echo" command in a pipe, (like:)
Shell Scripting Syntax (Toggle Plain Text)
$ typeset -i ind=0 $ echo ${arr[((ind++))]} | grep e bourne
the postincrement of "ind" is not evaluated, so the next time i also get:
Shell Scripting Syntax (Toggle Plain Text)
$ echo ${arr[((ind++))]} | grep e bourne $ echo ${arr[((ind++))]} | grep e 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
•
•
Join Date: Jun 2009
Posts: 1
Reputation:
Solved Threads: 0
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.
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.
•
•
•
•
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)
$ typeset -a arr; arr=(bourne again shell) $ typeset -i ind=0
Everything works as expected when doing:
Shell Scripting Syntax (Toggle Plain Text)
$ echo ${arr[((ind++))]} bourne $ echo ${arr[((ind++))]} again $ echo ${arr[((ind++))]} shell
But when i use the "echo" command in a pipe, (like
Shell Scripting Syntax (Toggle Plain Text)
$ typeset -i ind=0 $ echo ${arr[((ind++))]} | grep e bourne
the postincrement of "ind" is not evaluated, so the next time i also get:
Shell Scripting Syntax (Toggle Plain Text)
$ echo ${arr[((ind++))]} | grep e bourne $ echo ${arr[((ind++))]} | grep e 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
- how to recursive search of subdiretories in BASH script (Shell Scripting)
- Computer Architecture Reference (Computer Science)
- Short-circuit Boolean evaluation in C# ? (C#)
- Server 2003 evaluation copy (Windows NT / 2000 / XP)
- Need help with BASH to MS-DOS Batch conversion. (Shell Scripting)
- Pipe inside variable isn't working in bash (Shell Scripting)
Other Threads in the Shell Scripting Forum
- Previous Thread: Script to Change file permissions
- Next Thread: How to pass a shell variable to a perl command
| Thread Tools | Search this Thread |






