分享

linux脚本读取文件内容

 戴维图书馆 2018-08-04

如题是本文介绍的内容

一般而言有三种流行的写法:

如下:

写法一:

#!/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

个人比较推荐的一种写法是:

1
2
3
4
5
6
7
8
#!/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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多