Re: output redirection with process substitution asynchronous?
Hello again, I have to reply to my own post to correct it: On Dec 8, 2:00 pm, pjodrr pjo...@gmail.com wrote: coproc prefix_timestamp seq 10${COPROC[1]} eval exec ${COPROC[1]}- cat ${COPROC[0]} wait $COPROC_PID replace this with: { coproc prefix_timestamp 3 ; } 31 seq 10${COPROC[1]} eval exec ${COPROC[1]}- wait $COPROC_PID this is how i do it now, thanks for the discussion. Peter
Re: output redirection with process substitution asynchronous?
pk a écrit : I disagree. All the further changes in the requirements because creating a subshell or being asynchronous is not acceptable etc. are not a goal in themselves, but rather the indicators that he's trying to accomplish something else. I think he just want side-effects like in this recent example from Chris: http://thread.gmane.org/gmane.comp.shells.bash.bugs/13863/focus=13907 Granted: if he was explaining in greater detail which side-effects he wants, people might be able to suggest better alternatives.
Re: output redirection with process substitution asynchronous?
On Dec 8, 11:05 am, Marc Herbert marc.herb...@gmail.com wrote: pk a écrit : I disagree. All the further changes in the requirements because creating a subshell or being asynchronous is not acceptable etc. are not a goal in themselves, but rather the indicators that he's trying to accomplish something else. I think he just want side-effects like in this recent example from Chris: http://thread.gmane.org/gmane.comp.shells.bash.bugs/13863/focus=13907 Granted: if he was explaining in greater detail which side-effects he wants, people might be able to suggest better alternatives. I promise I'll do better the next time. Peter
Re: output redirection with process substitution asynchronous?
On Dec 8, 10:55 am, Marc Herbert marc.herb...@gmail.com wrote: DennisW wrote : Would you care to comment on the coproc command in Bash 4? I wish I could, but I know nothing about it. Anyone else? yeah, I tried that: prefix_timestamp() { while read line; do echo $(date): $line done } coproc prefix_timestamp seq 10${COPROC[1]} eval exec ${COPROC[1]}- cat ${COPROC[0]} wait $COPROC_PID but am not sure if I like that construct better. Peter
Re: output redirection with process substitution asynchronous?
pjodrr wrote: It would be nice if you explained what it is you're attempting to do, rather than ask for a solution for what you're thinking would do that. To be honest that is the first thing he (tried to) do: pjodrr wrote: how can I prefix every line of output of some command with a timestamp? What is wrong with the following: prefix_with_date () { while read; do printf '%s: %s\n' $(date) $REPLY; done } seq 4 | prefix_with_date ls | prefix_with_date
Re: output redirection with process substitution asynchronous?
Marc Herbert wrote: What is wrong with the following: prefix_with_date () { while read; do printf '%s: %s\n' $(date) $REPLY; done } seq 4 | prefix_with_date ls | prefix_with_date Sorry I missed the fact that you want to run your commands in the current shell. There are no real coroutines in shell. The current shell process does not know how to schedule two pieces of code. So each time two pieces of code communicate through a pipe they have to be run in two *concurrent* processes (typically: subshells). The producer and consumer of a pipe must run independently of each other. Whether you are using process substitution or the more usual and portable pipe | does not matter here: you need concurrency. So at least one of: 1. your prefixer code, 2. your unknown command has to run in a independent process, asynchronous with the current shell. Since you absolutely want your unknown commands to run in the current shell, then it is your prefixing code that has to run in a concurrent process. Now I do not really see any other way to avoid the ugliness of concurrently printing on stdout than to wait for your concurrent prefixer to complete, more or less like you did. A variant is to ask socat to handle the cleanup actions for you like this: prefix_with_date() { local P=/tmp/dataorig.$$ socat -u PIPE:${P} SYSTEM:'while read; do echo $(date):\\ $REPLY; done' socatPID=$! until [ -e ${P} ]; do sleep 1; done $@ ${P} wait $socatPID } prefix_with_date seq 5
Re: output redirection with process substitution asynchronous?
On Dec 7, 10:25 am, Marc Herbert marc.herb...@gmail.com wrote: Marc Herbert wrote: What is wrong with the following: prefix_with_date () { while read; do printf '%s: %s\n' $(date) $REPLY; done } seq 4 | prefix_with_date ls | prefix_with_date Sorry I missed the fact that you want to run your commands in the current shell. There are no real coroutines in shell. The current shell process does not know how to schedule two pieces of code. So each time two pieces of code communicate through a pipe they have to be run in two *concurrent* processes (typically: subshells). The producer and consumer of a pipe must run independently of each other. Whether you are using process substitution or the more usual and portable pipe | does not matter here: you need concurrency. So at least one of: 1. your prefixer code, 2. your unknown command has to run in a independent process, asynchronous with the current shell. Since you absolutely want your unknown commands to run in the current shell, then it is your prefixing code that has to run in a concurrent process. Now I do not really see any other way to avoid the ugliness of concurrently printing on stdout than to wait for your concurrent prefixer to complete, more or less like you did. A variant is to ask socat to handle the cleanup actions for you like this: prefix_with_date() { local P=/tmp/dataorig.$$ socat -u PIPE:${P} SYSTEM:'while read; do echo $(date):\\ $REPLY; done' socatPID=$! until [ -e ${P} ]; do sleep 1; done $@ ${P} wait $socatPID } prefix_with_date seq 5 Would you care to comment on the coproc command in Bash 4?
Re: output redirection with process substitution asynchronous?
Hi Marc, On Dec 7, 5:25 pm, Marc Herbert marc.herb...@gmail.com wrote: Marc Herbert wrote: What is wrong with the following: prefix_with_date () { while read; do printf '%s: %s\n' $(date) $REPLY; done } seq 4 | prefix_with_date ls | prefix_with_date Sorry I missed the fact that you want to run your commands in the current shell. There are no real coroutines in shell. The current shell process does not know how to schedule two pieces of code. So each time two pieces of code communicate through a pipe they have to be run in two *concurrent* processes (typically: subshells). The producer and consumer of a pipe must run independently of each other. Whether you are using process substitution or the more usual and portable pipe | does not matter here: you need concurrency. So at least one of: 1. your prefixer code, 2. your unknown command has to run in a independent process, asynchronous with the current shell. Since you absolutely want your unknown commands to run in the current shell, then it is your prefixing code that has to run in a concurrent process. Now I do not really see any other way to avoid the ugliness of concurrently printing on stdout than to wait for your concurrent prefixer to complete, more or less like you did. A variant is to ask socat to handle the cleanup actions for you like this: prefix_with_date() { local P=/tmp/dataorig.$$ socat -u PIPE:${P} SYSTEM:'while read; do echo $(date):\\ $REPLY; done' socatPID=$! until [ -e ${P} ]; do sleep 1; done $@ ${P} wait $socatPID } prefix_with_date seq 5 thank you very much for your explanation! I feel understood and will think about using socat and if I want my script to depend on another program. But at least it became clear that the builtin process substitution is not the solution for me. Regards, Peter Peter
Re: output redirection with process substitution asynchronous?
On Dec 5, 4:45 pm, pk p...@pk.invalid wrote: pjodrr wrote: Hi On Dec 4, 7:58 pm, pk p...@pk.invalid wrote: What's wrong with seq 4 | while read line; do echo $(date): $line; done it creates a subshell uh...where do you think your original (while read line; do echo $(date): $line; done) runs? in my original example the seq 4 runs in the current shell while here the command runs in a subshell. Peter
Re: output redirection with process substitution asynchronous?
pjodrr wrote: in my original example the seq 4 runs in the current shell while here the command runs in a subshell. It would be nice if you explained what it is you're attempting to do, rather than ask for a solution for what you're thinking would do that.
Re: output redirection with process substitution asynchronous?
On Dec 4, 7:46 pm, DennisW dennistwilliam...@gmail.com wrote: This should be in gnu.bash rather than gnu.bash.bug oh, you are right, it's not a bug yet Would this work for you? while read line; do echo $(date): $line $((num++)); done ah sorry, I used the command seq just as an example, it could be any other command that produces output, should have probably written like: $ exec 3 (while read line; do echo $(date): $line; done) $ some command that produces some output 3 Friday, December 4, 2009 4:20:29 PM MET: 1 $ Friday, December 4, 2009 4:20:29 PM MET: 2 Friday, December 4, 2009 4:20:29 PM MET: 3 Friday, December 4, 2009 4:20:29 PM MET: 4 thanks, Peter
Re: output redirection with process substitution asynchronous?
Hello, On Dec 4, 8:18 pm, DennisW dennistwilliam...@gmail.com wrote: It works for me. Does it not for you? If you're asking why not do it, then the answer is why call an external program unnecessarily?. Sorry, by the way, I missed what you were doing with the file descriptor on my first read. What is it that you're trying to accomplish? Are you doing this only to number the lines or is either seq or the while loop a stand-in for something else? the seq was only an example for demonstration. here is another example that shows what I mean: $ exec 3 (while read line; do echo tag: $line; done) $ seq 4 3 tag: 1 tag: 2 tag: 3 tag: 4 $ exec 3 (while read line; do echo $(date): $line; done) $ seq 4 3 $ Sat Dec 5 10:11:25 CET 2009: 1 Sat Dec 5 10:11:25 CET 2009: 2 Sat Dec 5 10:11:25 CET 2009: 3 Sat Dec 5 10:11:25 CET 2009: 4 while in the first example the prompt returns after the command completes, the prompt returns immediately in the second example. thanks for your attention, Peter
Re: output redirection with process substitution asynchronous?
On Dec 4, 9:28 am, pjodrr pjo...@gmail.com wrote: Hello, how can I prefix every line of output of some command with a timestamp? I thought like this: $ exec 3 (while read line; do echo $(date): $line; done) $ seq 4 3 Friday, December 4, 2009 4:20:29 PM MET: 1 $ Friday, December 4, 2009 4:20:29 PM MET: 2 Friday, December 4, 2009 4:20:29 PM MET: 3 Friday, December 4, 2009 4:20:29 PM MET: 4 please note that the prompt ($) returns before the command completes. Why is the process substitution asynchronous? Does anybody know of a better way to accomplish this? Thanks, Peter This should be in gnu.bash rather than gnu.bash.bug Would this work for you? while read line; do echo $(date): $line $((num++)); done
Re: output redirection with process substitution asynchronous?
pjodrr wrote: Hello, how can I prefix every line of output of some command with a timestamp? I thought like this: $ exec 3 (while read line; do echo $(date): $line; done) $ seq 4 3 Friday, December 4, 2009 4:20:29 PM MET: 1 $ Friday, December 4, 2009 4:20:29 PM MET: 2 Friday, December 4, 2009 4:20:29 PM MET: 3 Friday, December 4, 2009 4:20:29 PM MET: 4 please note that the prompt ($) returns before the command completes. Why is the process substitution asynchronous? Does anybody know of a better way to accomplish this? What's wrong with seq 4 | while read line; do echo $(date): $line; done
Re: output redirection with process substitution asynchronous?
On Dec 4, 12:58 pm, pk p...@pk.invalid wrote: pjodrr wrote: Hello, how can I prefix every line of output of some command with a timestamp? I thought like this: $ exec 3 (while read line; do echo $(date): $line; done) $ seq 4 3 Friday, December 4, 2009 4:20:29 PM MET: 1 $ Friday, December 4, 2009 4:20:29 PM MET: 2 Friday, December 4, 2009 4:20:29 PM MET: 3 Friday, December 4, 2009 4:20:29 PM MET: 4 please note that the prompt ($) returns before the command completes. Why is the process substitution asynchronous? Does anybody know of a better way to accomplish this? What's wrong with seq 4 | while read line; do echo $(date): $line; done It works for me. Does it not for you? If you're asking why not do it, then the answer is why call an external program unnecessarily?. Sorry, by the way, I missed what you were doing with the file descriptor on my first read. What is it that you're trying to accomplish? Are you doing this only to number the lines or is either seq or the while loop a stand-in for something else?