【问题标题】:Passing space contained values as option to perl将空间包含的值作为选项传递给 perl
【发布时间】:2017-03-24 16:22:44
【问题描述】:

在传递带有选项(包含空格的值)作为参数的 perl 脚本时,空格忽略问题。

test.pl

use strict;
use Getopt::Long;

my $option;
GetOptions(
 "myreg=s"=>\$option,
);

print "Option: $option\n";

我有一个 shell 脚本,当我传递带有选项(包含空格的值)的 perl 脚本时,脚本只打印空格之前的选项值。

/my/test/script.sh /my/testing/test.pl --myreg='Reg 1'

上面的执行打印,

Option: Reg

什么问题?请帮忙。

【问题讨论】:

  • Shell 插值和引用。在交给 perl 之前,你的引号会被 shell “解析”。

标签: perl


【解决方案1】:

这是因为/my/test/script.sh 执行了不正确的转义,类似于使用$@exec $@ 而不是"$@"exec "$@"

$ cat ./bad
#!/bin/sh
exec $@

$ ./bad ./test --myreg='Reg 1'
Option: Reg

$ cat ./good
#!/bin/sh
exec "$@"

$ ./good ./test --myreg='Reg 1'
Option: Reg 1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-04
    • 2015-06-15
    • 1970-01-01
    • 2021-07-21
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多