0

I want to update database from bash command:

#!/bin/bash
list= cat /home/wwwroot/list.txt;
mysql -u root -D dbname -e "UPDATE wp_postmeta SET meta_value = '$list' WHERE meta_key = 1";

cat /home/wwwroot/list.txt
text1
text2
text3
...

How to $list variable can be read in sql query?

Thank you.

5
  • You'll need to provide more sample data so we understand exactly what you mean by "update database from cat command", but you may find my answer stackoverflow.com/a/8649732/620097 to give you a place to start on working with SQL and shell scripting. Good luck. Commented Nov 26, 2017 at 4:45
  • If the file is on the same host where the MySQL Server is running, you can do this more easily and more securely with the LOAD_FILE() function. Commented Nov 26, 2017 at 4:47
  • 2
    list=$(cat /home/wwwroot/list.txt) Commented Nov 26, 2017 at 5:54
  • @Bill Karwin get result NULL. Commented Nov 26, 2017 at 7:02
  • @janos worked, thank you so much. Commented Nov 26, 2017 at 7:04

1 Answer 1

3

Cat is an overkill here.


I suggest using an array if you need space delimited values.

list=($(</home/wwwroot/list.txt))
mysql -u root -D dbname -e "UPDATE wp_postmeta \
SET meta_value = '${list[@]}' WHERE meta_key = 1";

The advantage here is if you need comma separated values then you could employ bash [ parameter substitution ] to achieve your feet. Just change the query to

mysql -u root -D dbname -e "UPDATE wp_postmeta \
SET meta_value = '${list[@]/%/,}' WHERE meta_key = 1";
#Note ${list[@]/%/,} appends comma after each value in the text file

All good :-)

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.