背景
I拼写是linux中的一个基本命令行拼写程序,我想为以前收集的文件名列表调用它。例如,这些文件名是从乳胶根文件中递归收集的。这是有用的,当需要拼写所有递归包括的胶乳文件,而没有其他文件。但是,从命令行调用ispell是非常重要的,因为ispell给出了表单的错误,“还不能处理非交互的使用”。在某些情况下。
(作为另一个方面,理想情况下,我希望使用ProcessBuilder类从java编程调用ispell,而不需要bash。然而,同样的错误似乎困扰着这种方法。)
问题
为什么ispell给出的错误“还不能处理非交互的使用”。在某些情况下,从包含read方法的循环中调用bash时,在其他情况下不会调用,如下面的代码示例所示?
下面的最小代码示例创建两个小文件(testFileOne.txt、testFileTwo.txt)和一个包含两个已创建文件(testFilesListTemp.txt)路径的文件。接下来,以三种不同的方式为testFilesListTemp.txt调用ispell : 1.在"cat“2的帮助下,首先将名称作为字符串收集起来,然后循环遍历收集到的字符串中的子字符串,然后为每个字符串调用ispell。3.直接遍历testFilesListTemp.txt的内容,调用ispell提取路径。
对于某些情况,第三种方法不起作用,并产生一个错误“还不能处理非交互的使用”。为什么会发生这个错误,如何防止它,和/或第三种方法的另一种变体,可以在没有错误的情况下工作?
#!/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 发布于 2016-01-28 22:10:16
第三个示例不起作用,因为您重定向了标准输入。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。稍后,通过键入0<&3 (如果愿意,可以省略0),将标准输入(0)从该描述符重定向到ispell。
https://stackoverflow.com/questions/35063210
复制相似问题