我们可以在 perl 脚本中获取 shell 脚本吗
问题描述:
我们可以在 perl 脚本中获取 shell 脚本吗??
can we source a shell script in the perl script??
示例:程序 1:
cat test1.sh
#!/bin/ksh
DATE=/bin/date
程序 2:
cat test2.sh
#!/bin/ksh
. ./test1.sh
echo `$DATE`
计划 3:
cat test3.pl
#!/usr/bin/perl
### here I need to source the test1.sh script
print `$DATE`'
如何在 perl 中获取 shell 以获取执行 test3.pl 时打印的日期
How to source the shell in perl to get the date printed when I execute test3.pl
谢谢抹布
答
你不能做一个
system("source src.sh");
system()
启动一个新的子 shell,您的环境变量不会传递给运行 Perl 脚本的 shell.即使您的 shell 脚本导出变量,它也会将它们导出到子外壳,而不是您的实际外壳.
system()
starts a new sub-shell, your environment variables do not get passed to the shell your Perl script is running in. Even though your shell script exports variables, it will export them to the sub-shell, not to your actual shell.
一种解决方案是编写一个包装脚本
One solution would be to write a wrapper script which
- 首先获取 shell 脚本,然后
- 运行 Perl 脚本