简体   繁体   English

bash从bash命令更新mysql

[英]bash update mysql from bash command

I want to update database from bash command: 我想从bash命令更新数据库:

#!/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 猫/home/wwwroot/list.txt
text1 文字1
text2 文字2
text3 文字3
... ...

How to $list variable can be read in sql query? 如何在SQL查询中读取$ list变量?

Thank you. 谢谢。

Cat is an overkill here. Cat在这里太过分了。


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. 这样做的好处是,如果需要逗号分隔的值,则可以使用bash [参数替换]来实现。 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 :-) 都很好:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM