bashshellscriptingpdf-generationgroff

How to make a loop to compile groff documents into a specific folder


I have several .ms documents scattered in different folders and I would like to make a script which compiles each .ms document and output the corresponding .pdf file in a specific folder.

The folder structure is as follow:

/home/user/foo/folder1/file1.ms
/home/user/foo/folder2/file2.ms
/home/user/foo/folder3/file3.ms
...

Here is what I have done so far:

#!/bin/bash
folder="/home/user/foo/"
file=$(du -a "$folder" | grep ".ms" | cut -f2- | sort)

However I don't know how to make the loop that is going to turn each line of the output into the corresponding .pdf file e.g:

/home/user/foo/bar/file1.pdf
/home/user/foo/bar/file2.pdf
/home/user/foo/bar/file3.pdf
...

The groff command I use to compile .ms to .pdf is:

groff -k -T pdf -ms file.ms > file.pdf

EDIT

Thanks to @dash-o answer I have updated my script to this :

#!/bin/bash
input="/home/user/foo/"
output="/home/user/foo/bar"
find $input -name '*.ms' -execdir sh -c 'groff -k -T pdf -ms {} > $output$(basename {} .ms).pdf' \;

However this compiles only the first document and does not place it in the "output" directory but in the same directory as the .ms file and I get "1: Syntax error: ")" unexpected".


Solution

  • #!/bin/bash
    input="/home/matthieu/Documents/Philosophie/L1"
    output="/home/matthieu/Documents/Philosophie/L1/S1/Cours"
    find "$input" -name '*.ms' -execdir sh -c 'groff -k -T pdf -ms "$1" > "$2/${1%.ms}.pdf"' sh {} "$output" \;