Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a bash script from a perl script

Tags:

bash

perl

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.

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

syntax error near unexpected token `do

like image 869
iDev Avatar asked Jul 24 '12 18:07

iDev


4 Answers

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:

if open(PIPE, "/bin/bash /path/to/script|") {
  while(<PIPE>){
  }
}
else {
  # can't run the script
  die "Can't run the script: $!";
}
like image 111
Igor Chubin Avatar answered Oct 22 '22 21:10

Igor Chubin


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.

Here's a good online reference: http://www.perlhowto.com/executing_external_commands

like image 1
newfurniturey Avatar answered Oct 22 '22 20:10

newfurniturey


You can use backticks, system(), or exec.

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

See also: this post.

like image 1
functionvoid Avatar answered Oct 22 '22 21:10

functionvoid


I solved my first problem according to Why doesn't "cd" work in a bash shell script? :

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

(Thanks to @Greg Hewgill)

like image 1
iDev Avatar answered Oct 22 '22 20:10

iDev