Shell – How to submit the input filename into find exec correctly

latexmakeshell

I want to create two pdf output files of a single LaTeX source.

One output file is public and the other one (with further information) becomes private.

I use a make file, which uses find to grep the tex file in the directory. This is a simple solution because this way I can reuse the makefile for many projects without needing to modify its content.

This is the important part of the makefile.

all:    
        # This creates the public output file
        find -name *.tex -exec sh -c 'pdflatex {}' \;

Now I want to add a further line to create the private output file.
It should look something like this:

all:    
        # This creates the public output file
        find -name *.tex -exec sh -c 'pdflatex {}' \;
        # This creates the private output file
        find -name *.tex -exec sh -c 'pdflatex --jobname=ABC  '\def\privatflag{}\input{XYZ}' {}' \;

For ABC I look for a solution to specify the default filname but with a prefix.

For XYZ I look for a solution to pass the input filename here.

The usage of the inner quotation marks is also not correct here I think.

Update 1: Maybe I can explain the problem more simple way.

This command works in the command shell:

pdflatex --jobname=outputfile '\def\privatflag{}\input{inputfile.tex}'

But I'm looking for a solution to use it with find -name *.tex -exec so that I don't need to specify the intput filename inputfile.tex.

Additionally I look for a way that I don't need to specify --jobname=outputfile. It should match the input filename with an additional prefix.

Update 2: Thanks to muru and Stéphane Chazelas, the issue is solved.

This is now the important part of the makefile

all:    
        # This creates the public output file
        find -name *.tex -exec sh -c 'pdflatex {}' \;
        # This creates the private output file
        find . -name '*.tex' -execdir sh -c 'pdflatex --jobname=privat_"$${1##*/}" "\def\privatflag{""}\input{$${1##*/}}"' {}-job {} \;

Best Answer

From your example, I think what you need is:

find . -name '*.tex' -execdir sh -c 'pdflatex --jobname=foo"${1##*/}" "\def\privatflag{""}\input{${1##*/}}"' {}-job {} \;

To break it down:

  • -execdir runs the command in the directory where the file was found.
  • ${1##*/} strips the path from the argument given by find.
  • The "" in {} is to prevent find from replacing {} with the matched path.

sh -c is need to process the path given by find and extract just the filename.

Related Question