On 05/24/2012 05:57 AM, David Haller wrote:
And stdin 'is empty', calc has read all lines, read x doesn't see any more lines:
So basically in z=$(calc -p '21+21') calc sees: $ calc -p '21+21' \ bar 3 \ bar 4 \ bar 5 \ bar 6 \ bar 7 \ bar 8 and discards everything else in the line beginning with the first 'bar' because as far as calc knows that is some undefined text? But then just continues reading the rest of the 'bar #' until it runs out of things to read?
$ printf "bar %s\n" $(seq 8) | while read x; read y; do \ z=$(strace -s 40 -eread calc -p '21+21'); echo "x=$x; y=$y; z=$z"; \ done [more reads of ELF binaries snipped and pruned] read(3, "\177ELF..., 832) = 832 read(0, "bar 3\nbar 4\nbar 5\nbar 6\nbar 7\nbar 8\n", 4096) = 36 "bar" is undefined
x=bar 1; y=bar 2; z=42
As you can see in the last 'read', calc read 'fd 0', i.e. stdin upto and_including_ the last line containing 'bar 8\n'. Tries to parse that stuff and barfs on the 'bar 3' on line 3 ('read x' and 'read y' read lines 1 and 2, as you can see in the 'echo' output).
Aah.. Gotcha :)
The part inside the '{ }' replaces your script!
$ printf "bar %s\n" $(seq 4) | { \ IFS=$'\n'; \ for x in $(seq 10 14); do \ z=$(calc -p "$x+1"); \ echo "x=$x; z=$z"; \ done; } "bar" is undefined
x=10; z=11 x=11; z=12 x=12; z=13 x=13; z=14 x=14; z=15
But: the for-loop doesn't abort once calc has read stdin (i.e. the 'printf 'bar %s\n' $(seq 4)' output), as the for loop does not depend on stdin.
OK, if I'm learning anything here, then that looks like: $ bar 1 \ bar 2 \ bar 3 \ bar 4 | { ..stuff.. z=$(calc -p "$x+1") ..stuff.. } calc still chokes on 'bar 1', but it has already executed the for x in $(seq 10 14) before it dies. So the calculations finished in the '{}'s before calc ate: 'bar 1 \n bar 2 \n bar 3 \n bar 4'
#!/bin/bash exec</dev/null
^^^^^^^^^^^^^^^^
IFS=$'\n' for l in $(<gtkrc-file.txt); do ....
Compare:
$ printf "bar %s\n" $(seq 4) | { exec</dev/null; IFS=$'\n'; \ for x in $(seq 10 14); do \ z=$(calc -p "$x+1"); echo "x=$x; z=$z"; done; } x=10; z=11 x=11; z=12 x=12; z=13 x=13; z=14 x=14; z=15
That way, you redirect your script's stdin from /dev/null, and thus 'calc' can't read anything from there that might confuse it.
That is the 'nugget' to be found. I would not have seen that or recognized that as an option for eons. That's where the (knowledge, skill, training, years of experience, etc..) really makes the difference. I completely lacked the appreciation for that level of redirection understanding. Oh, I've dug fairly far into redirection of stdin, stdout, and stderr, but stumbled face-first into what calc was doing with it ;-) <snip>
HTH, and ask if you're still (or again) confused, -dnh
dnh -- It does! That was excellent, and the archives will hold onto this thread to help close the gap for many to come in the levels of redirection understanding (as well as provide a jumping off point for furthering debugging skills) This is something that probably needs to be forwarded to the ABS folks for inclusion in the section of redirection or as it's own separate 'Advanced Topics in Redirection - Understanding Potential Errors'. What was it -- I believe attributed to Einstein -- "True Genius is the ability to explain the complexities of nature to a child." Thanks! -- David C. Rankin, J.D.,P.E. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org