Not able to call a perl script from a shell script - perl

I am trying to call a perl script from a shell script and code looks like shown below...
shell script test_sh
#Call the script
test2.pl ${PARTITION_ID} ${VNG_USER} ${VNG_PASSWORD} ${VNG_INSTANCE}
if [ $? -ne 0 ]
then
OP1ExitStatus -6
fi
while execution getting below error message:
./test_sh[142]: test2.pl: not found
Failed in test_sh
permission given to both files are 755.
But when i and calling test2.pl directly from the command line by passing all arguments it runs successfully.
I tried with below command as well :
#Call the script
perl test2.pl ${PARTITION_ID} ${VNG_USER} ${VNG_PASSWORD} ${VNG_INSTANCE}
if [ $? -ne 0 ]
then
OP1ExitStatus -6
fi
but it is also not working.
please let me know how to proceed in order to run it successfully.

From the command line you're invoking perl test2.pl directly. From the script you're assuming that (1) test2.pl is executable and (2) . is in $PATH somewhere. I would use the direct perl invocation in the script, at least for now.

check your shebang, eg #!/bin/bash. you may also want to try using the full path of your Perl executable.

That usually means that the path to perl in the shebang line at the top of the Perl script is wrong (assuming that the file has execute permission for you).
Try using:
#!/usr/bin/env perl

Your shell script is unable to find your test2.pl. You need to give the full path of test2.pl in your shell script or ensure it is in your $PATH.

Related

How to call shell script from perl script

I'm trying to call already saved shell script from perl script, but it's not working .
1.pl:
#!/usr/bin/perl
#!/bin/csh -f
use warnings;
use Shell;
system ("/bin/sh commands.sh");
commands.sh:
#!/bin/csh -f
echo "calling shell script from perl script";
If commands.sh is executable, then all you should need is:
#!/usr/bin/perl
system("/path/to/commands.sh")
If commands.sh does not have the executable flag set, then
#!/usr/bin/perl
system("/bin/csh /path/to/commands.sh");
All of the other code appears to be superfluous.
The path to the shell script changes now and then for me, so I keep it in a variable at the top of the script where it is easy to update:
our $pathToShellScript = '/path/to/script.sh';
system("/bin/sh $pathToShellScript");
Not sure why this errors:
sh: -c: line 0: unexpected EOF while looking for matching ''`
While the following works:
system ("/bin/sh", "$renamingScript");

How to make Perl script executable?

I have given a perl script file executable permission on a Unix based system, but when I try to execute the file I get a "command not found" error. I have #!usr/local/bin/perl at the start of the script file.
At the top of your script, replace the #!usr/local/bin/perl with #!/usr/bin/perl notice the root '/' prior to the usr/
And you can try perl <filename> keyword to run the file instead of ./<filename>, using perl keyword should run the script regardless of the shebang line at the top of your script.
replace your first line #!usr/local/bin/perl with #!usr/bin/perl(which is called Shebang). Then you can run the file with perl <Path To File>.

Executing Perl script from Tcl

I'm trying to execute a Perl script from Tcl.
I use eval in Tcl, and it executes the Perl script but doesn't execute the other command in my Tcl file.
Tcl:
eval perl perl_script
puts "Command executed"
Result: the Perl script is executed, but not the puts command. Why is this?
In tcl, eval executes its arguments as tcl code, similar to its perl equivalent. But, you do not want this, instead you need to launch the perl interpreter as an external process and ask this interpreter to execute the script passed in its commandline. This is the job of exec in tcl.
exec perl perl_script
Read more about exec # https://www.tcl.tk/man/tcl/TclCmd/exec.htm
UPDATE
exec will throw an error when the command returns a non-zero exit status. You need wrap exec with a catch to continue with non zero exit status. See WORKING WITH NON-ZERO RESULTS
UPDATE 2
The reason why eval perl script_path manages to successfully launch the perl interpreter is tcl's unknown magic. It is likely that you do not have tcl proc named perl. So, tcl calls the "unknown" proc, which tries to handle this exception intelligently by calling exec on its arguments since it finds an executable in $env(PATH). You can try info body unknown to see how this magic actually works.
Try this:
exec sh -c { perl -ape 's/this/that/' tmp.1 > tmp.11 }
It works for me.

Execute a command using a perl script

I have simple command in unix like
cat myfile.txt >&mytemp.txt&
The above command will simply create a copy of the file myfile.txt.
when i execute the command on the command line it returns me the process id like below:
> cat myfile.txt > & mytemp.txt &
[1] 769
>
I am forming the same command inside a perl script and calling it with system as below:
my $cmd="cat myfile.txt>&mytemp.txt&";
my $info = system("$cmd");
but the sytem command fails with the below error message:
sh: mytemp.txt: bad number
I even tried with escaping the > and &.But there is no change in the error message.
May i know the reason for this?where am i wrong here?
I'm pretty sure that you can't use the trailing & on this. If you want your program to continue while the command runs, then fork and have the child process run the call, then exit. Possibly exec can do this, though I haven't tried doing that with output redirection before...
Like the message says, that's not a valid sh command. Is it perhaps a csh command?
system('csh', '-c', $cmd);
Try this:
perl -e "`cat myfile.txt>&mytemp.txt&`;"
It's executing the command and returning the command output.
So it's possible to do:
#!/usr/bin/perl
my $content = `cat /etc/passwd`;
print $content;
If you put the code into a perl script:
cat-test.pl
#!/usr/bin/perl
use strict;
use warnings;
my $res = `cat myfile.txt>&mytemp.txt&`;

How can I run a Perl script on Mac OS X?

How do I run a Perl script on OS X?
You can run your Perl script by invoking the Perl interpreter and giving your file as input:
perl myprogram.pl
The easiest way to run a perl script is with the option:
perl myprogram.pl
However, you may find it more useful to add a shebang line at the top of the perl file.
#!/usr/bin/perl
print "Hello World!\n";
In order to execute this script, you need to add execute permissions to your program. Run:
chmod +x myprogram.pl
Now, in order to run your script, you can simply type:
./myprogram.pl
A good tutorial on Perl in OSX can be found here:
http://www.mactech.com/articles/mactech/Vol.18/18.09/PerlforMacOSX/index.html
A generic documentation on executing Perl code is of course perldoc perlrun.
To answer your question directly:
You can run a perl script on any Unix system by either having the code evaluated and executed from command line:
perl -e 'print "Hello World\n"';
Or you can save your Perl script to a file (customarily having .pl extension, say script1.pl and with the first line being #!/usr/bin/perl) and then you can execute it as any Unix program (after setting proper execute permissions)
/path/to/script/script1.pl
You can also execute a script from a file by running perl interpreter as the command and giving the script as a parameter (in this case execute permissions to the script are not needed):
perl /path/to/script/script1.pl
For some reason the whole directory didn't work for me but I just did
perl ~/Desktop/file.pl
(you could also use any folder that is in your user-folder after the ~/)