gpt4 book ai didi

Linux Bash 脚本 grep 错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:03:48 24 4
gpt4 key购买 nike

所以我和我的教授为此工作了大约 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

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

最佳答案

grep 的 '-q' 参数的破折号部分是一个特殊的非 ascii 字符,可能是 UTF-8 中的短破折号,我没仔细看。 Grep 不会将 en-dash 解释为开始选项,而是在文件列表中搜索字符串 '–q'。 “披萨”在文件列表中。

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

关于Linux Bash 脚本 grep 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49680178/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com