Operating System - Linux
1753741 Members
4408 Online
108799 Solutions
New Discussion юеВ

setting a log_it function

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

setting a log_it function

Hi all,

I want to use a log_it function as below:

LOGFILE=${DIR}/${HOST}_sapno.log



function log_it {

printf "%s\n" "$*" >> $LOGFILE

}

how can I get the log_it function to record the output of my commands:

eg:

# |log_it

obviously this doesnt work, I have used this function in the past but cant find my notes.

Thanks for any help

chris
hello
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor
Solution

Re: setting a log_it function

Hi Chris:

#!/usr/bin/sh

typeset LOGFILE=/tmp/mylog
function log_it {
printf "%s\n" "$*" >> $LOGFILE
eval "$*" >> $LOGFILE

}

log_it date

Regards!

...JRF...
lawrenzo_1
Super Advisor

Re: setting a log_it function

will give it a go ...

Thanks James.
hello
Peter Nikitka
Honored Contributor

Re: setting a log_it function

Hi,

to get your output to a logfile via
# |log_it

you'll have to read stdin in your function:

function log_it {
cat >>$LOGFILE
}

or - to get the output in the logfile and on screen:
function log_it {
tee -a $LOGFILE
}

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Arturo Galbiati
Esteemed Contributor

Re: setting a log_it function

Hi,

log_it () {
awk '{print}' $@
}

echo "jkjkjkjkjk"|log_it
jkjkjkjkjk

this runs fine on my Hp-UX 11i.
HTH,
Art
lawrenzo_1
Super Advisor

Re: setting a log_it function

thanks guys
hello