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" #这里变量内容没有改变