Redirect stdout and stderr to Function

后端 未结 6 1276
滥情空心
滥情空心 2020-12-25 14:07

I need help sending the output (stdin and stdout) from system commands to a bash function, while still accepting input from arguments. Something like the example that follow

6条回答
  •  攒了一身酷
    2020-12-25 14:44

    It's an old thread.. but I have used it to help me write a log function that will output also multiple lines of a command output:

    # Defines function to grab a time stamp #
    get_Time () { Time=$(date +%Y-%m-%d\ %H:%M:%S) ; }
    
    write_Log()
    {
    get_Time
    if [ -n "${1}" ]; then         # If it's from a "" then set it
        IN="${1}"
        echo "${Time} ${IN}" | tee -a ${log_File}
    else
        while read IN               # If it is output from command then loop it
        do
            echo "${Time} ${IN}" | tee -a ${log_File}
        done
    fi
    }
    

提交回复
热议问题