0

So my professor and I worked on this for about 2 hours and couldn't figure out what the problem was so I am hoping someone can see what we missed.

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

and when I do selection 2 I get to the grep and get this error message with my troubleshooting messages.

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 also works just fine outside the BASH script with the absolute paths. Tested the if statement and if I take out the -eq it does work properly because it reads that grep did successfully run just that the directory was not found. From what I can tell it is ignoring my file and instead is using the string search as the directory.

6
  • 1
    We ask that questions here have a minimal reproducible example -- the shortest possible code that others can run to produce a problem themselves, with everything unrelated to the specific issue removed. If you're having trouble figuring out exactly where your unexpected behavior happens, run bash -x yourscript to log each line as it runs, and trace through that to find the first line that either (1) is passed a different argument than you expect, or (2) has a different result than you expect. Commented Apr 5, 2018 at 19:39
  • BTW, in general, instead of foo; if [ $? -eq 0 ]; then you should just do if foo; then. Commented Apr 5, 2018 at 19:41
  • STRONG SUGGESTION: bash -x yourscript.sh. The "-x" will help you see how your "grep -q" is actually being expanded ... and should quickly show you what's actually going wrong. Here are more details: tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html Commented Apr 5, 2018 at 19:41
  • 1
    @paulsm4, please don't link TLDP's documentation -- the ABS in particular is full of outdated information and bad-practice examples. The BashGuide and the bash-hackers' wiki are far more reliable. wiki.bash-hackers.org/scripting/debuggingtips is the page on debugging in the latter, or mywiki.wooledge.org/BashGuide/… in the former. Commented Apr 5, 2018 at 19:42
  • How can grep –q "$searchstring" "$file" appear in you stdout? Commented Apr 5, 2018 at 19:54

1 Answer 1

3

The dash part of the '-q' argument to grep is a special non-ascii character, probably an en-dash in UTF-8, I didn't look too hard. Grep doesn't interpret the en-dash as starting an option and does a search for the string '–q' in the file list. 'pizza' is in the file list.

This can happen easily if you copy code from a web page or a word doc. Anyway, delete the -q and retype it and you script should work better.

Sign up to request clarification or add additional context in comments.

4 Comments

Good eyes. A little copy-and-paste confirms it's an en-dash (unicode U+2013, UTF8: e2 80 93). Also some advice for @ScottSmith: don't edit scripts in text editors that "helpfully" convert dashes, quotes, etc to their fancy unicode versions.
None of it was copied, the -q is quiet mode and is an option, it works just fine outside of the Bash script and in my professor's code too. thanks for the advice but on our system -q isn't an issue. I was having a permissions issue on one of my directories but I changed it and am still getting this error.
EDIT: I misread that and thought you said it wasn't an option I'm sorry and you were right. I used my number pad on my keyboard and for some reason on it - and - are different when typed. I even tested it. I deleted and retyped with the numbers and symbols on the keyboard instead and it worked, retried it with my number pad and went back to the same issue. I find that to be really weird but I appreciate the help.
Interesting, I've never heard of a keyboard generating a non-ascii dash from either position, I'm glad that can happen, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.