I often write KSH shell scripts that follow the same pattern:
- (1) retrieve output from one or more command
- (2) format it using grep|cut|awk|sed and print it to the screen or to a file
In order to do that, I often store the output of (1) in a tempfile, and then do the formatting in (2) on that file.
Take that code for instance:
TMPFILE=file.tmp
# If tmpfile exists rm it.
[ -f $TMPFILE ] && rm -f $TMPFILE
for SERVICE in $(myfunc); do
getInfo $SERVICE > $TMPFILE # Store raw output in the TMPFILE
# I retrieve the relevant data from the TMPFILE
SERV_NAME=$(head -1 $TMPFILE | sed -e 's/ $//')
SERV_HOSTNAME=$(grep HOSTNAME $TMPFILE | cut -d "=" -f2)
SERV_ARGS=$(grep Arguments $TMPFILE | cut -d ":" -f2)
print $SERV_NAME $SEP $SERV_HOSTNAME $SEP $SERV_ARGS
rm -f $TMPFILE #rm the TMPFILE in vue of next iteration
done
Is there a way, using pipes, redirections and whatnots, to avoid writing a file to disk each time?
If it helps, I'm using ksh Version M-11/16/88i
PATH
or other shell or environment variables.TMPFILE
may be fine, butTMPDIR
is special, so do you really want to be walking that tightrope? – jw013 Aug 24 '12 at 15:25