Shell script reads 2 of a line in a file at a time _linux shell

Source: Internet
Author: User

Writing a:

Copy Code code as follows:

#!/bin/bash
While Read line
Todo
echo $line #这里可根据实际用途变化
Done < Urfile

Writing two:

Copy Code code as follows:

#!/bin/bash
Cat Urfile | While Read line
Todo
Echo $line
Done

Note: The above code is urfile as a read file

Shell reads text file

Method One: Get the desired content by command, pass it to the variable

Copy Code code as follows:

var1=$ (grep-i "^root"/etc/passwd | cut-d:-F 3 2>/dev/null) <= read the ID of root in the/etc/passwd file

Method Two: Reading this file through the Read command

Copy Code code as follows:

While read Wone wtwo wthree
Todo
[Z $wOne] && continue #测试此行内容是否为空
xxx= $wOne #提取内容
Done </var/xxx/one.txt
echo "$xxx" #变量获取了文件中的内容

The following methods cannot extract the content, but you can output it. The problem seems to be the way in Bash's handling of pipelines
(bash may generate a child shell to process pipeline output).
Copy Code code as follows:

Cat/var/xxx/one.txt | While read Wone wtwo wthree
Todo
[Z $wOne] && continue #测试此行内容是否为空
xxx= $wOne #提取内容
Done
echo "$xxx" #这里变量内容没有改变

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.