I am working on an option which will be able to remove the line specified if the user types in the exact title and author.
However I will not be able to make it work
Using single quotes does not interpolate variables. Try something like this -
fnRemoveBook()
{
echo "Title: "
read Title
echo "Author: "
read Author
if grep -Fqe "$Title:$Author" BookDB.txt; then
sed -i "/$Title:$Author/ d" BookDB.txt # <---- Changes made here
echo "Book removed successfully!"
else
echo "Error! Book does not exist!"
fi
}
You're trying to pass a bash
variable (two, actually) into a sub-program (sed
), but surrounding the expression you're passing in single-quotes, which does no parameter expansion. That means that sed
is literally seeing $Title:$Author
. Try putting the whole sed
expression in double-quotes:
sed -i "/$Title:$Author/d" BookDB.txt
This will allow bash
to expand $Title
and $Author
before they get into sed
.