我在 shell (bash) 脚本中使用 find 命令来获取与模式匹配的文件列表,并将这些文件名转储到文本文件中。
find ./ -type f -name *.txt >> files.list
生成一个包含
的文件helloworld.txt
letter.txt
document-1.txt
document-1.backup.txt
等等
find 命令不是那么重要。我实际使用的那个包含 regex-es,并生成一个合理的匹配文件列表,用作程序的输入文件。
我想用一个标志标记每个文件名,以标记它是 Type A
文件还是 Type B
文件。即,我希望输出文件 files.list
看起来像这样
helloworld.txt type=A
letter.txt type=A
document-1.txt type=B
document-1.backup.txt type=B
如果 helloworld
和 letter
是 Type A
文件,而 document-1
是 Type B
文件。
我想也许我可以先写一个 files.list.tmp
文件,然后使用另一个 bash 脚本逐行处理它来读回它......但这会产生额外的文件和我真的不想那样做。
我可以使用 >>>
运算符来创建变量或其他东西吗?
我不太确定我应该在这里做什么。
顺便说一下,当这个 bash 脚本被调用时,它已经知道匹配正则表达式的文件是类型 A 还是类型 B,因为它作为脚本的参数给出。
最佳答案
I thought perhaps I could write a files.list.tmp file first, and then read it back in using another bash script processing it line by line... But that would make additional file and I don't really want to do that
您可以使用 process substitution 避免创建临时文件:
./ascript.sh < <(find . -type f -name '*.txt')
并让您的脚本逐行读取文件名。
关于linux - 在转储到文件之前处理从 bash 脚本中的 find 命令获取的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41241725/