如题是本文介绍的内容
一般而言有三种流行的写法:
如下:
写法一:
#!/bin/bash
while read line
do
echo $line
done < file(待读取的文件)
写法二:
#!/bin/bash
cat file(待读取的文件) | while read line
do
echo $line
done
写法三:
for line in `cat file(待读取的文件)`
do
echo $line
done
个人比较推荐的一种写法是:
#!/bin/bash
IFS=':'
echo "Employee Names:"
echo "---------------"
while read name empid dept
do
echo "$name is part of $dept department"
done < employees.txt
需要读入的文件名字 叫 employees.txt 这个位置也可以写绝对路径
文件内容:
Emma Thomas:100:Marketing
Alex Jason:200:Sales
Madison Randy:300:Product Development
Sanjay Gupta:400:Support
Nisha Singh:500:Sales
输出效果:
Employee Names:
---------------
Emma Thomas is part of Marketing department
Alex Jason is part of Sales department
Madison Randy is part of Product Development department
Sanjay Gupta is part of Support department
Nisha Singh is part of Sales department