Call bash script from perl script

I am trying to execute code in a perl script and you need to call another file in bash. Not sure if this is the best way to do this? can i directly call it with system ()? Please guide / show me an example.

from what I've 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 get an error while executing a perl script: There is no such file or directory.

syntax error near unexpected do token

+5
source share
4 answers

If you just want to run you script, you can use return outputs or a system:

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

or

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

If your script generates a number of errors, 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: $!";
}
+9
source

You can use backticks to execute commands:

$command = `command arg1 arg2`;

, system("command arg1 arg2"), .

-: http://www.perlhowto.com/executing_external_commands

+1

You can use backticks, system () or exec.

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

See also: this post.

+1
source

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 @Greg Hewgill)

+1
source

All Articles