linux脚本读取文件内容

如题是本文介绍的内容


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

如下:

写法一:


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






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值