我创建了一个调用两个 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/