It is currently Tue, 17 May 2022 19:18:30 GMT



 
Author Message
 Write stdin to file with while/read loop

I use a small script to write certain input to a file and prepend
certain strings and  a date to it with code like this:

cat addsnp:

  #!/bin/sh
  echo "Keywords: $1" >> $2
  date >> $2
  echo -e "Hit ^D when done to write data to file"
      while read line
      do
      echo $line >> $2
  done

This allows typed/pasted data to be written to $2 after prepending the
string contained in $1 and the date:

  addsnp "Keywords: something" file

Will process/write whatever is given on stdin when I hit ^d.

Not sure I understand why I can't pipe data in and have the same
behavior.

   echo "SOMETHING" |addsnp "Keywords: " file

`addsnp' doesn't wait for me to hit ^d or add to the data already piped,
then hit ^d.  Is it because a ^d is already being sent?

If so, can I avoid that somehow.  Maybe sticking tr or sed in there with the
right args or something?

I did try (probably dumb) one thing:

  echo "SOMETING" |sed 's/^D//'|addsnp "NOWWHAT" var.file
    (using ^v ^d in the sed part)
Still the `while' loop doesn't wait.



 Tue, 03 Jun 2003 23:30:44 GMT   
 Write stdin to file with while/read loop
In article <m27l51em6z....@gnus-5.8.8-cvs.now.playing>,
Harry Putnam  <rea...@newsguy.com> wrote:

There is no ^d.  ^d is how you cause EOF to be signalled when the program
is reading from a terminal.  When you're reading from a file or pipe, EOF
is recognized when you reach the end of the data.

(echo "SOMETHING"; cat) | addsnp "Keywords: file

--
Barry Margolin, bar...@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.



 Wed, 04 Jun 2003 08:36:26 GMT   
 Write stdin to file with while/read loop

{echo "Something"; cat) | addsnp "NOWWHAT" var.file



 Wed, 04 Jun 2003 10:16:50 GMT   
 Write stdin to file with while/read loop

[...]

[...]

[...]

Thanks fellows... I think I've used up my dumb dumb quota for today.



 Wed, 04 Jun 2003 11:18:02 GMT   
 
   [ 4 post ] 

Similar Threads

1. loop.c with file->read/write ?

2. Reading stdin from inside a while loop in sh

3. read from stdin while reading line by line a file

4. reading from stdin, buffer it and writing to stdout

5. Need help writing C program using UNIX system calls (read, write, etc) that copies files

6. Reading from stdin or real file

7. Piping - from a file then return to reading from stdin

8. Reading a file (other than stdin) from a CShell script

9. Sockets: write() writes OK, yet read() reads garbage...?

10. BCP program loops reading passwd file


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software