簡體   English   中英

從 bash-script 運行 sql-command?

[英]Run sql-command from bash-script?

我創建了一個必須更改表引擎的小腳本:

#!/bin/bash

echo "use test_1;" > query.sql

get_list () {
mysql -u teamcity -ppassword -B -N -e 'show tables like "%"' test_1 | xargs -I '{}'     echo "alter table {} engine=innodb;" >> query.sql
}

get_list ;

mysql -u teamcity -ppassword < query.sql

但是 - 如何避免使用query.sql文件? 我用“這里的文檔”做了幾次嘗試 - 但無法解決它......

例如 - 試試這個:

#!/bin/bash

get_list () {
mysql -u teamcity -ppassword -B -N -e 'show tables like "%"' test_1 | xargs -I '{}'     echo "alter table {} engine=innodb;"
}

a="$(get_list)"
b="use test_1;"
c="$b $a"

mysql -u teamcity -ppassword <<< $c

但它不起作用......

將所有內容放在一個函數和管道中:

#!/bin/bash
get_list () {
  echo "use test_1;"

  mysql -u teamcity -ppassword -B -N -e 'show tables like "%"' test_1 | xargs -I '{}' echo "alter table {} engine=innodb;"
}

get_list | mysql -u teamcity -ppassword 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM