Operating System - HP-UX
1821588 Members
3675 Online
109633 Solutions
New Discussion юеВ

Redirecting output of the screen to a file

 
SOLVED
Go to solution
Shivkumar
Super Advisor

Redirecting output of the screen to a file

Hi,

i want to redirect output of the some of commands and screen to a file. I used the command
$somecommand > outputfile.

it doesn't work for the type of application i am using.

is there some other way to redirect the output on the screen in some file ?

Thanks in advance.
Shiv
13 REPLIES 13
Patrick Wallek
Honored Contributor
Solution

Re: Redirecting output of the screen to a file

Try:

$ somecommand > outputfile 2>&1

If your command is sending output to STDOUT & STDERROR then the above will take care of both.
Christian Tremblay
Trusted Contributor

Re: Redirecting output of the screen to a file

# somecommand | tee will copy all the output from "somecommand" to

man tee
Pete Randall
Outstanding Contributor

Re: Redirecting output of the screen to a file

You can also use x commands to capture, share and print xwindows:

xwd -nobdrs -out savewindow
xwud -in savewindow -display $DISPLAY

Do a man on xwd, xwud and xpr for details.


Pete

Pete
James R. Ferguson
Acclaimed Contributor

Re: Redirecting output of the screen to a file

Hi Shiv:

Your redirection assumes that what you are interested in capturing comes from STDOUT (file descriptor #1). Well-behaved Unix tools produce output to STDOUT, but write *errors* to STDERR (file descripter #2).

Consider:

# cp 1> /dev/null

...still shows the usage of the command on your terminal; but:

# cp 2> /dev/null

...relagates the text to the bit-bucket.

If you want to capture both STDOUT and STDERR in a file, do:

# mycommand > filename 2>&1

That means redirect STDOUT to 'filename' and then redirect STDERR (file descriptor #2) to the same place as file descriptor #1 (STDOUT). The order is very important.

Regards!

...JRF...
Shivkumar
Super Advisor

Re: Redirecting output of the screen to a file

the script i am using is interactive one and is asking for username and password. probably this could be
the reason it is not able to redirect the output to a file. i used $somecommand | tee
but it didn't work.
spex
Honored Contributor

Re: Redirecting output of the screen to a file

Hi Shiv,

Depending on your application, you may find it difficult or impossible to get usuable output redirected to a file. This is especially true of curses/ncurses-based programs or interactive programs.

The 'script' or 'screen' commands may be of some use, but for certain programs, you will need to take screenshots or scrollback buffer dumps. For example, in PuTTY, right-click on the title bar and choose "Copy All To Clipboard". Or drag your cursor over the area of interest to select and copy. Then paste into vim/EditPlus/notepad.

PCS

PCS
James R. Ferguson
Acclaimed Contributor

Re: Redirecting output of the screen to a file

Hi (again) Shiv:

To add to my first examples, you can do this, too:

# cp 2>&1 | tee myoutput

Now you see STDERR on both your terminal and in the file "myoutput".

All these examples assume that its file descriptor 1 and 2 that the I/O of interest occurs upon. This is standard and a safe first assumption.

Regards!

...JRF...


Sheriff Andy
Trusted Contributor

Re: Redirecting output of the screen to a file

As spex said, you can try the script command.

script /tmp/logfile.txt
su shiv
login: shiv
password: xxx
bdf
ll
exit ( exits out of su session)
exit (exits out of script command and saves the file).
Shivkumar
Super Advisor

Re: Redirecting output of the screen to a file

Sorry to ask a very stupid question but need to get it clarified with the gurus here. In 2>&1, what is the purpose of &
James R. Ferguson
Acclaimed Contributor

Re: Redirecting output of the screen to a file

Hi Shiv:

The syntax '2>&1' means that file descriptor 2 (STDERR) is duplicated to file descriptor 1 (STDOUT). That is, STDERR is treated as a duplicate of STDOUT. Output which is directed to STDERR (FD #2) is written to the same place as STDOUT (FD #1).

Regards!

...JRF...
Shivkumar
Super Advisor

Re: Redirecting output of the screen to a file

Thanks James!! Actually i wanted to know the purpose of "&" here. Is it part of the syntax with FD#1 or has some special meaning ?

Best Regards,
Shiv
James R. Ferguson
Acclaimed Contributor

Re: Redirecting output of the screen to a file

Hi Shiv:

The ampersand ('&') is the shell syntax that means to duplicate. We are duplicating a file descriptor using the underlying system call 'dup(2)'.

Regards!

...JRF...

Shivkumar
Super Advisor

Re: Redirecting output of the screen to a file

Thanks James for clarifying!!

Warm Regards as always.
Shiv