Linux file id - getting current redirect stdout file?

I am trying to get the current stdout redirection and have some problems.

I have a script that always runs with a stdout redirect, i.e.

myscript.sh > /tmp/output.log

In myscript.sh, I need to find out to which file it is being output.

I am trying currently (not working):

logfile=$(readlink -f /proc/self/fd/1)

This is, for example, the output of logfile = / tmp / sflq.r3f. I need to find instead that it will be /tmp/output.log

Is it possible?

I use the korn shell if that matters ...

Thank!

+4
source share
2 answers

$() (, , ksh - tempfile, ksh, -, , ) readlink.

$(), stdout - ( ksh).

stdout - :

{ logfile=$(readlink -f /proc/self/fd/3); }  3>&1
# my ksh 93 needs the { ;} -- dash, and zsh don't 
echo "The logfile is: $logfile"

:

./myscript.sh > /tmp/output.log
echo OUTPUT.LOG
cat /tmp/output.log

:

OUTPUT.LOG
The logfile is: /tmp/output.log

- , :

echo -n "The logfile is: "
readlink -f /proc/self/fd/1 #goes straight to stdout so no need to capture
+4

, , , /proc/*self*/fd. , self - /proc/$$/fd script ...

(: $(readlink …) .)

0

Source: https://habr.com/ru/post/1658977/


All Articles