bash scripting: buffer output lines instead of directly store them into a file line by line?

Hi,
i wrote a little bash script that runs a few commands (like awk calls with formated [printf()] output) and directly store their output into a file, similar like this:


#!/bin/bash
OUTFILE="filename"

awk '/example/ {printf("   %8.2f  %d
", $5, $2)}' > $OUTFILE

# do something

awk '/another example/ {printf("   %4.1f  %d
", $1, $2)}' > $OUTFILE

# and so on

this works but now i realized that i would like to add an option to directly output to the standard output instead.
But i do not want to add much redundancy, like adding if/else statements for each command, separating nearly identical lines with the only different between them, that one has a: “> $OUTFILE” in the end and the other do not!

My idea is: instead of redirecting the output of the commands to $OUTFILE i would like to output them to some kind of buffer (i do not know if this is possible thought) and then in the end i just add one if/else statement deciding to output the buffer to $OUTFILE or STDOUT.

Is this possible? Sure i could output everything to a temporary file first and then in the end either just cat the output and delete the file or just rename it to $OUTPUT, but is that the best way?

If I understand what you want to do,
You can write both to stdout and a file using the app “tee”
There are plenty of examples how to use on the Web, IMO probably better than trying to read the documentation.

TSU

First, you put this here under OTHER VERSION, but do not mention that OTHER VERSION. Now your defence will probably be that writing bash scripts is rather openSUSE version independent. Yes it is. Thus it is that we have the sub forum Development > Programming and Scripting which is for “Questions about programming, bash scripts, perl, php, cron jobs, ruby, python, etc.”. And yes, guess what, you are not asked for an openSUSE version there!

I will move this thread to it’s proper place and it is CLOSED for the moment.

Moved from Applications and open again.

OK, After First, now Second ;).

Your script was only shown partly. Based on that:
When you want to redirect all (or a large part) standard output of the script to $OUTFILE, is is easier in the typing to do like this:

#!usr/bin/bash

#some statements

{

#all the awk statements, etc

} >$OUTFILE

Spares you typing a lot of >$OUTFILE , you can’t forget one and makes it easier to add more inside the { … }

But when you want to redirect some time and not the other time, you can use the exec statement. It is a bit strange side effect of this statement, but you can use

exec >$OUTFILE

to redirect from there on. This means that you can do:

#!/usr/bin/bash
# some statements

if .........
        exec >$OUTFILE
fi

#All statements where standard output will be redirected or not depending on the if statement above

HTH

And, rereading your post, what about using OUTFILE as your buffer/cache and at the end of the script decide what to do with it? Either leave it as it is or print it and remove it.

#the whole script
if ......
        cat $OUTFILE
        rm $OUTFILE
fi