When reading a line, it stops after the first iteration

I am trying to run a simple script to capture some server information using svmatchthe server names entered from the file.

#!/bin/sh
while read line; do
svmatch $line
done < ~/svr_input;

The team svmatchworks without problems when executed as a stand along the team.

+4
source share
1 answer

Redirect the stdin internal command from /dev/null:

svmatch $line </dev/null

Otherwise, svmatch can use stdin (which, of course, is a list of the remaining lines).

Another approach is to use a file descriptor other than the default value for stdin:

#!/bin/sh
while IFS= read -r line <&3; do
  svmatch "$line"
done 3<svr_input

... bash, /bin/sh, ; , bash 4.1 , FD:

#!/bin/bash
while IFS= read -r -u "$fd_num" line; do
  do-something-with "$line"
done {fd_num}<svr_input
+8

Source: https://habr.com/ru/post/1527417/


All Articles