比 shell 轉義更安全的方法來解決問題

比 shell 轉義更安全的方法來解決問題

我有一個遵循以下命名約定的檔案資料夾:「<id>-<name>」。我需要從乳膠文件中引用它們,但“<name>”經常更改,因此我需要僅使用“<id>.

我已經成功地透過啟用shell-escape並使用\input{|"<command>"}, 運行一個 shell 命令來做到這一點,該命令從 id 獲取完整的文件名。它可以工作,但啟用shell-escape讓我擔心,因為這個系統會時不時地編譯我沒有製作、也沒有閱讀過的檔案。

任何人都可以想到更好的解決方案,或者是否可以以某種方式使啟用shell-escape安全?

答案1

ls您可以使用(或在 Windows 中)產生檔案清單dir並將其傳遞給 tex,但如果您需要在 tex 內執行此操作,則texosquery預設允許執行:

目前目錄的 test 子目錄包含三個文件

$ ls testdir
a-b.txt  a-c.txt  z-a.txt

這個文件

\documentclass{article}
\usepackage{texosquery}
\begin{document}

\TeXOSQueryRegularFileList\zzz{,}{testdir}
\show\zzz

\end{document}

產生終端輸出

$ pdflatex dd124
This is pdfTeX, Version 3.14159265-2.6-1.40.21 (TeX Live 2020) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
(./dd124.tex
LaTeX2e <2020-02-02> patch level 5
L3 programming layer <2020-08-07>
(/usr/local/texlive/2020/texmf-dist/tex/latex/base/article.cls
Document Class: article 2019/12/20 v1.4l Standard LaTeX document class
(/usr/local/texlive/2020/texmf-dist/tex/latex/base/size10.clo))
(/usr/local/texlive/2020/texmf-dist/tex/latex/texosquery/texosquery.sty
(/usr/local/texlive/2020/texmf-dist/tex/latex/texosquery/texosquery.tex
(/usr/local/texlive/2020/texmf-dist/tex/latex/texosquery/texosquery.cfg)))
(/usr/local/texlive/2020/texmf-dist/tex/latex/l3backend/l3backend-pdfmode.def)
(./dd124.aux) (|texosquery-jre8 -ir , testdir )
> \zzz=macro:
->a-b.txt,a-c.txt,z-a.txt.
l.6 \show\zzz
             
? 

\zzz逗號分隔的檔案名稱清單也是如此,替換\show為您需要執行的對該清單的任何循環。

相關內容