Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way in a shell script to figure out where its output is redirected?

Tags:

bash

shell

We have scripts of following nature (in cron)

someScript.sh > /tmp/cronlog/somescript.$(date +%Y%m%d).log 2>&1

Now is there a way by which with in someScript.sh I can figure out what file the output has gone in to?

The script sends email with summary. At the same time I would like to mention that details could be found in so and so output file - with in the email.

I am aware of the construct if [ -t 1 ] to detect stdout etc but how to get the output file name? Note that I want this to be generic so that some one can change the output file in cron and the script does not need to be modified.

like image 416
ring bearer Avatar asked Apr 04 '12 15:04

ring bearer


People also ask

Which command will redirect who output?

The >> shell command is used to redirect the standard output of the command on the left and append (add) it to the end of the file on the right.

What does 1 >& 2 mean in shell script?

1>> and 2>> are redirections for specific file-descriptors, in this case the standard output (file descriptor 1) and standard error (file descriptor 2). So the script is redirecting all "standard" messages to ${jobout} and all error messages to ${joberr} .

How can I see the output of a shell script?

STDOUT - the standard output of the shell. By default, this is the screen. Most bash commands output data STDOUT to the console, which causes it to appear in the console. The data can be redirected to a file by attaching it to its contents using the command >> .

Which command displays output on screen as well redirects output to other file?

The '>' symbol is used for output (STDOUT) redirection. Here the output of command ls -al is re-directed to file “listings” instead of your screen.


2 Answers

The simplest thing I could think is that:

readlink -f /proc/$$/fd/1

$$ is the PID of the script (inside the script). On most unix systems, /proc/[pid] is the pseudo-directory containing info for process [pid].

/proc/[pid]/fd is a directory containing a list of symlinks for the open file-descriptors of the process. fd/0 is input, fd/1 is the output of the script, etc.

readlink then gives you the target file or tty if you don't redirect the output.

Of course, if you want to display it, you have to display it somewhere else than standard ouput, or it will be redirected! To debug, try the std error (2).

Various callings give those results on my box (script.sh just calls readlink -f /proc/$$/fd/1 >&2)

# ./script.sh
/dev/pts/0

# ./script.sh > /var/tmp/foo
/var/tmp/foo

# ./script.sh | more
/proc/12132/fd/pipe:[916212]
like image 142
huelbois Avatar answered Nov 15 '22 09:11

huelbois


Rather than trying to find a hack (and that too platform dependent) its better to take a slightly different approach here.

Set your cron job like this:

someScript.sh /tmp/cronlog/somescript.$(date +%Y%m%d).log

i.e. without and > or 2>&1 (stdout/stderr streams redirections) and just pass an argument with the desired logfile name.

Now inside someScript.sh redirect streams to your log file like this:

LOGFILE=$1
exec &>${LOGFILE}

And finally you can then message your clients that:

"output details could be found in ${LOGFILE}"
like image 42
anubhava Avatar answered Nov 15 '22 09:11

anubhava