首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Linux Bash脚本grep错误

Linux Bash脚本grep错误
EN

Stack Overflow用户
提问于 2018-04-05 19:34:20
回答 1查看 160关注 0票数 0

所以我和我的教授花了大约两个小时的时间来研究这个问题,但是我不知道问题出在哪里,所以我希望有人能看到我们错过了什么。

代码语言:javascript
运行
复制
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,并使用我的故障排除消息获得这个错误消息。

代码语言:javascript
运行
复制
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

Grep在带有绝对路径的BASH脚本之外也工作得很好。测试了if语句,如果我取出-eq,它确实工作正常,因为它读取grep确实成功运行,只是没有找到目录。据我所知,它忽略了我的文件,而是使用字符串搜索作为目录。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-05 21:10:39

对grep的“-q”论点中的破折号部分是一个特殊的非ascii字符,可能是在UTF-8中的一个短跑,我没有看得太仔细。Grep不将en-破折号解释为启动选项,并在文件列表中搜索字符串‘-q’。“比萨饼”在文件列表中。

如果您从网页或word文档复制代码,这种情况很容易发生。无论如何,删除-q并重新键入它,您的脚本应该工作得更好。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49680178

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档