方法有很多,下面写出三种方法:
写法一:
#!/bin/bash while read line do echo $line done <p style="text-align: left;">写法二:</p><pre class="brush:php;toolbar:false">#!/bin/bash cat filename(待读取的文件) | while read line do echo $line done
写法三:
for line in `cat filename(待读取的文件)` do echo $line done
说明:
for逐行读和while逐行读是有区别的,如:
$ cat file 1111 2222 3333 4444 555 $ cat file | while read line; do echo $line; done 1111 2222 3333 4444 555 $ for line in $(<file><span style="color: rgb(0, 0, 0); font-family: tahoma, arial, 宋体; font-size: 18px; white-space: normal; background-color: rgb(221, 237, 251);"><p style="text-align: left;"><span style="font-size: 14px;">更多shell按行读取文件的3种方法相关文章请关注PHP中文网!</span></p></span></file>
Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号