perl example not runiing

hi,
i wrote simple perl script and trying to run geting below error from


https://www.tutorialspoint.com/unix_terminal_online.php

sh-4.4$ vi hi.pl                                                                                                                                                                            
sh-4.4$ pwd                                                                                                                                                                                  
/home/cg/root                                                                                                                                                                                
sh-4.4$ /home/cg/root/hi.pl                                                                                                                                                                  
sh: /home/cg/root/hi.pl: Permission denied                                                                                                                                                  

i just wrote in hi.pl as
print "hii";


in command prompt of windows it ran fine once i install activevperl
https://www.activestate.com/activeperl/downloads

C:\Users\ss\perl\code>perl hello.pl
hii

please advise
LVL 7
gudii9Asked:
Who is Participating?
 
arnoldCommented:
Fr hi.pl to run, the permissions on the file must be 700 or -rwx------
The important parts are the r for read and the x for execute making the file an executable.
The last item, the first line in the script must be #!/usr/bin/perl
Without the above, even if you run chmod 700 hi.pl or chmod o+x hi.pl
Your error is because the file hi.pl is not executable. Once the above is done, the next attempt to run hi.pl
Will might error out because it would not know how to handle your input, or alternatively, sone she'll have a print function, and the script will run under a bash, zsh, ksh shell versus under perl as you intend.

Note the distinction of how you are trying to run hi.pl in the UNIX environment versus in the Windows. If you do the same in UNIX as you did on Windows you will get the same result, output.
Point being perl is the executeable and the file is an input argument.
0
 
gudii9Author Commented:
now is ee below error with print


sh-4.4$ vi hi.pl                                                                                                                                                                            
sh-4.4$ pwd                                                                                                                                                                                  
/home/cg/root                                                                                                                                                                                
sh-4.4$ /home/cg/root/hi.pl                                                                                                                                                                  
sh: /home/cg/root/hi.pl: Permission denied                                                                                                                                                  
sh-4.4$ chmod 700 hi.pl                                                                                                                                                                      
sh-4.4$ /home/cg/root/hi.pl                                                                                                                                                                  
/home/cg/root/hi.pl: line 1: print: command not found
0
 
gudii9Author Commented:
i have below content in file just one line

print "hii";
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
arnoldCommented:
Add the following as the first line in hi,pl
#!/usr/bin/perl

to make hi.pl look like (vi hi.pl
#!/usr/bin/perl
print "hi\n";

Open in new window


now run it, ./hi.pl
and you should see
hi
as the output.
0
 
gudii9Author Commented:
now i see output

sh-4.4$ vi hi.pl                                                                                                                                                                            
sh-4.4$ ./hi.pl                                                                                                                                                                              
hish-4.4$  

what is meaning of below line
#!/usr/bin/perl

my perl script not at above location but at brlow location

sh-4.4$ pwd                                                                                                                                                                                  
/home/cg/root

please advise
0
 
arnoldCommented:
Refereñce your Windows example
Your script, hi.pl is in the location reflected by pwd, the interpreter, perl is located in /usr/bin/perl
The first line tells the shell (environment in your case it is bash) where the interpreter that will process the script is.
Run the command,
which perl
This will tell your the command for perl is.
0
 
gudii9Author Commented:
sh-4.4$ which perl                                                                                                                                                                          
/usr/bin/perl  
is above is path where perl library is inatalled?
do we need to refer that in everu perl script i write at top?

who installed there tutoralpoint guys?
https://www.tutorialspoint.com/unix_terminal_online.php

i just instaled on windows laptop only
0
 
arnoldCommented:
Yes, unix, Linux scripts have to include the interpreter to be used to process the script as the first line
Note # means it is a comment
!/usr/bin/perl means use this executable.
At times shell script using sh, bash, ksh, zsh tcsh might be processed by the shell in which the person is
I.e. While first line says /bin/sh bash as the environment might process the script.
There are times where a script should be processed in a specific way, and to make sure one is directed to explicitly run
<shell> shell_script
Similar to the perl hi.pl you ran under Windows and can run under UNIX/Linux.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.