新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
shell腳本中按行讀取文本文件
shell是外殼的意思,就是操作系統(tǒng)的外殼。我們可以通過shell命令來操作和控制操作系統(tǒng),比如Linux中的Shell命令就包括ls、cd、pwd等等??偨Y(jié)來說,Shell是一個(gè)命令解釋器,它通過接受用戶輸入的Shell命令來啟動(dòng)、暫停、停止程序的運(yùn)行或?qū)τ?jì)算機(jī)進(jìn)行控制。

假設(shè)讀取的文件為當(dāng)期目錄下的 test.txt 文件,內(nèi)容如下:
Google
Runoob
Taobao
實(shí)例 1
#!/bin/bash
while read line
do
echo $line
done
執(zhí)行輸出結(jié)果為:
Google
Runoob
Taobao
實(shí)例 2
#!/bin/bash
cat test.txt | while read line
do
echo $line
done
執(zhí)行輸出結(jié)果為:
Google
Runoob
Taobao
實(shí)例 3
for line in `cat test.txt`
do
echo $line
done
執(zhí)行輸出結(jié)果為:
Google
Runoob
Taobao
for 逐行讀和 while 逐行讀是有區(qū)別的,如:
$ cat test.txt
Google
Runoob
Taobao
$ cat test.txt | while read line; do echo $line; done
Google
Runoob
Taobao
$ for line in $(
do
echo
$line;
done Google Runoob Taobao
文章名稱:shell腳本中按行讀取文本文件
瀏覽路徑:http://m.fisionsoft.com.cn/article/djegips.html


咨詢
建站咨詢
