linux - Perl 在不同的 bash 脚本中设置和获取环境

标签 linux bash perl shell unix

我创建了一个调用两个 bash 脚本的 perl 脚本。第一个脚本将设置一个环境变量,第二个将回显环境变量。我已经在下面给出了文件的内容

# perlscript.pl

print `. setnameenv.sh`;
print `. getnameenv.sh`;

# setnameenv.sh

export my_msg='hello world!'

# getnameenv.sh

echo $my_msg

现在,当我运行 perl 脚本 perl perlscript.pl 时,我希望在屏幕上打印“hello world”,但实际上我没有看到任何输出。我有什么办法可以在不修改 bash 脚本的情况下做到这一点吗?

最佳答案

您可以将 perl 嵌入到 bash 脚本中,

#!/bin/bash
. setnameenv.sh
exec perl -x "$0" "$@"

#!perl
# your script below

print `. getnameenv.sh`;

来自 perldoc

-x -xdirectory tells Perl that the program is embedded in a larger chunk of unrelated text, such as in a mail message. Leading garbage will be discarded until the first line that starts with #! and contains the string "perl". Any meaningful switches on that line will be applied.

关于linux - Perl 在不同的 bash 脚本中设置和获取环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31934392/

相关文章:

c - 将变量从 linux 内核传递给进程

linux - sched_class 结构中的 .next 字段有什么用?

linux - shell中两个日期之间的天数差异

bash - :- (colon dash) in bash 的用法

bash - shell脚本变量值

Perl 服务器端跨域检测

linux - bash --redirect 输出到日志文件(名称中包含日期)

linux - 如何在同一个文件夹中解压一堆相同的包?

perl - Perl用子守护程序守护

regex - 如何跳过类似 perl sed 的查找/替换命令中的不匹配项?