
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.