0

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';
6
  • 2
    I don't see how that's being used as input to mysql in the first place. You need to use << to make it a here-document. Commented Jan 25, 2022 at 18:56
  • @Barmar Thanks for reply. I am a beginner. I have no idea what you mean :-) It took about ten hours for me to get that what you see. I looked up here documents, but I don't really understand how I should use it in this case. But thanks anyway. Commented Jan 25, 2022 at 19:54
  • this and this may help. Commented Jan 25, 2022 at 20:21
  • I don't understand how your script is doing anything at all if you don't use a here document. When it executes mysql it's going to wait for you to type commands, it won't treat the following lines as SQL commands. Commented Jan 25, 2022 at 20:25
  • 1
    It would do that if you typed those commands by hand, it shouldn't write anything to the DB if you run it as a script. Commented Jan 25, 2022 at 20:56

1 Answer 1

1

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';"
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. That worked!
From manpage for mysql command --execute=statement, -e statement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.