syntax error in perl script for linux - perl

I have written a very simple perl script for Linux to determine the current user logged on.
However I keep getting the following error when trying to run it:
bash: use: command not found
bash: my: command not found
bash: ./test.pl: line 9: syntax error near unexpected token `else'
bash: ./test.pl: line 9: `} else {'
This is my code:
#!/usr/bin/perl
use strict;
my $loginName = '';
if ($^O =~ /MSWin/i)
{
$loginName = getlogin;
} else {
#else it is unix
$loginName = getpwuid($<);
}
print $loginName;
I have tried to google this but I dont see what I am doing wrong with my if statement? It works fine on Windows.
Thank you

You are invoking the script incorrectly: these errors are clearly from bash, while perl should be running the script instead.
I don't know how you're running it now, but (assuming its filename is mywhoami) you can always invoke perl explicitly:
perl mywhoami
It should also work to make it executable
chmod a+x mywhoami
and then execute it:
./mywhoami

I think it is something it the way you run the script.
Please try to run it as follow:
$perl <script.pl>

Related

Perl command executing good when run on command line but not working in the Perl script

Below is the code I'm trying to execute. I have mentioned the line 266 in the code. I have added that code to remove the blank lines in the log file. I'm not sure whether we can run the perl command inside a Perl script. Is there another way that I can run this so that I can remove the blank lines in the log file?
Below is the error I'm getting while running through the Perl script:
syntax error at ./reportJBossErrors.pl line 266, near "n -e "
Execution of ./reportJBossErrors.pl aborted due to compilation errors.
Here is a portion of the code, showing line 266:
sub main {
readConfiguration($config_file);
$short_hostname = `hostname | cut -f 1 -d.`;
chomp $short_hostname;
getFileandInstance($short_hostname);
$yesterday = getYesterday();
validateEnvironment();
$log_file = getLogFile($FMASK,$yesterday);
perl -i -n -e "print if /\S/" $log_file; # 266 line. This is where I'm getting the compilation error
processFile($log_file);
$html_out = writeEmail();
sendEmail($CONFIG{"FROMADDR"},$CONFIG{"TOADDR"},"Normal",
"JBOSS",$short_hostname,$log_file,$CONFIG{ENVTYPE},$html_out);
}
You can not call the perl command inside a Perl program as if it were a Perl builtin function. You can use system to run an external command:
my $cmd = 'perl -i -n -e "print if /\S/"';
system "$cmd $log_file";
You need to be careful of quoting. Since you have a file name/path in the Perl variable $logfile, which you want to interpolate, that can go inside double quotes. Since you do not want to interpolate \S, that should go in single quotes.
You cannot invoke the perl executable inside a Perl program as if it were a Perl builtin function. Instead, use the list form of system to run an external command. Don't forget to check if the command succeeded:
my #cmd = (perl => '-i', '-n', '-e', 'print if /\S/', $log_file);
system(#cmd) == 0
or die "system #cmd failed: $?";
In general, I would recommend using the full path to perl rather than relying on $PATH.
Also, if you need to keep track of status etc, use Capture::Tiny to get both STDOUT and STDERR of the command you are running so that you can log error information.

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");

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&`;

Not able to call a perl script from a shell script

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.

Why can't I run a Perl program from TextMate?

I'm following a bioinformatics text, and this represents one of my first Perl scripts. While in TextMate, this does not produce any result. Is it functioning? I added "hello world" at the bottom and I don't see that when I run the script in TextMate. What have I done wrong?
#!/usr/local/bin/perl -w
use lib "/Users/fogonthedowns/myperllib";
use LWP::Simple;
use strict;
#Set base URL for all eutils
my $utils = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils";
my $db = "Pubmed";
my $query ="Cancer+Prostate";
my $retmax = 10;
my $esearch = "$utils/esearch.fcgi?" .
"db=$db&retmax=$retmax&term=";
my $esearch_result = get($esearch.$query);
print "ESEARCH RESULT: $esearch_result\n";
print "Using Query: \n$esearch$query\n";
print "hello world\n";
Have you run other scripts successfully in TextMate? I suspect that you have an editor configuration issue (or that URL is not hittable and the script is generating an error).
Try running an even simpler script and see what happens, e.g. something like:
#!/usr/local/bin/perl -w
print "hello world\n";
Next, see if you can see error output:
#!/usr/local/bin/perl -w
warn "oh noes";
Most likely, your executing environment is not set up in the right way, i.e., TextMate does not know how to execute a Perl script and display the results to you. Have you tried running it from the shell?
Do you have /usr/local/bin/perl installed? What does which perl return at the terminal?
If your hashbang line is incorrect then when you come to run the script in TextMate you will just get an empty output window. Textmate does not give any error that it couldn't find the executable interpreter you prescribed after #! :(
Replace the first line with #!/usr/bin/env perl and it will run you login's default Perl (found in $PATH environment variable).
/I3az/