Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash - Passing value to both console and variable/file

I prepared a simple logger with a function logMETHOD:

function logMETHOD {
    exec 5>&1
    local log

    log="$( "$@" 2>&1 | tee /dev/fd/5; echo ${PIPESTATUS[0]}>/tmp/ssg_retval )"
    local retVal=$(</tmp/ssg_retval)
    rm -f /tmp/ssg_retval

    _LOG_SSG+=$log$'\n'
    return $retVal
}

It is great. It puts output of a passed method into both console (through /dev/fd/5) AND variable log (through /dev/fd/1) at the same time. It also returns value returned by the passed method. Eg.

logMETHOD scp test.txt ala@host:/direcotry

It works, but unfortunately I can't use it, cause I have no access to /dev/fd/5 on the server I need to use. "Permission denied".

I tried to use a temporary file instead of a file descriptor but it breaks dynamic output like scp progress bar.

Is there any way to implement this functionality without using file descriptors?

like image 340
aerion Avatar asked Jan 27 '26 08:01

aerion


1 Answers

It appears that you have permission to use &5 but not to use /dev/fd/5. You could try to trace down why by running ls -l /dev/fd/5 and comparing that to the user+group that your script runs under. It may be simpler, though, to just try replacing:

tee /dev/fd/5;

With:

tee >(cat >&5)
like image 117
John1024 Avatar answered Jan 29 '26 07:01

John1024



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!