我们可以在 perl 脚本中获取 shell 脚本吗 [英] can we source a shell script in perl script
本文介绍了我们可以在 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 脚本
这篇关于我们可以在 perl 脚本中获取 shell 脚本吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文