Compiling Latex from Linux bash script

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, ie 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.


#! /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
链接地址: http://www.djcxy.com/p/33382.html

上一篇: Geany1.32使用Miktex2.9不能编译biblatex

下一篇: 从Linux bash脚本编译Latex