Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Eliminating Append in .ksh Scripts 1

Status
Not open for further replies.

tbohon

Programmer
Apr 20, 2000
293
US
I am rewriting a slew (400+) of scripts and one of the things I'm doing is trying to clean them up a bit. The biggest offender in these scripts - in terms of 'garbage' - is that every line the original writer wanted to go to the log file is of the form

Code:
print "some status message" >> $logfile

I have been told - by those who know more than I do about this kind of stuff - that I can eliminate the '>> $logfile' piece above by adding a single command to the beginning of the script ... but when I asked what that command was, the email flow stopped.

Does anyone have a suggestion on how to eliminate this visual garbage? As long as I have to rewrite them, I want to do it 'right' and cleaning up the script by eliminating hundreds (in some cases) of these lines would go a long way to making them more readable.

Scripting is being done in .ksh on an AIX 5.2 box.

Thanks in advance.

Tom

"My mind is like a steel whatchamacallit ...
 
In your ksh man page have a look at the exec builtin.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Something like

exec 2>${logfile}
exec >&2

Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
Thanks to both of you. I don't have a man page entry for 'exec' but I'm pretty sure that's the method that was recommended to me. Am researching it now.

Appreciate it!

Tom

"My mind is like a steel whatchamacallit ...
 
Hi,

This is a korn shell solution by using coprocess :
put this code at the beginning of your script. All print and echo instructions will write on the display and the log file

Code:
#! /bin/ksh
...
LOG=/some/path/to/file.log
# If fd 1 (stdout) is still attached to a terminal
if [ -t 1 ]
then
        tee $LOG >/dev/tty |&
        # redirect stdout and stderr to the coprocess
        exec 1>&p 2>&1
else
        # script run from cron or in background
        # redirect stdout and stderr to the log file
        exec 1>$LOG 2>&1
fi

Ali
 
I don't have a man page entry for 'exec'
man ksh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top