0

I am trying to insert a row in MySQL table using bash, I have saved the below script as script.sh and I run using

bash script.sh

When the script is executed, it is successful with no errors but the password field which contains an encrypted value('$2b$10$FywS3Lc27let0C9VVvZYFOBYg.AwGA3VEtUs5YIAjjUWSSEI3Fqt2') is stored as some other value like

b0.AwGA3VEtUs5YIAjjUWSSEI3Fqt2

instead of

$2b$10$FywS3Lc27let0C9VVvZYFOBYg.AwGA3VEtUs5YIAjjUWSSEI3Fqt2

I tried using '' and "" and `` , all of them didn't work

So how do I insert when having an $ value ?

#!/bin/bash
mysql <<EOF
GRANT ALL PRIVILEGES ON *.* TO "test_mysql_user"@"localhost" IDENTIFIED BY "test_password";

FLUSH PRIVILEGES;
create database cricket;
use cricket;

//created tables 
.....
.....

INSERT INTO admin
(
admin_id,
first_name,
middle_name,
last_name,
email_id,
password,
address,
city,
state,
zip_code,
country,
phone_code,
phone_number,
email_campaign)
VALUES
(
'root',
'root',
'root',
'root',
'[email protected]',
'$2b$10$FywS3Lc27let0C9VVvZYFOBYg.AwGA3VEtUs5YIAjjUWSSEI3Fqt2', //this field is the error , saved as someother value
'root',
'root',
'root',
'root',
'root',
'root',
'root',
'root');
EOF

1
  • consider using source command. That may be a quick workaround. Commented Jul 28, 2020 at 1:55

1 Answer 1

2

You have to quote the 'EOF' string for if you want no parameter substitution.

From man bash | less +/"Here Documents"

Here Documents

This type of redirection instructs the shell to read input from the current source until a line containing only word (with no trailing blanks) is seen. All of the lines read up to that point are then used as the standard input for a command.

The format of here-documents is:

<<[-]word
        here-document
delimiter

No parameter expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word. If any characters in word are quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded. If word is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion. In the latter case, the character sequence <newline> is ignored, and \ must be used to quote the characters , $, and `.

If the redirection operator is <<-, then all leading tab characters are stripped from input lines and the line contain- ing delimiter. This allows here-documents within shell scripts to be indented in a natural fashion.

So, your script should look like this when you quote the 'EOF' string

mysql <<'EOF'
GRANT ALL PRIVILEGES ON *.* TO "test_mysql_user"@"localhost" IDENTIFIED BY "test_password";

FLUSH PRIVILEGES;
create database cricket;
use cricket;

//created tables 
.....
.....

INSERT INTO admin
(
admin_id,
first_name,
middle_name,
last_name,
email_id,
password,
address,
city,
state,
zip_code,
country,
phone_code,
phone_number,
email_campaign)
VALUES
(
'root',
'root',
'root',
'root',
'[email protected]',
'$2b$10$FywS3Lc27let0C9VVvZYFOBYg.AwGA3VEtUs5YIAjjUWSSEI3Fqt2', 
'root',
'root',
'root',
'root',
'root',
'root',
'root',
'root');
EOF

If quoting word is not an option for you and want to avoid the parameter substitution just for the encrypted password field, store the encrypted value in a variable and use that variable in the doc.

password='$2b$10$FywS3Lc27let0C9VVvZYFOBYg.AwGA3VEtUs5YIAjjUWSSEI3Fqt2'

mysql <<EOF
INSERT INTO admin
(
admin_id,
first_name,
middle_name,
last_name,
email_id,
password,
address,
city,
state,
zip_code,
country,
phone_code,
phone_number,
email_campaign)
VALUES
(
'root',
'root',
'root',
'root',
'[email protected]',
'$password',
'root',
'root',
'root',
'root',
'root',
'root',
'root',
'root');
EOF
Sign up to request clarification or add additional context in comments.

Comments

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.