简体   繁体   English

perl 正则表达式搜索问题并用环境变量替换

[英]Issue with perl regex search and replace with environment variable

I'm running a GCE VM with Ubuntu 18, and having an issue with a perl command.我正在运行带有 Ubuntu 18 的 GCE VM,并且遇到了 perl 命令的问题。

export ip_addr=`hostname -i`
echo "set \$ip_addr_priv \"{my_ip_address}\"" | sudo perl -n -e 's/(\$ip_addr_priv) +"\{([a-zA-Z0-9_]+)\}"/\1 "$ENV{ip_addr}"/g; print;'

When I run this in the command line, I get the following output:当我在命令行中运行它时,我得到以下 output:

set $ip_addr_priv ""

Instead of something like this:而不是这样的:

set $ip_addr_priv "127.0.0.1"

What am I doing wrong?我究竟做错了什么?

By default (and by design), sudo doesn't pass the current user's environment on to the new process.默认情况下(和设计), sudo不会将当前用户的环境传递给新进程。

You can override this behaviour with the -E command line flag.您可以使用-E命令行标志覆盖此行为。

echo "set \$ip_addr_priv \"{my_ip_address}\"" | sudo perl -n -e 's/(\$ip_addr_priv) +"\{([a-zA-Z0-9_]+)\}"/\1 "$ENV{ip_addr}"/g; print;'
set $ip_addr_priv ""

Vs:对比:

echo "set \$ip_addr_priv \"{my_ip_address}\"" | sudo -E perl -n -e 's/(\$ip_addr_priv) +"\{([a-zA-Z0-9_]+)\}"/\1 "$ENV{ip_addr}"/g; print;'
set $ip_addr_priv "127.0.1.1"

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

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