簡體   English   中英

Linux Bash腳本grep錯誤

[英]Linux Bash script grep error

因此,我和我的教授進行了大約2個小時的研究,無法弄清問題出在哪里,所以我希望有人能看到我們錯過的內容。

askDelete()
{
echo -e "  Still want to delete it? (y/n)\n"
read answer
if [ "$answer" = 'y']; then
    rm $1
else
    echo -e "\nFile was not removed\n"
fi
}

#############################################
clear

#script starts here

echo -e "\n\tCleaner Script\n"

dir=`pwd`

while [ "$choice" -ne 3 ] || [ "$choice" != "quit" ]
    do

    echo -e "\nEnter 1 to delete by filename or type the word file."
    echo -e "\nEnter 2 to delete by a string within a file or type the word string"
    echo -e "\nEnter 3 or quit to exit this program.\n"
    read choice

            case "$choice" in
                    1|"file") echo -e"Enter the name of the file to delete: "
                            read file
                            result=$(find . -name "$file")
                            if [ -z $result ]; then
                                    echo "File not found"
                            else
                                    askDelete $file
                            fi
                            ;;

                    2|"string") echo -e "Enter the sting to delete the files that contain it: "
                                    read searchstring
                                    result=$(find $dir -type f -perm /400)
                                    echo $result
                                    for file in $result;
                                    do
                                            echo -e "String is $searchstring \nFile is $file"
                                            grep –q "$searchstring"  "$file"
                                            if [ $? -eq 0 ]; then
                                                    echo "****MATCH****"
                                                    askDelete $file
                                            fi
                                    done
                            ;;

                    3|"quit") echo -e "Exiting program"
                            break;;
                    *) echo -e "\nChoice not listed";;
            esac
done

當我執行選擇2時,我會轉到grep,並獲得帶有故障排除消息的錯誤消息。

Enter the sting to delete the files that contain it:
pizza
/home/hopper/z#/CSCI/CSCI330/Assignments/assign4/smith.txt 
/home/hopper/z#/CSCI/CSCI330/Assignments/assign4/data2.txt 
/home/hopper/z#/CSCI/CSCI330/Assignments/assign4/jones2.txt 
/home/hopper/z#/CSCI/CSCI330/Assignments/assign4/cleaner.sh 
/home/hopper/z#/CSCI/CSCI330/Assignments/assign4/jones.txt 
/home/hopper/z#/CSCI/CSCI330/Assignments/assign4/data.txt
String is pizza
File is /home/hopper/z#/CSCI/CSCI330/Assignments/assign4/smith.txt
grep: pizza: No such file or directory
String is pizza
File is /home/hopper/z#/CSCI/CSCI330/Assignments/assign4/data2.txt
grep: pizza: No such file or directory
String is pizza
File is /home/hopper/z#/CSCI/CSCI330/Assignments/assign4/jones2.txt
grep: pizza: No such file or directory
String is pizza
File is /home/hopper/z#/CSCI/CSCI330/Assignments/assign4/cleaner.sh
grep: pizza: No such file or directory
/home/hopper/z#/CSCI/CSCI330/Assignments/assign4/cleaner.sh:                                              
grep –q "$searchstring"  "$file"
String is pizza
File is /home/hopper/z#/CSCI/CSCI330/Assignments/assign4/jones.txt
grep: pizza: No such file or directory
String is pizza
File is /home/hopper/z#/CSCI/CSCI330/Assignments/assign4/data.txt
grep: pizza: No such file or directory

在具有絕對路徑的BASH腳本之外,Grep也可以正常工作。 測試了if語句,如果我取出-eq,它會正常工作,因為它會讀取grep確實成功運行,只是找不到目錄。 據我所知,它忽略了我的文件,而是使用字符串搜索作為目錄。

grep的'-q'參數的破折號部分是一個特殊的非ASCII字符,可能是UTF-8中的破折號,我看起來並不難。 Grep不會將破折號解釋為開始選項,而是在文件列表中搜索字符串“ –q”。 “ pizza”在文件列表中。

如果您從網頁或Word文檔中復制代碼,則很容易發生這種情況。 無論如何,刪除-q並重新輸入它,腳本應該會更好地工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM