Linux 使用shell脚本读取文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8876411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
reading a file using shell script
提问by Abdul Manaf
I have a text file named sqlfile
, with the following content:
我有一个名为 的文本文件sqlfile
,内容如下:
a.sql
b.sql
c.sql
d.sql
What I want is that to store them in variables and then print using for
loop.
But here I get only d.sql
in the output of the script.
我想要的是将它们存储在变量中,然后使用for
循环打印。但在这里我只得到d.sql
脚本的输出。
The script:
剧本:
#!/bin/bash
while read line
do
files=`echo $line`
done < /home/abdul_old/Desktop/My_Shell_Script/sqlfile
for file in $files
do
echo $file
done
回答by SiegeX
A variable can only hold one element, what you want is an array
一个变量只能容纳一个元素,你想要的是一个数组
#!/bin/bash
while read line
do
files+=( "$line" )
done < /home/abdul_old/Desktop/My_Shell_Script/sqlfile
for file in "${files[@]}"
do
echo "$file"
done
回答by Jonathan Leffler
while read line
do files="$files $line"
done < /home/abdul_old/Desktop/My_Shell_Script/sqlfile
or
或者
files=$(</home/abdul_old/Desktop/My_Shell_Script/sqlfile)
or
或者
files=$(cat /home/abdul_old/Desktop/My_Shell_Script/sqlfile)
You're doing way too much work in your loop.
您在循环中做了太多工作。
The middle alternative works with bash
; the other two work with most shells. Prefer $(...)
to back-quotes.
中间选择适用于bash
; 其他两个适用于大多数 shell。更喜欢$(...)
反引号。
This code assumes there are no spaces in file names to mess things up. If you do use blanks in file names, you have to work marginally harder - see the array-based solution by SiegeX
这段代码假设文件名中没有空格来搞砸。如果您确实在文件名中使用空格,则必须稍微努力一点 - 请参阅SiegeX的基于数组的解决方案
回答by hhwangcocora
I think you need to make the "files" as array. otherwise, as soon as the while finishes, "files" stores the latest "line". try:
我认为您需要将“文件”作为数组。否则,一旦 while 结束,“文件”就会存储最新的“行”。尝试:
files=( "${files[@]}" $line )
回答by Odobenus Rosmarus
That's right, you assifn last value to "files"
没错,您将最后一个值赋予“文件”
You must use for instance += instead of =
例如,您必须使用 += 而不是 =
#!/bin/bash
while read line
do
files+=`echo " $line"`
done < /home/abdul_old/Desktop/My_Shell_Script/sqlfile
for file in $files
do
echo $file
done
回答by John
Using read is fine but you have to set the IFS environment variable first else leading and trailing white space are removed from each line: Preserving leading white space while reading>>writing a file line by line in bash.
使用 read 很好,但您必须首先设置 IFS 环境变量,否则将从每一行中删除前导和尾随空格:在读取时保留前导空格>>在 bash 中逐行写入文件。
回答by Aleks-Daniel Jakimenko-A.
All you have to do is:
您所要做的就是:
readarray myData < sqlfile
This will put file lines into an array called myData
Now you can access any of these lines like this:
这会将文件行放入一个名为的数组中,myData
现在您可以像这样访问这些行中的任何一行:
printf "%s\n" "${myData[0]}" #outputs first line
printf "%s\n" "${myData[2]}" #outputs third line
And you can iterate over it:
你可以迭代它:
for curLine in "${myData[@]}"; do
echo "$curLine"
done
Note that these lines would contain \n
character as well. To remove trailing newlines you can use -t
flag like this:
请注意,这些行也将包含\n
字符。要删除尾随换行符,您可以使用如下-t
标志:
readarray -t myData < sqlfile
readarray
is a synonym to mapfile
. You can read about it in man bash
readarray
是 的同义词mapfile
。你可以在man bash