In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)05/31 Report--
Xiaobian to share with you how to read the file line by line in the Shell script, I believe most people still do not know how to share this article for your reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!
Method 1: Use Input Redirection
The easiest way to read a file line by line is to use input redirection in a while loop.
To demonstrate, create a text file named " mycontent.txt" with the following contents:
[root@localhost ~]# cat mycontent.txt This is a sample file We are going through contents line by line to understand
Create a script called "example1.sh" that uses input redirection and looping:
[root@localhost ~]# cat example1.sh #!/ bin/bash while read rows do echo "Line contents are : $rows " done
< mycontent.txt 运行结果:How it works:
- Start the while loop and save the contents of each line in the variable "rows"
- Use echo to display the output, and the $rows variable is each line in the text file
- Use echo to display output, including custom strings and variables, and the $rows variable is each line in the text file
Tips: You can reduce the above script to a single command, as follows:
[root@localhost ~]# while read rows; do echo "Line contents are : $rows"; done < mycontent.txt
Method 2: Use cat command and pipe symbol
The second method is to use the cat command and pipe characters|, and then pipe its output as input to the while loop.
Create a script file "example2.sh" with the following contents:
[root@localhost ~]# cat example2.sh #!/ bin/bash cat mycontent.txt | while read rows do echo "Line contents are : $rows " done
Run Results:
How it works:
- Pipelines the output of the cat command as input to the while loop.
- |管道符将cat输出的内容保存在"$rows"变量中。
- Use echo to display output, including custom strings and variables, and the $rows variable is each line in the text file
Tips: You can reduce the above script to a single command, as follows:
[root@localhost ~]# cat mycontent.txt |while read rows;do echo "Line contents are : $rows";done
Method 3: Use the file name passed in as an argument
The third method appends the text file name to the end of the script when executing it by adding the $1 parameter.
Create a script file called "example3.sh" as follows:
[root@localhost ~]# cat example3.sh #!/ bin/bash while read rows do echo "Line contents are : $rows " done < $1
Run Results:
How it works:
- Start the while loop and save the contents of each line in the variable "rows"
- Use echo to display the output, and the $rows variable is each line in the text file
- Use input redirection
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.