: Weird ">" redirect behavior vs. ">>" redirect behavior

: Weird ">" redirect behavior vs. ">>" redirect behavior

Post by Sean O'Neil » Wed, 03 Apr 2002 02:50:09



Hopefully, someone has already seen this and knows whats up.

Simple script myscript.sh:

#!/bin/sh
i=1

date
while [ 1 ]; do
   echo "Count - ${i}"
   cat /etc/system
   i=`expr ${i} + 1`
   sleep 5
done

Run it as:

nohup sh myscript.sh > /tmp/myscript.log &

Let it run for a while.  Then run:

grep Count /tmp/myscript.log | wc -l
ls -l /tmp/myscript.log
cat /dev/null /tmp/myscript.log
ls -l /tmp/myscript.log
ls -l /tmp/myscript.log
ls -l /tmp/myscript.log
grep Count /tmp/myscript.log | wc -l

The file size info on my system (Solaris 8) for the /tmp/myscript.log
is zero at first and then goes right back to where it was but a bit
more when the next iteration of the cat command output /etc/system.

Also, the first grep..wc -l command shows how many times the script
has basically looped.  The second grep will show only a couple.  This
shows that the inode is saying the file contains everything is always
did but the grep command is saying it only contains up to some of
point a buffer - maybe the output file descriptor buffer?  I'm
guessing I'm not a programmer

If you change the ">" to a ">>" with your nohup , it works as
expected. The file is zeroed out and the inode info shows it growing
as you would expect it to.

What's the differences between ">" and ">>" that makes this happen?
Running the script with ksh does the same thing.

Looks like a bug to me.

--
........................................................
......... ..- -. .. -..- .-. ..- .-.. . ... ............
.-- .. -. -... .-.. --- .-- ... -.. .-. --- --- .-.. ...

Sean O'Neill

 
 
 

: Weird ">" redirect behavior vs. ">>" redirect behavior

Post by Philip Bro » Wed, 03 Apr 2002 17:41:04



>...
>If you change the ">" to a ">>" with your nohup , it works as
>expected. The file is zeroed out and the inode info shows it growing
>as you would expect it to.

>What's the differences between ">" and ">>" that makes this happen?
>Running the script with ksh does the same thing.

>Looks like a bug to me.

only a bug in your understanding.

'>>' means essentially, "Do a seek to the end of the file before writing
                         anything".

So, if you truncate the file to zero length, then >> will seek to the
new "end" position of 0.
The '>' operator does not seek to the end. So the kernel keeps its prior
last 'write' position.
Note: *the kernel*, not the shell.

--
 ***   http://www.bolthole.com/resume.html  ***
[Trim the no-bots from my address to reply to me by email!]
[ Do NOT email-CC me on posts. Pick one or the other.]

http://www.spamlaws.com/state/ca1.html