bash 逐行读取目录中的所有文件并对它们执行命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18319481/
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
Read all files line by line in a directory and do a command on them
提问by Beardy
I am trying to make a script that will allow me to read all files in a directory line by line while doing a command to a specific column of these files. The files that I am dealing with are .txt files that have values that are separated by commas. I am able to execute the code with a single file by inputting the text file to the script and then outputting the values but not with multiple files, which is my goal. I would like the files to be read in the order they are in in the directory. Here is what I have so far.
我正在尝试制作一个脚本,该脚本允许我在对这些文件的特定列执行命令时逐行读取目录中的所有文件。我正在处理的文件是 .txt 文件,其值以逗号分隔。我可以通过将文本文件输入脚本然后输出值而不是多个文件来使用单个文件执行代码,这是我的目标。我希望按照它们在目录中的顺序读取文件。这是我到目前为止所拥有的。
directory=$(/dos2unix/*.txt)
file=$(*.txt")
IFS=","
for "$file" in "$directory";
do
while read -ra line;
do
if [ "${line[1]}" != "" ]; then
echo -n "${line[*]}, Hash Value:"; echo "${line[1]}" | openssl dgst -sha1 | sed 's/^.* //'
else
if [ "${line[1]}" == "" ]; then
echo "${line[*]}, Hash Value:None";
fi
fi
done
done
Some of the errors that I am getting are:
我得到的一些错误是:
$ ./orange2.sh
/dos2unix/test1.txt: line 1: hello: command not found
/dos2unix/test1.txt: line 2: goodbye: command not found
/dos2unix/test1.txt: line 4: last: command not found
./orange2.sh: line 28: unexpected EOF while looking for matching `"'
./orange2.sh: line 33: syntax error: unexpected end of file
Any tips, suggestions, or examples?
任何提示、建议或示例?
Thanks all
谢谢大家
UPDATE
更新
I am also looking to copy all of the files eventually to contain the command that you see in the first if statement so I would like to a.) keep my files separate and b.) create a copy that will contain the updated value.
我还希望最终复制所有文件以包含您在第一个 if 语句中看到的命令,因此我希望 a.) 将我的文件分开并 b.) 创建一个包含更新值的副本。
采纳答案by John Kugelman
If you want to save the file names in a variable, use array=(a b c)
syntax to create an array. There's no dollar sign. Then loop over the array with for item in "${array[@]}"
.
如果要将文件名保存在变量中,请使用array=(a b c)
语法创建数组。没有美元符号。然后用 循环遍历数组for item in "${array[@]}"
。
To read from a file with a while read
loop, use while read; do ...; done < "$file"
. It's odd-looking, but the file is redirected into the loop as a whole.
要while read
使用循环从文件中读取,请使用while read; do ...; done < "$file"
. 它看起来很奇怪,但文件作为一个整体被重定向到循环中。
files=(/dos2unix/*.txt)
for file in "${files[@]}"; do
while IFS=',' read -ra line; do
...
done < "$file"
done
Another way is to use cat
to concatenate all the files together, which lets you get rid of the outer for
loop.
另一种方法是使用cat
将所有文件连接在一起,这样您就可以摆脱外部for
循环。
cat /dos2unix/*.txt | while IFS=',' read -ra line; do
...
done