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
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: $!";
}
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
You can use backticks, system(), or exec.
system("myscript.sh") == 0
or die "Bash Script failed";
See also: this post.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With