Operating System - HP-UX
1827855 Members
1525 Online
109969 Solutions
New Discussion

Re: how do I redirect output for set -x

 
SOLVED
Go to solution
Richard Mertz
Regular Advisor

how do I redirect output for set -x

I would like to send all the output generated by a script to a file. It appears that the output from set -x is not either stderr or stdout, so the normal 2>&1 >/tmp/abc doesn't send trace info. Anyone know how to do this?
What anybody thinks of me is none of my business.
10 REPLIES 10
Ian Dennison_1
Honored Contributor

Re: how do I redirect output for set -x

he he he! My soapbox issue!

Theres a 3rd output stream (called Std-something). Undocumented of course.

"2>&1" does not pick it up, but "|tee -a /tmp/abc" probably will. Try it and see!

Ancient Proverb - "Give a programmer a well-defined set of standards, and they shall find a way to break them!"

Share and Enjoy! Ian
Building a dumber user
Michael Tully
Honored Contributor

Re: how do I redirect output for set -x

Hi,

The easiest way is to start a typescript file.

# script /tmp/output
# run_my_script.sh

The output will go to both the screen and the /tmp/output file.

HTH
Michael
Anyone for a Mutiny ?
James R. Ferguson
Acclaimed Contributor

Re: how do I redirect output for set -x

Hi Richard:

#!/usr/bin/sh
set -x
date
echo hi
echo bye

Now:

./mysh 2> ./mysh.debug.debug

...directs stderr to the .debug file...

...and:

/tmp/tester 2> /tmp/tester.debug 1>&2

...directs both stderr and stdout to the .debug file.

Regards!

...JRF...
Tony Contratto
Respected Contributor

Re: how do I redirect output for set -x

Hi Richard,

Not sure if this is you problem but here goes...

In your question you have:
"2>&1 >/tmp/abc"

This will NOT send stderr and stdout to the file /tmp/abc

">/tmp/abc 2>&1" WILL send both stderr and stdout to the file /tmp/abc

Order does matter.

Hope this helps...
Tony
got root?
Richard Mertz
Regular Advisor

Re: how do I redirect output for set -x

More info for you guys

I already have a wrapper for my scripts. Essenentially the wrapper does:

daScript |tee -a logfile 2>&1

contents of daScript

set -x
echo abc
echo 123
echo easy as

The trace does not end up in the logfile.
What anybody thinks of me is none of my business.
Ian Dennison_1
Honored Contributor

Re: how do I redirect output for set -x

All,

I have just tested the following script,...

#!/usr/bin/hs

# Comment - wrong shell is deliberate
bdf

# End Script

By running '/tmp/test1 2>&1 |tee -a /tmp/aa' picks up the error in the file /tmp/aa.

By running '/tmp/test1 |tee -a /tmp/aa 2>&1', no output is displayed in the /tmp/aa file.

Running 11.11 (64bit), Patch Dec 2001, on a N4000.

Try reversing the order of 'tee' and '2>&1' as in the first example in this posting.

Share and Enjoy! Ian
Building a dumber user
Ian Dennison_1
Honored Contributor
Solution

Re: how do I redirect output for set -x

Sorry, wrong test script.

However, when I put a 'set -x' command in a simple script, the "2>&1 |tee -a /tmp/logfile" command does capture the debug output in the file.

Share and Enjoy! Ian
Building a dumber user
James R. Ferguson
Acclaimed Contributor

Re: how do I redirect output for set -x

Hi Richard:

Is this what you want?:

The wrapper:

#!/usr/bin/sh
/tmp/tester 2>&1|tee -a /tmp/tester.debug

The script (/tmp/tester):

#!/usr/bin/sh
set -x
date
echo hi
echo bye
exit 0

NOW: The output (stderr & stdout) appear both on your terminal *and* in /tmp/tester.debug:

+ date
Wed Jan 29 20:01:38 EST 2003
+ echo hi
hi
+ echo bye
bye
+ exit 0

Regards!

...JRF...
Bill Hassell
Honored Contributor

Re: how do I redirect output for set -x

The reason that 2>&1 >/tmp/abc doesn't work is that redirection occurs left to right, so 2>&1 says:

send stderr to stdout as currently defined

and >/tmp/abc says: send stdout to /tmp/abc

but stderr was connected to stdout *before* it was redefined. So stderr goes to whatever stdout used to be.

The correct order is: redirect stdout, then redirect stderr to whatever stdout has become:

sh -x script_name >/tmp/abc 2>&1

I debug scripts this way by piping the stderr and stdout into more as in:

sh -x script_name 2>&1 | more

Note that -x is effective for the main body of the script and disappears inside functions. Also note that stderr (trace output) may not be perfectly shuffled into the stdout lines...shell timing issues.


Bill Hassell, sysadmin
Richard Mertz
Regular Advisor

Re: how do I redirect output for set -x

Thanks guys!

Richard
What anybody thinks of me is none of my business.