bashshellgroff

Run multiple commands with entr


I am attempting to use the 'entr' command to automatically compile Groff documents.

I wish to run the following line:

refer references.bib $1 | groff -ms $1 -T pdf > $2

Sadly it will only compile once if I try this:

echo $1 | entr refer references.bib $1 | groff -ms $1 -T pdf > $2

I have also tried the following, but it creates an infinite loop that cant be exited with Ctrl+C:

compile(){
    refer references.bib $1 | groff -ms $1 -T pdf > $2
}

while true; do
    compile $1 $2
    echo $1 | entr -pd -s 'kill $PPID'
done

What is the correct way of doing this?


Solution

  • I didn't try this because I didn't want to install entr. But I think the following should work:

    echo "$1" | entr sh -c "refer references.bib $1 | groff -ms $1 -T pdf > $2"
    

    Note that we run the pipe refer | groff in a shell to group it together. The command from your question without the shell runs refer upon file change, but groff only once. In entr ... | groff the groff part isn't executed by entr, but by bash in parallel.

    This command works only if $1 and $2 do not contain special symbols like spaces, *, or $. The correct way to handle these arguments would be ...

    echo "$1" | entr sh -c 'refer references.bib "$1" | groff -ms "$1" -T pdf > "$2"' . "$1" "$2"