while read讀取文本內容

讀取文件給 while 循環
方式一: bash

exec <FILE 
while read line 
do 
    cmd 
done 

方式二:spa

cat FILE_PATH |while read line 
do 
    cmd 
done 

方式三:code

while read line 
do 
    cmd 
done <FILE

 

舉例:blog

ip.txt內容以下:ip

10.1.1.11 root 123
10.1.1.22 root 111
10.1.1.33 root 123456
10.1.1.44 root 54321

 

寫法1:cmd

cat ip.txt | while read ip user pass
do
    echo "$ip--$user--$pass"
done

 

寫法2:class

while read ip user pass
do
    echo "$ip--$user--$pass"
done < ip.txt

 

使用IFS做爲分隔符讀文件test

說明:默認狀況下IFS是空格,若是須要使用其它的須要從新賦值循環

IFS=:di

例如:

# cat test
chen:222:gogo
jie:333:hehe
# cat test.sh
#!/bin/bash
IFS=:
cat test | while read a1 a2 a3
do
    echo "$a1--$a2--$a3"
done
相關文章
相關標籤/搜索