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

Need help w/simple script

0 views
Skip to first unread message

David Conrad

unread,
Oct 8, 2000, 1:52:17 AM10/8/00
to
Hello,

please help me with a simple scripting problem. I am trying to write a
borne shell script. I have a script that takes a text file as an input
and does statistics on it:

test_file
2 4 5 5
2 5 3 2
etc

Average
.whatetever
.
.
.

This part works fine. I want to be able to read from standard input and
do the same stats. Right now my program looks for a file. Is there
anyway I can detect if standard input was sent, and then write it to a
file? I want to be able to do the below...

cat test_file | stats

I was thinking something like 0 > afile but that didnt' seem to work.

Hope this makes sense.
Thank you!
please respond to con...@engr.orst.edu

Jehsom

unread,
Oct 8, 2000, 2:48:08 AM10/8/00
to
David Conrad <con...@engr.orst.edu> wrote:
> I have a script that takes a text file as an input
> and does statistics on it:
> This part works fine. I want to be able to read from standard input and
> do the same stats. Right now my program looks for a file. Is there
> anyway I can detect if standard input was sent, and then write it to a
> file? I want to be able to do the below...
> cat test_file | stats
> I was thinking something like 0 > afile but that didnt' seem to work.

I assume there is some command in your script that reads in the contents
of the file and processes it. Instead of specifying the filename there,
just leave it blank and most programs will just read from stdin. It's
hard to tell without seeing some of your code though.

Moshe

--
jehsom@ angband.org cc.gatech.edu polter.net shaftnet.org nullity.dhs.org
wreck.org bellsouth.net resnet.gatech.edu burdell.org yo.dhs.org gooning.org
usa.net togetherweb.com; gte741e mj116 @prism.gatech.edu; jacobsonconsulting@
usa.net; ICQ 1900670

David Conrad

unread,
Oct 8, 2000, 3:00:00 AM10/8/00
to
filename=$2

Here is a sample of my program.

printf "Average\tMedian\n"
while read line
do
for i in $line
do
sum=`expr $sum + $i`
count=`expr $count + 1`
done

test=`expr $count / 2 + 1`


for i in $line
do
echo $i
done | sort -r | head -$test | tail -1 >tmp

read mean < tmp

average=`expr $sum / $count`
printf "$average\t $mean \n"
sum=0
count=0

done < $filename

------

All I need to do is replace < test2.txt with nothing and it will read from
standard input? Can I wrap an if statement around filename so that if a filename
was not specified, no file would be read? Thanks!

Jehsom

unread,
Oct 8, 2000, 3:00:00 AM10/8/00
to
David Conrad <con...@engr.orst.edu> wrote:
> filename=$2

> Here is a sample of my program.

> printf "Average\tMedian\n"
> while read line
> do

> [ some stuff ]
> done < $filename

> All I need to do is replace < test2.txt with nothing and it will read
> from standard input? Can I wrap an if statement around filename so that
> if a filename was not specified, no file would be read? Thanks!

So basically it's a big "while read" loop whose input is $filename.

Instead of providing the input by putting "< $filename" after the
"done" at the end, do it by putting "cat $filename | " in front of
the "while read" statement.

Actually, you should put:

cat $1 | while read line; do
[ some stuff ]
done

With this method, if $1 is specified, cat takes its input from that
file. If not, $1 is empty, so cat takes its input from stdin, thus
providing the desired result.

Peter Sundstrom

unread,
Oct 8, 2000, 8:31:31 PM10/8/00
to

David Conrad <con...@engr.orst.edu> wrote in message
news:39E0FCD4...@engr.orst.edu...

> filename=$2
>
> Here is a sample of my program.
>
> printf "Average\tMedian\n"
> while read line
> do
> for i in $line
> do
> sum=`expr $sum + $i`
> count=`expr $count + 1`
> done
>
> test=`expr $count / 2 + 1`
>
>
> for i in $line
> do
> echo $i
> done | sort -r | head -$test | tail -1 >tmp
>
> read mean < tmp
>
> average=`expr $sum / $count`
> printf "$average\t $mean \n"
> sum=0
> count=0
>
> done < $filename
>
> ------
>
> All I need to do is replace < test2.txt with nothing and it will read from
> standard input? Can I wrap an if statement around filename so that if a
filename
> was not specified, no file would be read? Thanks!


This is actually a useful use of cat. Just change your while loop to read:

cat $filename | while read line
do
...
done

That way if $filename is not set, STDIN will be used.


David Conrad

unread,
Oct 9, 2000, 10:22:30 PM10/9/00
to
Worked like a charm. Thank you everyone for the help!!

Jehsom wrote:

> David Conrad <con...@engr.orst.edu> wrote:
> > filename=$2
>
> > Here is a sample of my program.
>
> > printf "Average\tMedian\n"
> > while read line
> > do

> > [ some stuff ]
> > done < $filename


>
> > All I need to do is replace < test2.txt with nothing and it will read
> > from standard input? Can I wrap an if statement around filename so that
> > if a filename was not specified, no file would be read? Thanks!
>

> So basically it's a big "while read" loop whose input is $filename.
>
> Instead of providing the input by putting "< $filename" after the
> "done" at the end, do it by putting "cat $filename | " in front of
> the "while read" statement.
>
> Actually, you should put:
>
> cat $1 | while read line; do
> [ some stuff ]
> done
>
> With this method, if $1 is specified, cat takes its input from that
> file. If not, $1 is empty, so cat takes its input from stdin, thus
> providing the desired result.
>

0 new messages