Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

copy shell-output to logfile with timestamp prefix on log only ?

138 views
Skip to first unread message

shellguest

unread,
Apr 15, 2013, 5:16:09 AM4/15/13
to
I am trying to solve the following, but after seeing so many examples and articles about redirections none of them worked so far as i wished:

- I have a ready shell script which does things has some main block and functions inside, as usual.

#!/bin/bash

echo "line1"
echo "line2"
echo "line3"

function a
IF clauses
script can also do exits with some echo statements

function b

function c

EOF

- I now need a logfile for everything.
- The log should be plaintext, not going to syslog.
- Log should contain all statement from both STDOUT and STDERR.
- Log should get a timestamp with each line as prefix. `date` : thelogline

- The timestamp should ONLY be in the Logfile, NOT on STDOUT.
- Though STDOUT and STDERR should be on the screen. But NO timestamp as a prefix.

- As i am lazy i would not like to change all the echo statements.
- So i learned about solution to put the brackt arround everything and tee: { } | tee -a logfile.txt
which is not enough, as the timestamp is not there and if i put awk or sed inbetween i have
it also on screen :-(

No need i use tee, it can be also with 'exec >> logfile.txt' at the top, if it can fulfill all the requirements from above somehow ?

Thanks for any suggestions...

Bill Marcum

unread,
Apr 15, 2013, 5:38:01 AM4/15/13
to
On 04/15/2013 05:16 AM, shellguest wrote:
>
> - As i am lazy i would not like to change all the echo statements. -
> So i learned about solution to put the brackt arround everything and
> tee: { } | tee -a logfile.txt which is not enough, as the timestamp
> is not there and if i put awk or sed inbetween i have it also on
> screen :-(
>
> No need i use tee, it can be also with 'exec >> logfile.txt' at the
> top, if it can fulfill all the requirements from above somehow ?
>
> Thanks for any suggestions...
>

Instead of piping to tee, you could pipe to
awk '{"date"| getline mydate; print mydate,$0 > "logfile.txt";
print }'

Kenny McCormack

unread,
Apr 15, 2013, 5:43:27 AM4/15/13
to
In article <b069b080-7014-4b52...@googlegroups.com>,
shellguest <amor...@gmail.com> wrote:
...
>- I now need a logfile for everything.
>- The log should be plaintext, not going to syslog.
>- Log should contain all statement from both STDOUT and STDERR.
>- Log should get a timestamp with each line as prefix. `date` : thelogline
>
>- The timestamp should ONLY be in the Logfile, NOT on STDOUT.
>- Though STDOUT and STDERR should be on the screen. But NO timestamp as
>a prefix.

I solved exactly this problem some time back, using the greatest Unix tool
of them all - Expect.

Here's the Expect script:

--- Cut Here ---
#!/usr/bin/expect --
# Post process the log file with:
# awk '-F\r' '{print $2,$1}' /tmp/typescript | less
log_file -noappend /tmp/typescript
eval spawn $argv
interact -o "\n" { send_user [timestamp -format %c\n] }
--- Cut Here ---

Usage is something like: ./AboveScript ./yourscript

Caveat: Yes, this is the lazy way - and I've since learned how to do it all
in Expect instead of having to post-process with AWK. Still, the above
works, and it is pretty good. Figuring out how to do it more cleanly is
left as an exercise for the reader...

--
A liberal, a moderate, and a conservative walk into a bar...

Bartender says, "Hi, Mitt!"

Kenny McCormack

unread,
Apr 15, 2013, 5:46:47 AM4/15/13
to
In article <kkgi3v$bu3$1...@news.xmission.com>,
Kenny McCormack <gaz...@shell.xmission.com> wrote:
>In article <b069b080-7014-4b52...@googlegroups.com>,
>shellguest <amor...@gmail.com> wrote:
>...
>>- I now need a logfile for everything.
>>- The log should be plaintext, not going to syslog.
>>- Log should contain all statement from both STDOUT and STDERR.
>>- Log should get a timestamp with each line as prefix. `date` : thelogline
>>
>>- The timestamp should ONLY be in the Logfile, NOT on STDOUT.
>>- Though STDOUT and STDERR should be on the screen. But NO timestamp as
>>a prefix.
>
>I solved exactly this problem some time back, using the greatest Unix tool
>of them all - Expect.

I realized after posting that this script will display the timestamps on the
screen as well as in the logfile. I think (but have not tested) that if you
change "send_user" to "send_log", it will put it only in the logfile.

In any case, learn Expect; you'll be glad you did.

--
Religion is regarded by the common people as true,
by the wise as foolish,
and by the rulers as useful.

(Seneca the Younger, 65 AD)

shellguest

unread,
Apr 17, 2013, 8:38:08 AM4/17/13
to
Tthanks, that did it ! It look now like this...


echo "$(date +"%Y%m%d-%H%M%S")> \"$0 $@\"" 2>&1 >>"logfile.txt"
{

...

} 2>&1 | awk '{"date +%Y%m%d-%H%M%S" | getline mydate; print mydate, $ 0>> "logfile.txt"; print} '


Kenny McCormack

unread,
Apr 17, 2013, 9:23:53 AM4/17/13
to
In article <7ce11d6b-4c43-4427...@googlegroups.com>,
Did you look at the Expect based solution at all? Much more compact and
flexible than shell hacks...

--
Given Bush and his insanely expensive wars (*), that we will be paying for
for generations to come, the only possible response a sensible person need
ever give, when a GOPer/TeaBagger says anything about "deficits", is a
polite snicker.

(*) Obvious money transfers between the taxpayers and Bush's moneyed
interests. Someday, we'll actually figure out a way to have a war where the
money just gets moved around and nobody (on either side) gets injured or
killed. That will be an accomplishment of which we will be justly proud.

0 new messages