I want to forward all my stdout and errout to a logger script that log them with some extra info such as dateTime and the script caused the error. After some googling I finally wrote my bash program in this manner:
I added these two lines in my main script:
exec 1> >(xargs ./doLog)
exec 2> >(xargs ./doLog)
The ./doLog is somehow like this:
if [ -n $1 ]
then
echo -n "Jaky: `date`: $@ " >> $Log_File
fi
So it takes every input from 1> and 2> and echo it to my log file.
The problem is that for some reason I need to convert the doLog script to a function that perform the same result. but I don’t know how to pass the input of exec 1 and 2 to a function. I tried xargs and other file descriptor but I couldn’t get the result.
Another problem is that the log passed to the doLog doesn’t contain end of line character, So all output appear connected to each other.
Some help please.
You can’t use
xargsfor functions. That’s why you need to read tha data in the function direct from stdin. Do it usingread. There is a small example below.Function:
Usage: