Search code examples
bashcurlmariadb

Update MariaDB table with variable from curl in bash script


I have read literally every answer on the net that I could find. Nothing is similar to my problem, so here it is:

I have a bash script with curl and I get a variable back. I want to update my database with this variable, however it doesn't work.

My variable is $stream and no matter what I do, I always get the word "$stream" into the database instead of the result of the curl.

My script is:

#!/bin/bash
    
stream=$(curl --silent "https://player.mediaklikk.hu/playernew/player.php?video=mtv1live&noflash=yes&output=vast&unviewed_position_start=1&env=vp&impl=s&correlator=&osfamily=Windows&osversion=10&browsername=Chrome&browserversion=97.0.4692.99&title=M1&contentid=mtv1live&embedded=0" | grep -Po 'file": "\K(.*?)(?=")' | sed 's/\\\/\\\//https:\\\/\\\//g')
    
echo $stream

mysql
use mydatabase;

UPDATE my_table SET my_url = "$stream" WHERE my_name = 'stream_name';

Solution

  • You can use the -e option to execute a query. Put this in double quotes and variables will be expanded.

    #!/bin/bash
    
    stream=$(curl --silent "https://player.mediaklikk.hu/playernew/player.php?video=mtv1live&noflash=yes&output=vast&unviewed_position_start=1&env=vp&impl=s&correlator=&osfamily=Windows&osversion=10&browsername=Chrome&browserversion=97.0.4692.99&title=M1&contentid=mtv1live&embedded=0" | grep -Po 'file": "\K(.*?)(?=")' | sed 's/\\\/\\\//https:\\\/\\\//g')
    
    echo $stream
    
    mysql mydatabase -e "UPDATE my_table SET my_url = '$stream' WHERE my_name = 'stream_name';"