Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我怎么能逃过一劫!(砰/感叹)在调用bash命令的perl脚本中?_Bash_Perl_Passwords_Escaping_Quotes - Fatal编程技术网

我怎么能逃过一劫!(砰/感叹)在调用bash命令的perl脚本中?

我怎么能逃过一劫!(砰/感叹)在调用bash命令的perl脚本中?,bash,perl,passwords,escaping,quotes,Bash,Perl,Passwords,Escaping,Quotes,我正在更新一个遗留的Perl脚本。这非常简单,因为它设置了一些变量,然后调用bash命令。问题是我传递的密码中有两个感叹号。这些似乎被翻译错了。假设我有这个脚本: $source_db_ip = "1.2.3.4"; # carl dev/qa $source_user = 'user1'; $source_password = "password!!"; $destination_db_ip = "5.6.7.8"; $destination_user = "user2"; $des

我正在更新一个遗留的Perl脚本。这非常简单,因为它设置了一些变量,然后调用bash命令。问题是我传递的密码中有两个感叹号。这些似乎被翻译错了。假设我有这个脚本:

$source_db_ip = "1.2.3.4"; # carl dev/qa
$source_user = 'user1';
$source_password = "password!!";

$destination_db_ip = "5.6.7.8";
$destination_user     = "user2";
$destination_password = "password2!!";

my $status = `pt-table-sync h=$source_db_ip,p='${source_password}',u=$source_user,D=db_name,t=table_name   h=$destination_db_ip,p='$destination_password',u='$destination_user',D=db_name,t=table_name`;

这一直在失败。我知道凭据是正确的,因为我已经手动检查了它们。那么,我如何才能正确地转义密码以便正确地翻译它呢?

正如您可以通过使用
`perl-E'say for\@ARGV'-…`
知道的那样,以下两个字符串作为参数传递:

h=1.2.3.4,p=password!!,u=user1,D=db_name,t=table_name
h=5.6.7.8,p=password2!!,u=user2,D=db_name,t=table_name
您没有指定所需的格式,但看起来不错。请注意,我会使用
shell
而不是
“$var”

use String::ShellQuote qw( shell_quote );

my $cmd = shell_quote(
   'pt-table-sync',
   join(',',
      "h=$source_db_ip",
      "p=$source_password",
      "u=$source_user",
      "D=db_name",
      "t=table_name",
   ),
   join(',',
      "h=$destination_db_ip",
      "p=$destination_password",
      "u=$destination_user",
      "D=db_name",
      "t=table_name",
   ),
);

`$cmd`

顺便说一句,backticks需要一个
sh
命令,而不是
bash
命令。顺便说一句,机器上的任何人都可以看到命令行,因此在命令行中包含密码是个坏主意。谢谢你的建议。我试过了,在执行shell_quote时收到了一个错误。但事实证明,我的一个主机名有一个错误,所以我没有进一步追查这个错误。