[kwlug-disc] Capturing output in scripts

unsolicited unsolicited at swiz.ca
Tue Aug 26 21:40:27 EDT 2014


That's the intent.

All output to land in same log file for later (if necessary) analysis, 
let alone e-mail'ing to person responsible for monitoring. vs, say, 
stdout to logfile, leaving stderr still coming out to screen (which 
isn't present in a cron job).

This doesn't address the problem of misaligned output, output to stderr 
being accompanied by calls to flush(), unlike stdout, but that's another 
story, and not one for which I've ever heard of a solution for (within a 
script). [Let alone how to tell which is which when looking at the log 
file later.]

Within code one could call flush(), and I have scripts with things like 
log2screen(), log2log(), and log2both() = {log2screen $1; log2log $1;}, 
so I could call flush within those.

But it doesn't address within a script calls to external programs. e.g.

log2both( echo $1 > /tmp/logfile )

log2both "I reached this point in the script."
extprog > /tmp/logfile 2>&1
log2both "I've exited extprog now."

Khalid's original point and question was essentially how to avoid the '> 
/tmp/logfile 2>&1' on every line in a script. As originally posed, he 
had the group of commands encapsulated within ()'s.

His later further point was essentially wanting the script itself to 
manage all logging, including where it goes, so that the cron call 
doesn't have to. Without cron calling a script that has to call another 
created script with the  '> /tmp/logfile 2>&1' in the first script.

Actually Khalid ... isn't that the answer? Put it within a function in 
your script?

myfunc > /tmp/logfile 2>&1 ?

myfunc
{
     stuff
     stuff
     stuff
}

and/or isn't the answer to enclose in {}'s not ()'s? (Or is that a bash 
and not a sh thing?)


On 14-08-26 08:53 PM, Bob Jonkman wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> On 14-08-26 05:47 AM, Giles Malet wrote:
>> On 14-08-21 03:27 PM, William Park wrote:
>>>
>>> exec [...] 2>&1 1>/tmp/logfile
>>
>> Order is significant as this is evaluated left to right, so you
>> need to redirect 1 before 2, else 2 is left pointing at what 1
>> *originally* was. Thusly:
>>
>> exec 1>/tmp/logfile 2>&1
>
>
> So if it's evaluated left to right doesn't _your_ incantation mean
> "Send STDOUT to /tmp/logfile, then STDERR to STDOUT (which is now
> /tmp/logfile)"?
>
>
> (Or is that what you intended, and I've lost track of the orginal
> question from lack of context?)
>
> - --Bob.
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.11 (GNU/Linux)
> Comment: Ensure confidentiality, authenticity, non-repudiability
>
> iEYEARECAAYFAlP9K/MACgkQuRKJsNLM5eo3FQCffNgpc+DhaTSwXMtU5ImY0aUA
> lFQAn1EqQJvmU8qMVEfz9kQi01FuQj+d
> =RQcN
> -----END PGP SIGNATURE-----
>
>
> _______________________________________________
> kwlug-disc mailing list
> kwlug-disc at kwlug.org
> http://kwlug.org/mailman/listinfo/kwlug-disc_kwlug.org
>





More information about the kwlug-disc mailing list