Search code examples
bashsqliteif-statementvariablesclause

BASH VARIABLE ADEED AFTER SQLITE3 LIKE CLAUSE


I have a question about an sqlite3 command that im trying to insert inside my script

if [[ -n $(tail -z 600 /var/log/pihole.log | grep "$IP" | wc -c) ]];then

sudo -u pihole sqlite3 /etc/pihole/pihole-FTL.db 'DELETE FROM network WHERE hwaddr LIKE "$MAC"'

fi

But soon i run my program it actually doesn't do what it was made for !

I already try whit a real mac address instead of the variable $MAC and it works !

So my question is how can i actually attach a variable in this particular bash command and of my script !

Thank-you in advance really appreciated !


Solution

  • Your problem here is the usage of single quotes: in bash variables are not expanded in single quotes. You can solve this by either using double quotes or mixing single and double quotes (ensuring the variable you want expanded is in double quotes):

    $ MAC=1111
    $ echo "DELETE FROM network WHERE hwaddr LIKE \"$MAC\""
    DELETE FROM network WHERE hwaddr LIKE "1111"
    $ echo 'DELETE FROM network WHERE hwaddr LIKE '"\"$MAC\""
    DELETE FROM network WHERE hwaddr LIKE "1111"