Bash when the loop stops unexpectedly

I am analyzing two scenarios with some behavior that I do not understand:

#/bin/bash
tijd=${1-60}
oud=`ls -l $MAIL`
while : ; do
   nieuw=`ls -l $MAIL`
   echo $oud $nieuw
   sleep $tijd
done | { read a b rest ; echo $a ; echo $b ; echo $rest ; }

The while loop in this script stops after one iteration.

#/bin/bash
tijd=${1-60}
oud=`ls -l $MAIL`
while : ; do
   nieuw=`ls -l $MAIL`
   echo $oud $nieuw
   sleep $tijd
done | cat

The while loop in this script is infinite.

What is the difference? I think this is something with a pipe and brackets, but I can’t explain it.

+4
source share
2 answers

read pipe - SIGPIPE, , LHS , RHS while read). cat , cat , read .

, :

while : ; do pwd; done | { read -r line; echo $line; }
/Users/admin

So read . , pipefail, :

set -o pipefail

:

while : ; do pwd; done | { read -r line; echo "$line"; }
echo $?
141

exest 141 SIGPIPE.


, read RHS while:

while : ; do pwd; sleep 5; done | { while read -r line; do echo "$line"; done; }
/Users/admin
/Users/admin
/Users/admin

, , while read LHS .

+2

, { a rest;..} , , , .

, , :

#/bin/bash
tijd=${1-60}
oud=`ls -l $MAIL`
while read a b rest; do
  echo $a
  echo $b
  echo $rest
done < <(
  while : ; do
    nieuw=`ls -l $MAIL`
    echo $oud $nieuw
    sleep $tijd
  done
)

"" . pipe |, {read..} , .

< (..) . , , .

0

All Articles