iDev
iDev

Reputation: 2421

Call a bash script from a perl script

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

Upvotes: 7

Views: 52432

Answers (4)

iDev
iDev

Reputation: 2421

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)

Upvotes: 1

Igor Chubin
Igor Chubin

Reputation: 64563

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: $!";
}

Upvotes: 11

ellipse-of-uncertainty
ellipse-of-uncertainty

Reputation: 1035

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

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

See also: this post.

Upvotes: 1

newfurniturey
newfurniturey

Reputation: 38416

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

Upvotes: 1

Related Questions