bash脚本文件读写操作
读文件
script % touch read.sh
script % chmod 755 read.sh
#!/usr/bin/env bash
fileName=""
echo input file name:
read fileName # 等待用户输入文件名
COUNT=1
while read -r LINE # 读入一行
do
echo $COUNT $LINE
((COUNT++))
done < $fileName # 读入文件
exit 0
script % ./read.sh
input file name:
food.txt
1 apple
2 grape
3 blackberry
food.txt:
apple
grape
blackberry
写入文件 >
read.sh:
#!/usr/bin/env bash
COUNT=1
while read -r LINE # 读入一行
do
echo $COUNT $LINE
((COUNT++))
done < $1 # 读入文件
exit 0
将读文件的结果写入out.txt:
script % ./read.sh food.txt > out.txt
script % cat out.txt
1 apple
2 grape
3 blackberry