简体   繁体   English

如何在bash中转义mysqldump引号?

[英]How to escape mysqldump quotes in bash?

I need to take a backup of my database using mysqldump tool. 我需要使用mysqldump工具备份数据库。 My test table has a TIMESTAMP column that I have to use as filter. 我的测试表有一个TIMESTAMP列,我必须将其用作过滤器。 I'm using a bash script with the following code: 我正在使用带有以下代码的bash脚本:

#!/bin/bash
cmd="mysqldump --verbose --opt --extended-insert --result-file /home/backups/mysql/test/test_table.20161205.bak test --where=\"timestamp_c>='2016-12-03 00:00:00'\" --tables \"test_table\""
echo $cmd
$cmd

I'm printing the command that I assume should work. 我正在打印我认为应该可以使用的命令。 The above script produces the output: 上面的脚本产生输出:

mysqldump --verbose --opt --extended-insert --result-file /home/backups/mysql/test/test_table.20161205.bak test --where="timestamp_c>='2016-12-03 00:00:00'" --tables "test_table"

If I copy the printed command on the terminal, it works; 如果我在终端上复制了打印的命令,它将起作用; however, the command on the script print the error: 但是,脚本上的命令将显示错误:

mysqldump: Couldn't find table: "00:00:00'""

Is there something that I'm not understanding about quotes escape? 关于引号转义我是否不了解?

Variables hold data, not code. 变量保存数据,而不保存代码。 Define a function instead. 而是定义一个函数。

cmd () {
  mysqldump --verbose --opt --extended-insert \
            --result-file /home/backups/mysql/test/test_table.20161205.bak test \
            --where="timestamp_c>='2016-12-03 00:00:00'" \
            --tables "test_table"
}

Try executing your command using 'eval'. 尝试使用“ eval”执行命令。 Example: 例:

#!/bin/bash
cmd="mysqldump --verbose --opt --extended-insert --result-file /home/backups/mysql/test/test_table.20161205.bak test --where=\"timestamp_c>='2016-12-03 00:00:00'\" --tables \"test_table\""
eval $cmd

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

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