对于bash执行的Perl逃逸的说法 [英] Perl escaping argument for bash execution

查看:187
本文介绍了对于bash执行的Perl逃逸的说法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用Perl编写的一些code,其执行其执行中的一些bash命令。我的问题是,当bash命令属性包含其内部没有bash命令执行的空白。不过,我已经成功与那些说法只是周围添加报价参数工作。不幸的是在测试过程中,我发现,当 \\ 字符被放置在变量后面这显然逃脱bash的执行报价和失败bash命令我的脚本失败。例如:

I've written some code in Perl which executes some bash command within its execution. My problem was when bash command attributes contained white space inside which failed bash command execution. But I've managed to work with those argument simply adding quotes around argument. Unfortunately during tests I've found that my script fails when \ character is placed at the end of variable which obviously escapes bash execution quotes and fails bash command. For example:

my $arg1 = 'This is sample text which can be put into variable. And a random sign\\';
`echo "$arg1"`

是否有任何机会,以变量自动转义特殊字符?

Is there any chance to automatically escape special characters in variable?

推荐答案

这是更容易使用在bash单引号;那么你需要担心的唯一字符是一个单引号本身。

It's much easier to use single quotes in bash; then the only character you need to worry about is a single quote itself.

($arbitrary_string) =~ s/'/'"'"'/g;

`echo '$arbitrary_string'`

这篇关于对于bash执行的Perl逃逸的说法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