2016-01-28 27 views
1

背景如何從腳本運行ispell時瞭解並避免非交互式模式錯誤?

的ispell是在linux基本命令行的拼寫程序,我想呼籲的文件名以前收集的名單。例如,這些文件名是從乳膠根文件遞歸收集的。當需要拼寫所有遞歸包含的latex文件時,這是有用的,並且不需要其他文件。然而,從命令行調用ispell事實證明是不平凡的,因爲ispell會給出 形式的錯誤「尚無法處理非交互式使用」。在某些情況下。

(作爲一個方面不行,我非常希望從Java使用的ProcessBuilder類ispell的編程方式調用,並且無需慶典。同樣的錯誤似乎然而,糾纏了這種方法。)

問題

爲什麼ispell會給出錯誤「無法處理非交互式使用」。在某些情況下,當從包含read方法的循環中調用bash時,在其他情況下不會調用,如下面的代碼示例所示?

的下面最小的代碼的示例創建兩個小文件 (testFileOne.txttestFileTwo.txt)和含有兩個創建的文件(testFilesListTemp.txt)的路徑中的文件。 接下來,ispell以三種不同的方式調用testFilesListTemp.txt: 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 

回答

0

第三個示例不起作用,因爲您重定向了標準輸入。 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)以後您重定向到ispell0<&3(如果你喜歡,你可以省略0)。