Reputation: 321
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
Upvotes: 0
Views: 1141
Reputation: 8591
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
Upvotes: 2
Reputation: 128
I think it is something it the way you run the script. Please try to run it as follow:
$perl <script.pl>
Upvotes: 0