我有一个MySQL查询,需要在bash脚本中多次运行
它有一个很长的正则表达式列表
我希望避免在脚本中多次重复长列表
代码:
select * from tableName where fieldName regexp concat(
... big long list of regular expressions ...
);
下一个MySQL命令将具有不同的选择条件
其他MySQL命令将删除而不是选择
但是正则表达式的列表保持不变
我尝试使用bash source命令,但MySQL对此进行了解释,所以我认为这必须是一个MySQL命令
所以我该怎么做。。。
select * from tableName where fieldName regexp concat(
[import regular expression list from file]
);
提前谢谢
更新-bash尝试:
script1:
#!/bin/bash
mysql -uusername -ppassword -D dbname -e "select * from tablename where fieldname regexp concat(
source /scripts/script2
);"
script2:
'cat|',
'dog|',
'fish'
错误:
ERROR 1054 (42S22) at line 1: Unknown column 'source' in 'where clause'
连接所需的代码段,然后将批量传递给SQL。
#!/bin/bash
sed '1s/^/select * from tablename where fieldname regexp concat(/
$s/$/);/' /scripts/script2 |
mysql -uusername -ppassword -D dbname
(要查看sed
脚本的作用,请注释掉MySQL的管道。)
或者相当于
#!/bin/bash
mysql -uusername -ppassword -D dbname <<____HERE
select * from tablename where fieldname regexp concat($(cat /scripts/script2));
____HERE
但是,如果您的here文档包含任何shell元字符(如此处的星号),您将面临出现奇怪副作用的风险。在最坏的情况下,可能会将其分为引用部分和未引用部分。
或者,用一个有点愚蠢的变通方法解除它的武装:
#!/bin/bash
splat='*'
mysql -uusername -ppassword -D dbname <<____HERE
select $splat from tablename where fieldname regexp concat($(cat /scripts/script2));
____HERE