从 perl 脚本调用 bash 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11636721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call a bash script from a perl script
提问by iDev
I am trying a code in perl script and need to call another file in bash. Not sure, which is the best way to do that? can I directly call it using system() ? Please guide/ show me a sample way.
我正在尝试用 perl 脚本编写代码,需要在 bash 中调用另一个文件。不确定,这是最好的方法吗?我可以使用 system() 直接调用它吗?请指导/向我展示一个示例方法。
from what I have tried so far :
从我到目前为止所尝试的:
#!/usr/bin/perl
system("bash bashscript.sh");
Bash :
重击:
#!/bin/bash
echo "cdto codespace ..."
cd codetest
rm -rf cts
for sufix in a o exe ; do
echo ${sufix}
find . -depth -type f -name "*.${sufix}" -exec rm -f {} \;
done
I am getting an error when I execute the perl script : No such file or directory codetest
执行 perl 脚本时出现错误:没有这样的文件或目录 codetest
syntax error near unexpected token `do
意外标记附近的语法错误`do
回答by Igor Chubin
If you just want run you script you can use backticks or system:
如果你只想运行你的脚本,你可以使用反引号或系统:
$result = `/bin/bash /path/to/script`;
or
或者
system("/bin/bash /path/to/script");
If your script produces bug amount of data, the best way to run it is to use open + pipe:
如果您的脚本产生大量数据,运行它的最佳方法是使用 open + pipe:
if open(PIPE, "/bin/bash /path/to/script|") {
while(<PIPE>){
}
}
else {
# can't run the script
die "Can't run the script: $!";
}
回答by newfurniturey
You can use backticks to execute commands:
您可以使用反引号来执行命令:
$command = `command arg1 arg2`;
There are several other additional methods, including system("command arg1 arg2")
to execute them as well.
还有其他几种附加方法,包括system("command arg1 arg2")
执行它们。
Here's a good online reference: http://www.perlhowto.com/executing_external_commands
这是一个很好的在线参考:http: //www.perlhowto.com/executing_external_commands
回答by functionvoid
回答by iDev
I solved my first problem according to Why doesn't "cd" work in a bash shell script?:
我根据 为什么“cd”在 bash shell 脚本中不起作用?:
alias proj="cd /home/tree/projects/java"
(Thanks to @Greg Hewgill)
(感谢@Greg Hewgill)