我偶然看到了这篇文章
该帖子中的解决方案完全按照书面说明运行。但是我希望能够使用 shell 变量来选择它在运行时生成的文件的名称1 美元所以我可以在调用脚本时在命令行中选择文件名。
所以我尝试替换文件名我的文件.tex和$1.tex在整个 shell 脚本中:
#!/bin/bash
# Create a temporary directory
curdir=$( pwd )
tmpdir=$( mktemp -dt "latex.XXXXXXXX" )
# Set up a trap to clean up and return back when the script ends
# for some reason
clean_up () {
cd "$curdir"
[ -d "$tmpdir" ] && rm -rf "$tmpdir"
exit
}
trap 'clean_up' EXIT SIGHUP SIGINT SIGQUIT SIGTERM
# Switch to the temp. directory and extract the .tex file
cd $tmpdir
# Quoting the 'THEEND' string prevents $-expansion.
cat > $1.tex <<'THEEND'
\documentclass{article}
\begin{document}
Hello World
\end{document}
THEEND
# If the file extracts succesfully, try to run pdflatex 3 times.
# If something fails, print a warning and exit
if [[ -f '$1.tex' ]]
then
for i in {1..3}
do
if pdflatex $1.tex
then
echo "Pdflatex run $i finished."
else
echo "Pdflatex run $i failed."
exit 2
fi
done
else
echo "Error extracting .tex file"
exit 1
fi
# Copy the resulting .pdf file and .tex file to original directory, display .pdf file and exit
cp $1.pdf $curdir;
cp $1.tex $curdir;
evince $1.pdf
exit 0
这不起作用并给了我以下错误:
提取 .tex 文件时出错
我哪里做错了?
答案1
更换
如果[[-f'$1.tex']]
和
如果[[-f“$1.tex”]]
修正了错误。
需要双引号来扩展变量。