LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-03-2013, 02:11 PM   #1
kobigeu
LQ Newbie
 
Registered: Aug 2013
Posts: 6

Rep: Reputation: Disabled
shell script - copy stdout to file


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)
Code:
exec &> tmp5
....
cat tmp5 | script3
...
cat tmp5
rm tmp5
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.
 
Old 08-03-2013, 05:56 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,774

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
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.
 
Old 08-04-2013, 12:39 AM   #3
kobigeu
LQ Newbie
 
Registered: Aug 2013
Posts: 6

Original Poster
Rep: Reputation: Disabled
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 !
 
Old 08-04-2013, 07:48 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by ntubski View Post
since "...." will go in a subshell
Code:
(....
) | tee >(script3)
We could probably just use { } instead of ( ) for that. After all stdout returns back to original when code is no longer around { }.
Code:
{
    ...
} 2>&1 | tee >(script3)
Quote:
Originally Posted by ntubski View Post
Code:
exec 1>&- 2>&-
This one causes bad file descriptor error. I think it's no longer necessary.
Code:
( exec >&-; echo -; ) > >(cat)
@kobigeu Try to read more about Redirection and Process Substitution in the bash manual.

Last edited by konsolebox; 08-04-2013 at 07:49 AM.
 
Old 08-04-2013, 08:52 AM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,774

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by kobigeu View Post
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 View Post
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.
 
Old 08-04-2013, 09:05 AM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by ntubski View Post
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"
 
Old 08-04-2013, 09:15 AM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,774

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by konsolebox View Post
Since the context of it doesn't need to be placed on a subshell
I'm not sure if we are talking about the same "it"? The second example echoes nothing, which I think is what applies.
 
Old 08-04-2013, 09:43 AM   #8
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Print stdout and stderr to screen + log file and catch any errors in shell script idaham Linux - General 3 03-28-2013 06:43 AM
How to copy one file and to multiple directories using shell script? cebuy Programming 8 03-31-2009 07:04 PM
Shell script - stdout & stderr to terminal and file jantman Linux - Software 1 12-07-2006 04:34 PM
Shell script to copy file name with part of directory Transition Linux - General 5 01-18-2005 05:40 PM
shell script to copy lines from a file Warmduvet Programming 2 09-14-2004 09:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration