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

We have scripts of the following nature (in cron)

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

Now there is a way by which with the help someScript.shI can find out which file the output came to?

The script sends an email with a resume. At the same time, I would like to mention that the details can be found in this and that output file - in the letter.

I know the construction if [ -t 1 ]for detecting stdout, etc., but how to get the name of the output file? Please note that I want this to be shared so that someone can change the output file in cron and the script does not need to be changed.

+5
source share
2 answers

The simplest thing I could think of is that:

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

$$ - PID script ( script). unix/proc/[pid] - -, [pid].

/proc/[pid]/fd - , . fd/0, fd/1 - script ..

readlink tty, .

, , - , , ! , std (2).

(script.sh 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]
+9

, ( ), .

cron :

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

. > 2>&1 ( stdout/stderr) logfile.

someScript.sh :

LOGFILE=$1
exec &>${LOGFILE}

, :

"output details could be found in ${LOGFILE}"
+4

All Articles