while read does not work in Unix shell script

Asked

Viewed 86 times

0

I’m writing a script and using while read twice he makes the 1 only once:

un=$(echo $LOGNAME | tr '[A-Z]' '[a-z]')
grep -v '^#' nodeteste.txt > auxiliar.txt
while read line
do
  ssh "$un@$line" "cat arquivo" > arquivo.externo
  cut -d "," -f 1 arquivo.externo > arquivo.externo.cut
  uniq arquivo.externo.cut > arquivo.externo.cut.uniq
  while read line2
  do
   valor=$(grep -c "$line2 " arquivo.base)
   if [ $valor -eq 0 ]; then
     echo $line >> arquivo.base.diferente
     echo $line2 >> arquivo.base.diferente
     grep -i "$line2," arquivo.externo >> arquivo.base.diferente
     echo $line $line2 $valor "eh diferente"
   fi
  done < arquivo.externo.cut.uniq
done < auxiliar.txt
rm auxiliar.txt

he executes the first while just once, I’m suspicious that he missed stdin/out at some point!! Someone could give some hint!!

1 answer

0

I found the problem, ssh uses stdin causing the problem of stopping the 1 loop after only 1 pass, when I use the -n option it redirects stdin stopping the problem.

Problem source:

ssh "$un@$line" "cat file" > external.

Works source:

ssh -n "$un@$line" "cat file" > external.

SSH Manual: -n Redirects stdin from /dev/null (prevents Reading stdin).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.