cat file | while read line problem

Redirect loop

Perhaps you should have seen the following such an approach in other scripts:
while read line
do
       …
done < file
Just beginning to see this structure, it is difficult to understand <how the file is to work together with the cycle. Because there are a lot of commands within the loop, and redirect our previous contacts are for a command to work. There is a principle, the principle master, and this issue is very simple:
Circulating the input redirection is applicable to all cycles required to read data from the standard input command;
Circulating redirected output applied to loop all the commands required to write data to the standard output;
When using an input or output redirection explicitly within the cycle, internal redirect external redirect cover.
While the above structure, read command is a need to read data from the standard input. We take a closer look at the use of read commands it, this command is one of the most frequently used commands in shell scripts.
read
First look at the read command syntax:
read arg1 arg2 arg3 arg4 …
a read command is assigned to, it needs to obtain a value from the standard input, then these values ​​are sequentially assigned to the variable positions arg1, arg2, arg3, arg4 ..., when input in the space as the field separator.
Is a maximum read characteristic may be generated in the interaction script, because the input data from the standard read. read very common reason why, first, because we often need to assign, and second, because it can be interactive, three is able to read more than once to the variable assignment.
$ read host ip name
linux 10.0.0.1 licong
$ echo $host $ip $name
linux 10.0.0.1 licong
$
You can see, linux, 10.0.0.1, licong were assigned to the variable host, ip and name. Look:
$ read host ip
linux 10.0.0.1 licong
$ echo $host
linux
$ echo $ip
10.0.0.1 licong
$
When we enter the field is larger than the number of variables, the value of the last variable will be more than one field, but all the remaining contents; when the input field is smaller than the number of variables, the extra variables will be null, you can try yourself. Now we will look at
while read line
do
       …
done < file
read through the input redirection, all the contents of the first file is assigned to the variable row line, comprising a generally circular body variable command processing line; then the loop processing of the second line of the file, the third row. . . Until the last line of the file. Remember while judged according to the command that follows the exit status whether to perform loop it? Yes, there are exit status read command, when it is read from the file in the file, exit status is 0, the cycle continues awakened; after reading the last line read from the file, the next time there is no content readable, and this when read a non-zero exit status, so the loop will exit.
Another very common use:
command | while read line
do
    …
done
If you remember the use of the pipeline, this structure should not be difficult to understand it. Command as input the output of the command read cycle, this structure length for processing more than one line output, of course, very good awk do such a thing.
. APP_FILE = / aabb (this is a multi-line file)
CAT $ app_file | the while the Read Line
do
    SSH -q $ Line LS / Home / ADMIN
DONE
Fail: only executed once, and out of the loop
The problem here is that all content is written to the cache and read out together, and let SSH redirection out
note:

1. ssh command on each execution, reads the contents of all of the standard input.

cat file | ssh 1.1.1.1 cat will read the contents of all files

2. For a while loop, when used as follows:

    cat file | while read line

As used herein, redirect the file to the contents of the input while command, while command reads the data from the input line per read.

The problem here, if you call the ssh command in a while loop, ssh will read all the data in the input current to go, which is the cat file to redirect data while command, ssh command are read left to as for when the next cycle, read read content is empty, leading to early termination of the cycle.

To solve this problem in two ways

1. ssh -n -n parameter using

2. ssh xxxx </ dev / null shell command input redirection

The main parameters:

-l login user specified

-p Sets the port number

-f run in the background, and add the recommended parameters -n

-n redirects standard input to / dev / null, prevent the reading of standard input

-N Do not execute a remote command, the only port forwarding

-q quiet mode, ignore all error messages and conversations

-T Disable pseudo terminal configuration
Reference article
http://blog.163.com/clevertanglei900@126/blog/static/11135225920118742327322/
 

First, the redirection method; Pipeline Act: cat $ FILENAME | while read LINE

