0

Two test scripts as below:

$cat echo_test1.sh
#!/bin/sh

count=0

while true
do

   echo "Test count: $count"
   count=`expr $count + 1`
   sleep 2
done

Second script which receive data from first script continuously:

$cat echo_test2.sh
#!/bin/sh

while read $1
do

  echo "Test 2: $1"
  sleep 2

done

and run it like this:

$./echo_test1.sh | ./echo_test2.sh

test1 script echo continuously and test2 receive it and echo again with some addition to received message.

what are the ways possible to achieve this function.

coder007
  • 317
  • 1
  • 5
  • 12
  • You can't read into `$1`. Choose a real variable name, not a positional parameter. – Charles Duffy Oct 01 '18 at 03:38
  • That is to say: `while read line; do echo "Test 2: $line"; done` would work perfectly. – Charles Duffy Oct 01 '18 at 03:38
  • ...btw, there's no reason to `sleep` on the receiving end -- let it run as fast as it can; it'll be held up waiting for there to actually be content to read. – Charles Duffy Oct 01 '18 at 03:39
  • Also: Always show your actual error message in the question: Not just your expected code, but how it fails in practice (or anticipated and actual output, highlighting how they differ). I was able to figure it out here, but with a well-asked question, that wouldn't be necessary. – Charles Duffy Oct 01 '18 at 03:40
  • This is definitely not duplicate question . while read line; do echo "Test 2: $line"; done worked. thanks – coder007 Oct 01 '18 at 04:05
  • It certainly *is* a duplicate question -- your bug was the `read $1`, which the linked question tells you won't/can't work (to modify `$1`). The context of that bug being unique makes no difference: A good SO question strips context and focuses only on the immediate issue, described in the [mcve] guidelines. – Charles Duffy Oct 01 '18 at 14:40

0 Answers0