使用 bash one-liner 附加文​​件?

Rya*_*yan 3 shell bash shell-script

目前我使用多行将内容附加到组合文件中,例如

./myprogram 1.txt > Out.txt # Overwrite for the 1st file
./myprogram 2.txt >> Out.txt
./myprogram 3.txt >> Out.txt
./myprogram 4.txt >> Out.txt
Run Code Online (Sandbox Code Playgroud)

可以用单线代替吗?

小智 8

(./myprogram 1.txt; ./myprogram 2.txt; ./myprogram 3.txt; ./myprogram 4.txt) > out.txt
Run Code Online (Sandbox Code Playgroud)


frn*_*ntn 5

这取决于您想要做什么以及您的程序如何处理输入参数。

但是假设你有/path/to/myprogram.sh一个看起来像这样的人:

#!/bin/bash
echo "Processing file ${1?missing input file}"
Run Code Online (Sandbox Code Playgroud)

您可以执行以下操作

find /path/to/inputfiles -name "*.txt" -exec /path/to/myprogram.sh {} \; > Out.txt
Run Code Online (Sandbox Code Playgroud)

或者在 bash(或任何类似 Bourne 的 shell)中:

for i in *.txt; do /path/to/myprogram.sh "$i"; done > Out.txt
Run Code Online (Sandbox Code Playgroud)

(我使用 for-loop 或 find 因为如果您有 1000 个输入文件而不是示例中的 4 个文件,它会更方便)