Function While_read_LINE(){

cat $FILENAME | while read LINE

do

echo $LINE

done

}

         Note: I just put all this way is called pipeline method, we should be able to see out of it compared. When the input and output of the command will meet pipeline when pipeline to the left as to the right of the pipe command is then input it.

Second, examples

1, pri.sh

#!/bin/bash

cat ~/xieco/dirls | while read dd
do
  echo "chown -R test:test ${dd}"
  chown -R deuser:deuser ${dd}
done
 

 

2, Dirr

/usr/local/htdocs/dcserver/utilserver/
/usr/local/configs/

### questions asked
number of digital computing a.txt document appears in each row and you want to calculate the entire document appeared in a total of several numbers. For example a.txt follows:
12aa * lkjskdj
alskdflkskdjflkjj
our script named ncount.sh, when you run it:
bash ncount.sh a.txt
output should be:
2
0
SUM: 2

#### Reference answer
`` `

#!/bin/bash
sum =0
while  read  line
do
line_n=` echo  $line| sed  's/[^0-9]//g' | wc  -L`
echo  $line_n
sum =$[$ sum +$line_n]
done  < $1
echo  "sum:$sum"

  

When we enter the field is larger than the number of variables, the value of the last variable will be more than one field, but all the remaining contents; when the input field is smaller than the number of variables, the extra variables will be null, you can try yourself. Now we will look at
while read line
do
       …
done < file
read through the input redirection, all the contents of the first file is assigned to the variable row line, comprising a generally circular body variable command processing line; then the loop processing of the second line of the file, the third row. . . Until the last line of the file. Remember while judged according to the command that follows the exit status whether to perform loop it? Yes, there are exit status read command, when it is read from the file in the file, exit status is 0, the cycle continues awakened; after reading the last line read from the file, the next time there is no content readable, and this when read a non-zero exit status, so the loop will exit.
Another very common use:
command | while read line
do
    …
done
If you remember the use of the pipeline, this structure should not be difficult to understand it. Command as input the output of the command read cycle, this structure length for processing more than one line output, of course, very good awk do such a thing.
Perhaps you should have seen the following such an approach in other scripts:
while read line
do
       …
done < file
Just beginning to see this structure, it is difficult to understand <how the file is to work together with the cycle. Because there are a lot of commands within the loop, and redirect our previous contacts are for a command to work. There is a principle, the principle master, and this issue is very simple:
Circulating the input redirection is applicable to all cycles required to read data from the standard input command;
Circulating redirected output applied to loop all the commands required to write data to the standard output;
When using an input or output redirection explicitly within the cycle, internal redirect external redirect cover.
While the above structure, read command is a need to read data from the standard input. We take a closer look at the use of read commands it, this command is one of the most frequently used commands in shell scripts.
read
First look at the read command syntax:
read arg1 arg2 arg3 arg4 …
a read command is assigned to, it needs to obtain a value from the standard input, then these values ​​are sequentially assigned to the variable positions arg1, arg2, arg3, arg4 ..., when input in the space as the field separator.
Is a maximum read characteristic may be generated in the interaction script, because the input data from the standard read. read very common reason why, first, because we often need to assign, and second, because it can be interactive, three is able to read more than once to the variable assignment.
$ read host ip name
linux 10.0.0.1 licong
$ echo $host $ip $name
linux 10.0.0.1 licong
$
You can see, linux, 10.0.0.1, licong were assigned to the variable host, ip and name. Look:
$ read host ip
linux 10.0.0.1 licong
$ echo $host
linux
$ echo $ip
10.0.0.1 licong
$
When we enter the field is larger than the number of variables, the value of the last variable will be more than one field, but all the remaining contents; when the input field is smaller than the number of variables, the extra variables will be null, you can try yourself. Now we will look at
while read line
do
       …
done < file
read through the input redirection, all the contents of the first file is assigned to the variable row line, comprising a generally circular body variable command processing line; then the loop processing of the second line of the file, the third row. . . Until the last line of the file. Remember while judged according to the command that follows the exit status whether to perform loop it? Yes, there are exit status read command, when it is read from the file in the file, exit status is 0, the cycle continues awakened; after reading the last line read from the file, the next time there is no content readable, and this when read a non-zero exit status, so the loop will exit.
Another very common use:
command | while read line
do
    …
done
If you remember the use of the pipeline, this structure should not be difficult to understand it. Command as input the output of the command read cycle, this structure length for processing more than one line output, of course, very good awk do such a thing.
. APP_FILE = / aabb (this is a multi-line file)
CAT $ app_file | the while the Read Line
do
    SSH -q $ Line LS / Home / ADMIN
DONE
Fail: only executed once, and out of the loop
The problem here is that all content is written to the cache and read out together, and let SSH redirection out
note:

1. ssh command on each execution, reads the contents of all of the standard input.

cat file | ssh 1.1.1.1 cat will read the contents of all files

2. For a while loop, when used as follows:

    cat file | while read line

As used herein, redirect the file to the contents of the input while command, while command reads the data from the input line per read.

The problem here, if you call the ssh command in a while loop, ssh will read all the data in the input current to go, which is the cat file to redirect data while command, ssh command are read left to as for when the next cycle, read read content is empty, leading to early termination of the cycle.

To solve this problem in two ways

1. ssh -n -n parameter using

2. ssh xxxx </ dev / null shell command input redirection

The main parameters:

-l login user specified

-p Sets the port number

-f run in the background, and add the recommended parameters -n

-n redirects standard input to / dev / null, prevent the reading of standard input

-N Do not execute a remote command, the only port forwarding

-q quiet mode, ignore all error messages and conversations

-T Disable pseudo terminal configuration
Reference article
http://blog.163.com/clevertanglei900@126/blog/static/11135225920118742327322/
 

First, the redirection method; Pipeline Act: cat $ FILENAME | while read LINE

Function While_read_LINE(){

cat $FILENAME | while read LINE

do

echo $LINE

done

}

         Note: I just put all this way is called pipeline method, we should be able to see out of it compared. When the input and output of the command will meet pipeline when pipeline to the left as to the right of the pipe command is then input it.

Second, examples

1, pri.sh

#!/bin/bash

cat ~/xieco/dirls | while read dd
do
  echo "chown -R test:test ${dd}"
  chown -R deuser:deuser ${dd}
done
 

 

2, Dirr

/usr/local/htdocs/dcserver/utilserver/
/usr/local/configs/

### questions asked
number of digital computing a.txt document appears in each row and you want to calculate the entire document appeared in a total of several numbers. For example a.txt follows:
12aa * lkjskdj
alskdflkskdjflkjj
our script named ncount.sh, when you run it:
bash ncount.sh a.txt
output should be:
2
0
SUM: 2

#### Reference answer
`` `

#!/bin/bash
sum =0
while  read  line
do
line_n=` echo  $line| sed  's/[^0-9]//g' | wc  -L`
echo  $line_n
sum =$[$ sum +$line_n]
done  < $1
echo  "sum:$sum"

  

When we enter the field is larger than the number of variables, the value of the last variable will be more than one field, but all the remaining contents; when the input field is smaller than the number of variables, the extra variables will be null, you can try yourself. Now we will look at
while read line
do
       …
done < file
read through the input redirection, all the contents of the first file is assigned to the variable row line, comprising a generally circular body variable command processing line; then the loop processing of the second line of the file, the third row. . . Until the last line of the file. Remember while judged according to the command that follows the exit status whether to perform loop it? Yes, there are exit status read command, when it is read from the file in the file, exit status is 0, the cycle continues awakened; after reading the last line read from the file, the next time there is no content readable, and this when read a non-zero exit status, so the loop will exit.
Another very common use:
command | while read line
do
    …
done
If you remember the use of the pipeline, this structure should not be difficult to understand it. Command as input the output of the command read cycle, this structure length for processing more than one line output, of course, very good awk do such a thing.

Guess you like

Origin www.cnblogs.com/klb561/p/11241651.html