0
votes

I am using following script to compile Latex documents to pdf on commandline in Linux:

#! /bin/bash
NAME=`echo "$1" | cut -d'.' -f1`
pdflatex -file-line-error -halt-on-error $NAME.tex
xdg-open $NAME.pdf

It works, but even if there is error in compilation by pdflatex, xdg-open line runs and shows any previously created pdf file.

How can I add a conditional statement for last line of code, i.e. xdg-open should run only if compilation in previous line by pdflatex is successful? Else, it should give error message and not try to show any pdf file. Does pdflatex return any error code that can be checked in bash script? Thanks for your help.

1

1 Answers

2
votes
#! /bin/bash
NAME=`echo "$1" | cut -d'.' -f1`
pdflatex -file-line-error -halt-on-error $NAME.tex && xdg-open $NAME.pdf

Should do the trick.

Edit to address the question in the comment:

Sure, that works either with wrapping the other commands in a subshell:

pdflatex -file-line-error -halt-on-error $NAME.tex && ( xdg-open $NAME.pdf; command2; command 3 )

or by evaluating the return code in an if statement:

if [[ $? -eq 0 ]]; then
  xdg-open $NAME.pdf
  command2
  command3
fi