7

我正在尝试 perl 脚本中的代码,并且需要在 bash 中调用另一个文件。不确定,这是最好的方法吗?我可以使用 system() 直接调用它吗?请指导/告诉我一个示例方式。

从我到目前为止所尝试的:

     #!/usr/bin/perl
     system("bash bashscript.sh");

重击:

#!/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

执行 perl 脚本时出现错误:没有这样的文件或目录 codetest

意外标记`do附近的语法错误

4

4 回答 4

11

如果你只想运行你的脚本,你可以使用反引号或系统:

$result = `/bin/bash /path/to/script`;

或者

system("/bin/bash /path/to/script");

如果您的脚本产生大量数据错误,则运行它的最佳方法是使用 open + pipe:

if open(PIPE, "/bin/bash /path/to/script|") {
  while(<PIPE>){
  }
}
else {
  # can't run the script
  die "Can't run the script: $!";
}
于 2012-07-24T18:12:50.223 回答
1

您可以使用反引号来执行命令:

$command = `command arg1 arg2`;

还有其他几种附加方法,包括system("command arg1 arg2")执行它们。

这是一个很好的在线参考:http ://www.perlhowto.com/executing_external_commands

于 2012-07-24T18:08:29.113 回答
1

我根据 Why does not "cd" work in a bash shell script? 解决了我的第一个问题?

alias proj="cd /home/tree/projects/java"

(感谢@Greg Hewgill)

于 2012-07-24T21:51:51.867 回答
1

您可以使用反引号、system() 或 exec。

  system("myscript.sh") == 0
    or die "Bash Script failed";

另见:这篇文章。

于 2012-07-24T18:09:59.677 回答