JamesPoppycock JamesPoppycock - 5 months ago 22
Bash Question

Grep 2 variables with a ":" in between

I am currently stuck on this piece of code for a long time now. Below is my code.

if grep -ixqF $bookName:$authorName BookDB.txt
then echo "Book found"
echo "Book not found"

BookDB basically contains Book title and author information as shown below, where syntax is "Book-name:Author".

Mary had a little nose:JamesPoppcock
Merry Christmas:Christ Sake
Kinky Asians:Walter Bedding

However, the program always output "Book found", even if the book and author is not in the BookDB.txt. What I want from this program is that it will read in 2 user input,
and match it exactly (including the semi-colon) to the database.

Any help will be greatly appreciated!

Edit: The program always output "Book not found", instead of "Book found"


Quote the variable expansion portion:

grep -ixqF "$bookName:$authorName" BookDB.txt

Without quoting the expansion is suffering from word splitting (and glob expansion) and only the first word is taken as a pattern to search and the rest are being treated as file name(s) along with BookDB.txt.