背景
ispell是linux中的一个基本命令行拼写程序,我想调用它来获取以前收集的文件名列表。例如,这些文件名是从乳胶根文件递归收集的。当需要拼出所有递归包含的乳胶文件,而不是其他文件时,这非常有用。然而,从命令行调用ispell并不是一件小事,因为ispell给出了表单的错误
“还不能处理非交互使用。”在某些情况下。
(另一方面,理想情况下,我希望使用processbuilder类从java以编程方式调用ispell,而不需要bash。然而,同样的错误似乎困扰着这种方法。)
问题
为什么ispell会给出错误“还不能处理非交互使用”。在某些情况下,当从包含read方法的循环中调用bash时,但在其他情况下不会,如下面的代码示例所示?
下面的最小代码示例创建两个小文件
(testfileone.txt,testfiletwo.txt)和包含两个创建文件(testfilelistemp.txt)路径的文件。
接下来,以三种不同的方式调用ispell以获取testfileslisttemp.txt:
一。在“猫”的帮助下
2.首先将名称收集为字符串,然后在收集的字符串中的子字符串上循环,并为每个子字符串调用ispell。
三。通过直接循环testfileslisttemp.txt的内容,以及
为提取的路径调用ispell。
对于某些reaon,第三种方法不起作用,并产生一个错误
“还不能处理非交互使用。”为什么这个错误
发生,以及如何预防,和/或是否有其他变化
第三种方法是不会出错的?

#!/bin/bash

#ispell ./testFiles/ispellTestFile1.txt

# Creating two small files and a file with file paths for testing
printf "file 1 contents" > testFileOne.txt
printf "file 2 contents. With a spelling eeeeror." > testFileTwo.txt
printf "./testFileOne.txt\n./testFileTwo.txt\n" > testFilesListTemp.txt

COLLECTED_LATEX_FILE_NAMES_FILE=testFilesListTemp.txt


# Approach 1: produce list of file names with cat and
# pass as argumentto ispell
# WORKS
ispell $(cat $COLLECTED_LATEX_FILE_NAMES_FILE)

# Second approach, first collecting file names as long string,
# then looping over substrings and calling ispell for each one of them
FILES=""
while read p; do
echo "read file $p"
FILES="$FILES $p"
done < $COLLECTED_LATEX_FILE_NAMES_FILE

printf "files list: $FILES\n"

for latexName in $FILES; do
    echo "filename: $latexName"
    ispell $latexName
done


# Third approach, not working
# ispell compmlains in this case about not working in non-interactive
# mode
#: "Can't deal with non-interactive use yet."
while read p; do
    ispell "$p"
done < $COLLECTED_LATEX_FILE_NAMES_FILE

最佳答案

第三个示例不起作用,因为您重定向了标准输入。ispell需要终端和用户交互。当您编写这样的代码时:

while read p; do
    ispell "$p"
done < $COLLECTED_LATEX_FILE_NAMES_FILE

循环中任何程序从标准输入读取的所有内容都将从$COLLECTED_LATEX_FILE_NAMES_FILE文件中获取。ispell检测到并拒绝操作。但是,您可以使用“描述重定向”从文件中读取read p,并从“真实”终端中读取ispell "$p"。只要做:
exec 3<&0
while read p; do
    ispell "$p" 0<&3
done < $COLLECTED_LATEX_FILE_NAMES_FILE

exec 3<&0“复制”(保存)标准输入(0,即“终端”)到描述符3。稍后,通过键入ispell,将标准输入(0)从该描述符重定向到0<&3(如果愿意,可以省略0)。

关于linux - 从脚本运行ispell时如何理解和避免非交互模式错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35063210/

10-12 03:41