linux - 从 Linux bash 脚本编译 Latex

标签 linux bash compiler-errors latex

我正在使用以下脚本在 Linux 命令行上将 Latex 文档编译为 pdf:

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

它可以工作,但即使 pdflatex 编译时出现错误,xdg-open 行也会运行并显示任何先前创建的 pdf 文件。

如何为最后一行代码添加条件语句,即 xdg-open 只有在 pdflatex 的前一行编译成功时才运行?否则,它应该给出错误消息并且不尝试显示任何 pdf 文件。 pdflatex 是否返回任何可以在 bash 脚本中检查的错误代码?感谢您的帮助。

最佳答案

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

应该可以解决问题。

编辑以解决评论中的问题:

当然,这可以通过将其他命令包装在子 shell 中来实现:

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

或者通过评估 if 语句中的返回码:

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

关于linux - 从 Linux bash 脚本编译 Latex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43713705/

相关文章:

linux - 不同回调同时访问同一代码块

c++ - Perl 5.14 源代码 - 示例程序失败

mongodb - 如何通过shell脚本执行mongo命令?

bash - 使用 [[ ]] 和 -eq 时出错

c++ - 模板问题 - 将函数作为参数传递时遇到问题(非静态成员函数的使用无效)

linux - 从给定列表中复制目标文件夹中存在的 linux 文件

linux - 在 csv 命令输出的列后选择值

bash - 如何在 cut 命令中循环变量范围

c++ - C3859 : Virtual memory range for PCH exceeded

c++ - 为什么 SFINAE 在它应该工作的地方导致编译器错误?