简体   繁体   English

从命令行构造Perl命令参数

[英]Construct perl command params from the command line

I would like to add multiple -e command dynamically to perl but it throws multiple errors like this: 我想向perl动态添加多个-e command ,但它会引发如下多个错误:

String found where operator expected at -e line 2, near "'s/\\$testName/\\$test-name/g;'" (Missing semicolon on previous line?) 在-e行2的“'s / \\ $ testName / \\ $ test-name / g;'”附近找到了运算符所期望的字符串(上一行缺少分号?)

Heres what I'm doing: 这是我在做什么:

find css -name "*.scss" -print0 | \
xargs -0 -t perl -pi \
$(perl -ne '/^(\$(?=[a-z0-9]*[A-Z])[^:\s]+)\s*:/ && print "-e \047s/\\$1/\\" . lc(join("-", split(/(?=[A-Z])/, $1))) . "/g;\047\n"' _variables.scss | sort -ur | tr "\n" " ")

The command $(perl -ne ...) extracts and transforms some content and outputs something like: 命令$(perl -ne ...)提取并转换一些内容,并输出如下内容:

-e 's/\$testName/\$test-name/g;' -e 's/\$coolName/\$cool-name/g;' -e 's/\$camelCased/\$camel-cased/g;'

As far as I can tell the problem resides in the way bash/perl evaluates the last command, the manual execution of the command output by "xargs -t" works fine. 据我所知问题出在bash / perl评估最后一条命令的方式上,“ xargs -t”手动执行命令输出可以正常工作。

The following simple case fails too: 以下简单情况也失败了:

target="-e 's/\$testName/\$test-name/g;' -e 's/\$coolName/\$cool-name/g;' 's/\$camelCased/\$camel-cased/g;'"
perl -pi $target css/core.scss

Try this: 尝试这个:

ARGS=$(perl -ne '/^(\$(?=[a-z0-9]*[A-Z])[^:\s]+)\s*:/ && print "-e \047s/\\$1/\\" . lc(join("-", split(/(?=[A-Z])/, $1))) . "/g;\047\n"' _variables.scss | sort -ur | tr "\n" " ")
eval "find css -name '*.scss' -print0 | xargs -0 -t perl -pi $ARGS"

Or 要么

eval "find css -name '*.scss' -print0 | xargs -0 -t perl -pi $(perl -ne '/^(\$(?=[a-z0-9]*[A-Z])[^:\s]+)\s*:/ && print "-e \047s/\\$1/\\" . lc(join("-", split(/(?=[A-Z])/, $1))) . "/g;\047\n"' _variables.scss | sort -ur | tr "\n" " ")"

You'll need to include an eval with the xargs to get the variable to be parsed as parameters. 您需要在xargs包含一个eval ,以将变量解析为参数。 Here's a quick demonstration: 这是一个快速演示:

# Variable containing parameters
> target="-e 's/a/b/g;' -e 's/x/y/g;'"

# Variable substitution - Doesn't work
> echo "aaaabbbbccccddddxxxx" | perl -pi ${target}
String found where operator expected at -e line 2, near "'s/x/y/g;'"
        (Missing semicolon on previous line?)
syntax error at -e line 2, near "'s/x/y/g;'"
Execution of -e aborted due to compilation errors.

# Manual substitution - Works
> echo "aaaabbbbccccddddxxxx" | perl -pi -e 's/a/b/g;' -e 's/x/y/g;'
bbbbbbbbccccddddyyyy

# Eval substitution - Works
> echo "aaaabbbbccccddddxxxx" | eval perl -pi ${target}
bbbbbbbbccccddddyyyy

In your situation, the following should work: 在您的情况下,以下方法应该起作用:

find css -name "*.scss" -print0 | \
    xargs -0 -t eval perl -pi $(perl -ne .....)

You don't get quote removal on the results of variable expansion and command substituion. 您不会在变量扩展和命令替换的结果上删除引号。

See http://mywiki.wooledge.org/BashFAQ/050 for more details. 有关更多详细信息,请参见http://mywiki.wooledge.org/BashFAQ/050

You can't get what you want here the way you are trying to do it. 您无法以尝试的方式在这里得到想要的东西。 Store the results of the inner perl in an array and expand that on the outer perl command. 将内部perl的结果存储在数组中,并在外部perl命令上对其进行扩展。

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

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