ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
hello,
I have 2 scripts. at the script1 I have this line
Code:
".....
./script2 >> $outputfile
.."
at script2 I print a lot and I need all to be at the stdout(because it will go out to $outputfile), and at some point at script2 I need to use all I have printed till now at script2.
cause I have at script2 line like this
Code:
"....
echo what I have printed till now at script2 | script3
..."
I tried something like that (the first line is at the beginnig of the script2, and the 2 last lines are at the end of script2, so it will go to $outputfile)
what's wrong with that(showed me empty lines at $outputfile)?
Is there a way where I can copy the stdout to tmp5(that it will be both on stdout and tmp5) - its the best solution for me.
I think the problem is that you still have tmp5 open for writing when you cat it. Maybe try
Code:
# 3 = old stdout, 4 = old stderr, stdout and stderr to tmp5
exec 3>&1 4>&2 1>tmp5 2>&1
....
# close fds for tmp5, restore the original stdout and stderr
exec 1>&- 2>&- 1>&3- 2>&4-
<tmp5 script3
...
cat tmp5
rm tmp5
Possibly you can use tee if it's okay to run script3 in parallel, although since "...." will go in a subshell it might give you problems depending on what's in "...":
Code:
(....
) | tee >(script3)
...
Actually, would
Code:
(....
exec 1>&- 2>&-
...
) | tee >(script3)
avoid the subshell problems? There's even more concurrency, though.
Can you plz explain me what does the parameters(and &) mean?
Should I put this line together? exec 1>&- 2>&- 1>&3- 2>&4-<tmp5 script3
or it whould be 2 lines?
and what does it means <tmp5 script3
Thanks !
Can you plz explain me what does the parameters(and &) mean?
See Redirections, please ask specific questions if anything is unclear.
Quote:
Should I put this line together? exec 1>&- 2>&- 1>&3- 2>&4-<tmp5 script3
or it whould be 2 lines?
2 lines.
Quote:
and what does it means <tmp5 script3
It means the same as cat tmp5 | script3, it's more usual to write script3 <tmp5, but I wrote it like that to emphasize the similarity to the cat version.
Quote:
Originally Posted by konsolebox
We could probably just use { } instead of ( ) for that. After all stdout returns back to original when code is no longer around { }.
But it would still be in a subshell because it's part of a pipeline so it makes no difference, right?
Quote:
This one causes bad file descriptor error.
Hmm, I must have been thinking that the second part of the script doesn't have output, but that makes no sense, oops.
But it would still be in a subshell because it's part of a pipeline so it makes no difference, right?[/CODE]
Since the context of it doesn't need to be placed on a subshell unlike those placed after pipe (|), it won't. This would still echo "Was in current shell.":
Code:
{ :; A="Was in current shell."; } > >(:); echo "$A"
Or
Code:
{ :; A="Was in current shell."; } | tee >(:); echo "$A"
Now that's odd. With pipe you don't need to fork twice to create two processes on the background and share each other. Perhaps it's necessary to avoid errors or conflicts in other implementations of the shell.
Code:
I'm not sure if we are talking about the same "it"?
The part of the code or the context of the script that doesn't run in a subshell by default like those in { }.
Since we can't use pipes to maintain the code not running on a subshell we could just use process substitution twice:
Code:
{
...
} > >(exec tee >(script3)) 2>&1
Last edited by konsolebox; 08-04-2013 at 09:56 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.