read output of process into a variable
Hi, don't know if this is the right newsgroup, but it's the only one I can find with "bash" in its name :-) I want to do something like this: result="" /usr/bin/output_generator | while read line; do extracteddata=`echo "$line" | sed -e 's/X/Y/'` result="$result $extracteddata" done /usr/bin/another_tool "$result" In the last line is "result" as empty as at the start of the whole thing - I guess because the inner "while" loop is executed in a subshell, so that changing the value of "result" in this loop does not affect the "outer result". How can I solve this? I have some very ugly solutions, but I guess there must be something "nice" :-) (using bash-3.2.17(1)-release) Thanks and regards -stefan-
Re: read output of process into a variable
It is not a bug in bash. it is just how it works. the while loop creates a subshell and changes to the variables are not visable outside of the subshell. if you put the while loop first, then it will not create the subshell. do this: result="" while read line; do extracteddata=`echo "$line" | sed -e 's/X/Y/'` result="$result $extracteddata" done < <(/usr/bin/output_generator) /usr/bin/another_tool "$result" the <() is syntax for a named pipes. it makes a command look like a file. Be aware that this may leave files in your /tmp directory. BTW: I would use $() syntax instead of the backtic syntax; just easier to see. -- potter On 30 Jan 2008 11:21:34 GMT, Stefan Palme <[EMAIL PROTECTED]> wrote: > Hi, > don't know if this is the right newsgroup, but it's the only one > I can find with "bash" in its name :-) > > I want to do something like this: > > result="" > /usr/bin/output_generator | while read line; do > extracteddata=`echo "$line" | sed -e 's/X/Y/'` > result="$result $extracteddata" > done > /usr/bin/another_tool "$result" > > In the last line is "result" as empty as at the start of the > whole thing - I guess because the inner "while" loop is executed > in a subshell, so that changing the value of "result" in this > loop does not affect the "outer result". > > How can I solve this? I have some very ugly solutions, but > I guess there must be something "nice" :-) > > (using bash-3.2.17(1)-release) > > Thanks and regards > -stefan- > > >
Re: read output of process into a variable
Thanks for your reply. > It is not a bug in bash. it is just how it works. I know, but did not find a gnu.bash.help newsgroup :$ > do this: > > result="" > while read line; do > extracteddata=`echo "$line" | sed -e 's/X/Y/'` result="$result > $extracteddata" > done < <(/usr/bin/output_generator) > /usr/bin/another_tool "$result" Nice... ;-) In real life even "/usr/bin/output_generator" is not only a call to one program, but a piped command consisting of three or four steps, which makes the whole thing unreadable again. But I guess I can create and use a function containing the "output_generator" chain... > BTW: I would use $() syntax instead of the backtic syntax; just easier > to see. Ok... Thanks and regards -stefan-
Re: read output of process into a variable
Stefan Palme wrote: > don't know if this is the right newsgroup, but it's the only one > I can find with "bash" in its name :-) That newsgroup is gatewayed to the bug-bash mailing list. > I want to do something like this: > > result="" > /usr/bin/output_generator | while read line; do > extracteddata=`echo "$line" | sed -e 's/X/Y/'` > result="$result $extracteddata" > done > /usr/bin/another_tool "$result" > > In the last line is "result" as empty as at the start of the > whole thing - I guess because the inner "while" loop is executed > in a subshell, so that changing the value of "result" in this > loop does not affect the "outer result". Please See the Bash FAQ question E4 for more information describing this problem. Bob
Re: bash 2.05b.0(1)-release on Debian Sarge: [A-Z]* expands as [A-Za-z]* :-(
Alan Mackenzie wrote: > Ah. I've got $LANG set to en_GB. Who did this? How dare they! > OK, I did this myself, somehow, presumably during installation of > Debian Sarge. On Debian: sudo dpkg-reconfigure locales > Why didn't "they" tell me I was messing up my shell? Why do I feel > so stupid? (OK, don't answer that one!) :-) You might also want to look at this (now old and in need of updating but applies here perfectly) FAQ for coreutils. http://www.gnu.org/software/coreutils/faq/#Sort-does-not-sort-in-normal-order_0021 > More to the point, where are these variables (LANG, > LC_{ALL,COLLATE,CTYPE,MESSAGES,NUMERIC} documented? They're > mentioned skimpily in the bash man page, but where are they fully > documented? What is a "locale category"? What set of values can > these variables take? _How_ are they "used"? For documentation I would start here at the standards docs: http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html#tag_08_02 Bob
Re: bash 2.05b.0(1)-release on Debian Sarge: [A-Z]* expands as [A-Za-z]* :-(
Hi, Bob and Eric, Thanks muchly for the help! On Mon, Jan 28, 2008 at 04:57:22PM -0700, Bob Proulx wrote: > Eric Blake wrote: > > According Alan Mackenzie: > > | % ls [A-Z]* > > | . Sadly, ls ignores my intentions and undiscerningly prints a list of > > | all files whose names begin with a letter, big or small. > > Actually, it follows your (unintended) directions, thanks to your > > current locale, which does a collation sort. You aren't doing > > [A-Za-z], but [AaBb...Z], because your current locale prefers > > case-insensitive collation. Change your locale (try LC_COLLATE=C or > > LC_ALL=C) to see the difference. Yes, this works! Ah. I've got $LANG set to en_GB. Who did this? How dare they! OK, I did this myself, somehow, presumably during installation of Debian Sarge. Why didn't "they" tell me I was messing up my shell? Why do I feel so stupid? (OK, don't answer that one!) More to the point, where are these variables (LANG, LC_{ALL,COLLATE,CTYPE,MESSAGES,NUMERIC} documented? They're mentioned skimpily in the bash man page, but where are they fully documented? What is a "locale category"? What set of values can these variables take? _How_ are they "used"? > Or in the new expression syntax say that you are looking for upper > case letters explicitly. > > ls -d [[:upper:]]* > That should work regardless of locale setting. OK. I'll use that if I really have to, but I'd prefer [A-Z] to work right. Again, thanks! > Bob
Re: read output of process into a variable
On Wed, 30 Jan 2008 13:44:47 -0700, Bob Proulx wrote: > Stefan Palme wrote: >> don't know if this is the right newsgroup, but it's the only one I can >> find with "bash" in its name :-) > > That newsgroup is gatewayed to the bug-bash mailing list. > >> I want to do something like this: >> >> result="" >> /usr/bin/output_generator | while read line; do >> extracteddata=`echo "$line" | sed -e 's/X/Y/'` result="$result >> $extracteddata" >> done >> /usr/bin/another_tool "$result" >> >> In the last line is "result" as empty as at the start of the whole >> thing - I guess because the inner "while" loop is executed in a >> subshell, so that changing the value of "result" in this loop does not >> affect the "outer result". > > Please See the Bash FAQ question E4 for more information describing this > problem. Thanks, I've understood the problem and solved it. And sorry for asking this FAQ on the wrong NG. Regards -stefan-