LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-06-2012, 11:54 AM   #1
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Rep: Reputation: Disabled
Linux and perl


I have a questions that I cant find the answer to when I search for them. I typically write shell script and save out as .sh and run them in ubuntu's terminal.
If I add perl code to the bash do I need to define the header as #!/bin/bash or #!/usr/bin/perl??
What is the correct file type be .sh or .pl??
When executing the .sh in the terminal I do bash name.sh to run it. What is the procedure for .pl?
 
Old 12-06-2012, 12:07 PM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 981
Blog Entries: 2

Rep: Reputation: 235Reputation: 235Reputation: 235
The filename extension (such as .sh) is not important.

Get the shebang line (first line) to say "#!" followed by the full pathname of the interpreter you want. Give the script execute permission with chmod. Just call it.
 
Old 12-06-2012, 12:15 PM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
As linosaurusroot mentioned, the extension isn't important when it comes down to it (Linux doesn't look at it, unlike windows).

If you do want to see by looking at the file name, being just a mere mortal, how it is interpreted/executed the following extensions are widely used:

.sh -> shell script (bash/sh/ksh/..)
.pl -> perl
.py -> python

Last edited by druuna; 12-06-2012 at 12:25 PM. Reason: does -> doesn't!!
 
Old 12-06-2012, 01:40 PM   #4
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,367
Blog Entries: 10

Rep: Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038Reputation: 2038
Quote:
...If I add perl code to the bash do I need to define the header as #!/bin/bash or #!/usr/bin/perl??...
Code:
#!/bin/bash
echo $(perl --version | head -2 | tail -1)
 
Old 12-06-2012, 02:09 PM   #5
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
Contrary to awk and sed it is not common to include perlcode in shellscripts. Most often it will be much easier to write a perlscript "on it's own". Note that Perl is much more powerful than the shell, sed and awk together.

Markus
 
Old 12-06-2012, 06:47 PM   #6
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 981
Blog Entries: 2

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by markush View Post
Contrary to awk and sed it is not common to include perlcode in shellscripts. Most often it will be much easier to write a perlscript "on it's own". Note that Perl is much more powerful than the shell, sed and awk together.
Also in the 1990s when I mixed a lot of shell/sed/awk it took mental effort to switch between the different syntax involved. For anything non-trivial I suggest going straight to Perl. This also gets you fewer calls to programs outside your script (which brings more portability between different unix versions).
 
Old 12-06-2012, 11:29 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,245

Rep: Reputation: 2326Reputation: 2326Reputation: 2326Reputation: 2326Reputation: 2326Reputation: 2326Reputation: 2326Reputation: 2326Reputation: 2326Reputation: 2326Reputation: 2326
I concur with linosaurusroot.
If you do start using Perl, here's some good links
http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials
http://www.tizag.com/perlT/index.php
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] PERL, How to pipe out put of a perl script for processing with linux command. Soji Antony Programming 3 11-16-2012 10:04 AM
LXer: Installing Eclipse, the Epic Perl plugin and my first Perl GUI program LXer Syndicated Linux News 0 05-08-2009 07:41 PM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 11:22 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 09:09 PM


All times are GMT -5. The time now is 10:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration