Bash脚本读取文件 [英] Bash script to read a file
本文介绍了Bash脚本读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
不确定为什么最后一行没有从脚本中剪切":
Not sure why the last line does not cut the " from the script:
#!/bin/bash
FILENAME=$1
while read line
do
cut -d '"' -f2
echo $line
done < $FILENAME
$ cat file
"1" test
"2" test
"3" test
"4" test
"5" test
如果我使用以下命令运行此脚本:
If I run this script with the following command:
$ ./test file
2
3
4
5
"1" test
推荐答案
循环执行一次.
- 它将
"1" test
读入变量$line
. - 它执行
cut -d '"' -f2
,该操作读取文件的2-5行(因为这是当时的标准输入)并打印数字. - 它回显第一行的内容.
- It reads
"1" test
into variable$line
. - It executes
cut -d '"' -f2
which reads lines 2-5 of the file (because that is the standard input at the time) and prints the number. - It echoes what it read as the first line.
修复:
cut -d '"' -f2 $FILENAME
另一方面,如果您希望将数字放入变量中,则可以通过多种方式进行操作,包括:
If, on the other hand, you want to get the numbers into a variable, you could do this in a variety of ways, including:
cut -d '"' -f2 $FILENAME |
while read number
do # What you want
echo $number
done
或:
while read line
do
number=$(echo "$line" | cut -d '"' -f2)
echo $number
done
这篇关于Bash脚本读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文