Reputation: 905
I want my makefile to setup a mysql database. This is my procedure:
Retrieve user
, host
, password
and database
values from a config.json
file with python, and store them in a config
variable
Get each value stored in the config
variable with awk (each value is separated by two spaces)
Run some mysql with these values
This is my script:
all:
config=$(python3 -c 'import json; file = open("config.json"); config = json.load(file); file.close(); print(config["mysql"]["user"]+" "+config["mysql"]["host"]+" "+config["mysql"]["password"]+" "+config["mysql"]["database"])'); \
user=$(echo "$config" | awk -F " " '{print $1}'); \
host=$(echo "$config" | awk -F " " '{print $2}'); \
password=$(echo "$config" | awk -F " " '{print $3}'); \
database=$(echo "$config" | awk -F " " '{print $4}'); \
sudo mysql -e "CREATE DATABASE IF NOT EXISTS $database; GRANT ALL PRIVILEGES ON $database.* TO '$user'@'$host' IDENTIFIED BY '$password'"
If I type this directly in my console it works fine. Why doesn't it work when running make
?
This is my console output:
config=; config = json.load(file); file.close(); print(config["mysql"]["user"]+" "+config["mysql"]["host"]+" "+config["mysql"]["password"]+" "+config["mysql"]["database"])'); \
user=; \
host=; \
password=; \
database=; \
sudo mysql -e "CREATE DATABASE IF NOT EXISTS atabase; GRANT SELECT, INSERT, UPDATE, DELETE, INDEX, ALTER ON atabase.* TO 'ser'@'ost' IDENTIFIED BY 'assword'; USE atabase;"
/bin/sh: -c: line 0: syntax error near unexpected token `('
/bin/sh: -c: line 0: `config=; config = json.load(file); file.close(); print(config["mysql"]["user"]+" "+config["mysql"]["host"]+" "+config["mysql"]["password"]+" "+config["mysql"]["database"])'); user=; host=; password=; database=; sudo mysql -e "CREATE DATABASE IF NOT EXISTS atabase; GRANT ALL PRIVILEGES ON atabase.* TO 'ser'@'ost' IDENTIFIED BY 'assword'"'
make: *** [all] Error 2
Upvotes: 0
Views: 1778
Reputation: 461
$
are used for Makefile variable reference.
So before passing it to the shell, make expands $(python3 -c 'import json; file = open("config.json")
to nothing, since you are in fact asking for the expansion of the variable python3 -c 'import json; file = open("config.json"
(which of course does not exist). Same story for the other lines. To avoid that, you'll need to escape $
characters in the Makefile with another $
character.
Upvotes: 1
Reputation: 9664
$
does not make it from your Makefile into the shell it runs. Escape them using $$
to make sure they get passed through as intended.
Upvotes: 1