![bash를 사용하여 파일을 한 줄씩 추가하시겠습니까?](https://linux55.com/image/41273/bash%EB%A5%BC%20%EC%82%AC%EC%9A%A9%ED%95%98%EC%97%AC%20%ED%8C%8C%EC%9D%BC%EC%9D%84%20%ED%95%9C%20%EC%A4%84%EC%94%A9%20%EC%B6%94%EA%B0%80%ED%95%98%EC%8B%9C%EA%B2%A0%EC%8A%B5%EB%8B%88%EA%B9%8C%3F.png)
현재 저는 결합된 파일에 내용을 추가하기 위해 여러 줄을 사용합니다.
./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
한 줄로 대체할 수 있나요?
답변1
(./myprogram 1.txt; ./myprogram 2.txt; ./myprogram 3.txt; ./myprogram 4.txt) > out.txt
답변2
이는 수행하려는 작업과 프로그램이 입력 매개변수를 처리하는 방법에 따라 다릅니다.
/path/to/myprogram.sh
하지만 다음과 같은 사람이 있다고 가정해 보겠습니다 .
#!/bin/bash
echo "Processing file ${1?missing input file}"
다음을 수행할 수 있습니다.
find /path/to/inputfiles -name "*.txt" -exec /path/to/myprogram.sh {} \; > Out.txt
또는 bash(또는 Bourne과 유사한 쉘)에서:
for i in *.txt; do /path/to/myprogram.sh "$i"; done > Out.txt
(예제에서 4개의 파일 대신 1000개의 입력 파일이 있으면 훨씬 더 편리하기 때문에 for 루프나 find를 사용하고 있습니다.)
답변3
Per의 답변과 매우 유사하지만 원래 레이아웃을 유지합니다.
{
./myprogram 1.txt
./myprogram 2.txt
./myprogram 3.txt
./myprogram 4.txt
} > Out.txt
http://www.gnu.org/software/bash/manual/bashref.html#Command-Grouping
답변4
나는 이 솔루션이 더 우아하다고 생각했습니다.
FILE=/tmp/file.log
exec >> $FILE
./myprogram 1.txt
./myprogram 2.txt
./myprogram 3.txt
./myprogram 4.txt
exec 1<&2
echo 'Normal output has returned!'