Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a script that issues a lot of commands. I wish to save the output of this script (Errors and stdout) to a file, but I also want the actual commands that were executed to be saved to the file as well (as displayed by bash -x)

I've tried:

bash -x script.sh > log.txt

to no avail

Any help is appreciated. Thanks

share|improve this question
    
Can you get some idea from gnu.org/software/bash/manual/html_node/Redirections.html ? –  Pothi Oct 29 '13 at 2:35
    
have you tried piping to tee? –  dyasny Oct 29 '13 at 3:13

2 Answers 2

I had tested the following its giving what you are looking for, The command below will log stdout/stderror to the file output.txt

bash -x script.sh &> output.txt

Sandeep

share|improve this answer

I believe your problem is that the output from "bash -x" is sent to stderr while your script's output is being sent to stdout.

When you executed:

   bash -x script.sh > output.txt

...the normal output from your script was sent to the redirected file and the script commands (from the -x) continued to be displayed on your terminal screen.

In order to send stderr and stdout to the same stream, you need to duplicate one atop the other. This is done using shell file descriptor dupe syntax "e.g. 2>&1"), like so:

   bash -x script.sh > output.txt 2>&1

This syntax duplicates file descriptor #2 (stderr) over the top of file descriptor #1 (stdout)... which results in both streams getting redirected to your output file.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.