从一个shell脚本变量导出到一个Perl脚本 [英] Export variable from a shell script into a perl script

查看:360
本文介绍了从一个shell脚本变量导出到一个Perl脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Perl的code

`. /home/chronicles/logon.sh `; 

print "DATA  : $ENV{ID}\n";

在logon.sh,我们出口变量ID(shell脚本的采购)。

In logon.sh , we are exporting the variable "ID" (sourcing of shell script).

手动运行

$> . /home/chronicles/logon.sh
$> echo $LOG

虽然我在终端手动运行(而不是从脚本)。我得到的输出。 (但不能从脚本工作)

While I am running in terminal manually (not from script). I am getting the output. (But not working from the script)

我跟着这个帖子:

<一个href=\"http://stackoverflow.com/questions/2967762/how-to-export-shell-variable-within-perl-script\">How到Perl脚本中导出shell变量

但没有解决的问题。

注意

我不能更改logon.sh脚本。

I am not allowed to change "logon.sh" script.

推荐答案

反引号内的脚本是在一个子进程执行。而环境变量从继承的的方法中,亲本不能访问的子进程的环境

The script inside the backticks is executed in a child process. While environment variables are inherited from parent processes, the parent can't access the environment of child processes.

不过,你可以返回儿童环境变量的内容并把它变成一个Perl变量像

However, you could return the contents of the child environment variable and put it into a Perl variable like

use strict; use warnings; use feature 'say';
my $var = `ID=42; echo \$ID`;
chomp $var;
say "DATA: $var";

输出:

DATA: 42

下面的例子shell会话:

Here an example shell session:

$ cat test_script
echo foo
export test_var=42
$ perl -E'my $cmd = q(test_var=0; . test_script >/dev/null; echo $test_var); my $var = qx($cmd); chomp $var; say "DATA: $var"'
DATA: 42

正常输出重定向到的/ dev / null的,所以只有回声$ test_var 节目。

这篇关于从一个shell脚本变量导出到一个Perl脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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