set -x within script and capture as a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting set -x within script and capture as a file
# 1  
Old 08-19-2011
set -x within script and capture as a file

Okay, I've been working on a script for providing information on the progress of a backgrounded ditto command. Thanks to google and a lot of searching I've resolved all but one very odd error. At this point, I want to use xtrace (set -x) to try to uncover the issue. I have found several examples but none have quite helped me to acheive what I'm trying to do.

The idea is to be able to enable or disable xtrace via a script option (getopts), and capture the results in a file. What I've run into is that while I can create the file with no problem, all the debug info is displayed in the shell and not sent to the intended file. Is it possible to do this in this manner, or will I need to pass it from the command line?

Just in case it makes a difference, I am running OS X (10.5-10.7).

Thanks.

Code:
#!/bin/bash
while getopts "x" opt; do

	case $opt in
                x)     debugON=1;;
		\?) echo "Invalid option: -$OPTARG" >&2;;
	esac
done

if [ $debugON = 1 ]; then
     today=`date "+%m%d%Y"`
     set -x 2> ~/Desktop/xtrace$today
else
     set +x
fi

# 2  
Old 08-19-2011
Quote:
Originally Posted by reid
Code:
     set -x 2> ~/Desktop/xtrace$today

That will only redirect standard error for that one set statement and nothing else. If you want to redirect the standard error for the current execution environment you need to use exec.
Code:
exec 2>~/Desktop/xtrace$today
set -x


Note that all subsequenct commands will also inherit that as standard error. If that is undesirable, and you'd prefer for all commands other than the shell's tracing to continue to use the original stderr's destination, then you'll need to store it and override it per command (tedious and repetitious, but as far as I know the only way to do it).
Code:
exec 3>&2 2>~/Desktop/xtrace$today
set -x
date --bad-option 2>&3


You'd also need to store the original stderr if at some point you'd want to "undo" the tracing.
Code:
set +x
exec 2>&3 3>&-

Regards,
Alister

Last edited by alister; 08-19-2011 at 06:25 AM..
# 3  
Old 08-19-2011
Perfect, that was exactly what I was looking for!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help on script to capture info on log file for a particular time frame

Hi I have a system running uname -a Linux cmovel-db01 2.6.32-38-server #83-Ubuntu SMP Wed Jan 4 11:26:59 UTC 2012 x86_64 GNU/Linux I would like to capture the contents of /var/log/syslog from 11:00AM to 11:30AM and sent to this info via email. I was thinking in set a cron entry at that... (2 Replies)
Discussion started by: fretagi
2 Replies

2. Shell Programming and Scripting

Script to capture string in a log file

Dear all, I have a log file to be analysed. this log file contains vaiours lines of code starting with date timestamp. if my search string is exception then that resepective log statement starting from the date is required. example: 2014/10/01 16:14:44.459|>=|E|X|19202496|2832|... (5 Replies)
Discussion started by: shravee
5 Replies

3. Shell Programming and Scripting

AWK Script to Capture Each Line of File As Variable

Hi All, I'm working on creating a parts database. I currently have access to a manufacturer database in HTML and am working on moving all of the data into a MySQL db. I have created a sed script that strips out the HTML and unnecessary info and separates the script into one line for each field.... (3 Replies)
Discussion started by: dkr
3 Replies

4. Shell Programming and Scripting

looking for help on script to capture file permission.

Hi Guys, I'm a DBA and need help on shell scripting. My Oracle Database is sitting on HP-UX machine. Anyone has a script that can spool out permission of all oracle binary files in the below directory: /opt/ora10g/oracle/ Format to be spooled out : chmod <exisiting permission> filename... (10 Replies)
Discussion started by: ciaciachew
10 Replies

5. Shell Programming and Scripting

shl script capture a file

need to be able to capture a file with the following conditions: The filenames are, for example, 3526_332840.dat, where 3526 is constant, and 332840 is a sequential number which is always a couple hundred greater than the previous day's file. I want to be able to change this script to acoomplish... (1 Reply)
Discussion started by: rechever
1 Replies

6. Shell Programming and Scripting

script to capture content of deleted file

I need to capture the content of a file before its being deleted. This file gets deleted immediately after it is created. I use the below shell command in the command prompt, but I'm not getting the desired result. bash-3.00# while true; do cat file* > tempfile; done; What I'm trying here... (5 Replies)
Discussion started by: bjawasa
5 Replies

7. Shell Programming and Scripting

Capture Shell Script Output To A File

Hi, I am running a shell script called dbProcess.sh which performs shutdown and startup of various Oracle instances we have.At the time of execution the script produces the following output to the command line window $./dbProcess.sh stop #### Run Details ###### Hostname : server-hop-1... (4 Replies)
Discussion started by: rajan_san
4 Replies

8. Shell Programming and Scripting

Capture two set of data to same line...simple

I would like to capture output from two commands to a test file on the same line... I want to get a file with all Applications and the Version of it...here are the two commands I use to get the output. To get Application list I use ls -1 /Applications/ |grep .app >>... (3 Replies)
Discussion started by: elbombillo
3 Replies

9. Shell Programming and Scripting

Script to capture new lines in a file and copy it to new file

Hi All, I have a file that gives me new line/output every 5 minutes. I need to create a script that capture new line/output besides "IN CRON_STATUS", in this case the new output is "begin ------ cron_status.sh - -----------". I want this script to capture the line starting from "begin ------... (0 Replies)
Discussion started by: fara_aris
0 Replies
Login or Register to Ask a Question