我们可以在 perl 脚本中获取 shell 脚本吗

rag*_*ghu 5 perl

我们可以在 perl 脚本中获取 shell 脚本吗?

示例: 方案 1:

cat test1.sh
#!/bin/ksh
DATE=/bin/date
Run Code Online (Sandbox Code Playgroud)

方案2:

cat test2.sh
#!/bin/ksh
. ./test1.sh  
echo `$DATE`
Run Code Online (Sandbox Code Playgroud)

方案3:

cat test3.pl
#!/usr/bin/perl
### here I need to source the test1.sh script
print `$DATE`'
Run Code Online (Sandbox Code Playgroud)

如何在 perl 中获取 shell 以便在执行 test3.pl 时打印日期

谢谢拉古

cod*_*ict 4

你不能做一个

system("source src.sh");
Run Code Online (Sandbox Code Playgroud)

system()启动一个新的子 shell 时,您的环境变量不会传递到运行 Perl 脚本的 shell。即使您的 shell 脚本导出变量,它也会将它们导出到子 shell,而不是实际的 shell。

一种解决方案是编写一个包装脚本

  1. 首先获取 shell 脚本,然后
  2. 运行 Perl 脚本