6

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

iDev
  • 1,717
  • 5
  • 31
  • 52
  • 1
    yes, you can using `system("bash -c script.sh")`, for example – 2r2w Jul 24 '12 at 18:07
  • @IgorChubin you're right , but if script has execution than `bash -c` is ok too – 2r2w Jul 24 '12 at 19:25
  • I solved my first problem according to http://stackoverflow.com/questions/255414/why-doesnt-cd-work-in-a-bash-shell-script : alias proj="cd /home/tree/projects/java" (Thanks to @Greg Hewgill) – iDev Jul 24 '12 at 21:48
  • Possible duplicate of [How can I call a shell command in my Perl script?](https://stackoverflow.com/q/3200801/608639) – jww Jul 25 '19 at 10:54

4 Answers4

10

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: $!";
}
Igor Chubin
  • 51,940
  • 8
  • 108
  • 128
  • Good answer, but /bin/sh may not be bash, and if it is bash will run in --posix mode. – jordanm Jul 24 '12 at 18:33
  • 1
    No reason to run `sh` just to launch `bash`: `system("/bin/bash", "/path/to/script");` and `open(my $PIPE, '-|', "/bin/bash", "/path/to/script")` would avoid this. – ikegami Jul 24 '12 at 19:20
  • also just `\`bash script.sh\`` could be used – 2r2w Jul 24 '12 at 19:28
  • @IgorChubin What does the `$!` do at the end of your code? I'm unfamiliar with Perl. – MrPickles Jun 29 '15 at 12:23
  • @IgorChubin Is the `|` necessary at the end of `"/bin/bash /path/to/script|")? What does that do? – MrPickles Jun 29 '15 at 12:28
  • @MrPickles: Yes, it's necessary. that is actually the main point. Without the pipe symbol you will just open a file with the name in the quotes (it does not exist, obviously, because it'a a command name). And now what is `$!`: that is a special variable, containing error message in it – Igor Chubin Jun 30 '15 at 16:19
1

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

newfurniturey
  • 34,078
  • 9
  • 85
  • 99
1

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

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

See also: this post.

Community
  • 1
  • 1
functionvoid
  • 1,015
  • 11
  • 20
1

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)

Community
  • 1
  • 1
iDev
  • 1,717
  • 5
  • 31
  • 52