It isn't supposed to respond. It is doing exactly what it should: it reads the input and assigns it to the variable fileType
. However, you then have a while
loop which is checking the value of a variable that never changes:
while [[ "$fileType" != "EXIT" ]];
Since the value of $fileType
is set only once and before the while
loop, that while
becomes an infinite loop unless you pass EXIT
on the first try. In any case, your script wouldn't work anyway since the output of grep -q pattern file
will never be 1
. In fact, it will always be empty since that's what grep -q
does. I suspect you meant to check the exit status of the command but, even that is pointless: there is no benefit in reading the entire file once with grep -q
only to then read it all over again with grep -F
.
Here's a working version of your function:
function findFiles() {
while [[ "$fileType" != "EXIT" ]];
do
echo "Enter file type to search for (include '.') || Type EXIT to terminate program "
read fileType
grep -F "$fileType" index.html >> foundFiles.txt
done
rm index.html
};
Or, if you want to avoid creating an empty foundFiles.txt
if no matches were found, you can try:
function findFiles() {
while [[ "$fileType" != "EXIT" ]];
do
echo "Enter file type to search for (include '.') || Type EXIT to terminate program "
read fileType
if grep -qm1 "$fileType" index.html; then
grep -F "$fileType" index.html >> foundFiles.txt
fi
done
rm index.html
};
The -m
ensures the grep
exits after the first match so you don't need to read the whole file twice. Note how I am not using the command substitution ($(command)
) syntax since I am checking that the command was successful, and not trying to use its output.