An Introduction to Linux Shell Scripting

How to Use Bash, Perl, PHP or Python in a Linux Script

© Mark Alexander Bain

Sep 9, 2008
Linux Shell Scripting, Mark Alexander Bain
A Linux shell script can be a powerful tool - this articles looks at examples of a few of the scripting languages that are available to the programmer.

The number one rule with Linux shell scripting is that there is no number one rule; and that's because there's no set language for programming a Linux script - it's just a matter of a programmer choosing a scripting language - one that will do the particular job, or one that the programmer is comfortable with - and then telling the system which script language interpreter is to be used.

Scripting Languages for Linux

There are numerous scripting languages for Linux - too many to cover here - and so this article will just give examples of four of them:

  • Bash
  • Perl
  • PHP
  • Python

Having chosen which language to use it's then essential that the script user should need to know nothing further about it - this means that they should be able to call it from the command line without knowing which interpreter is needed to run the script - and the key to doing this is the shebang line.

The Shebang Line

The shebang line is critical to any script - it's always the first line of the script and instructs the script which interpreter to use. It always has the same format:

  • the shebang line starts with the symbols #! (hence the name)
  • the shebang line ends with the full path to the interpreter

The location of the interpreter will vary according the Linux distribution being used, and the easiest way to find it is to use the which method:

$ which bash
/bin/bash
$ which perl
/usr/bin/perl
$ which php
/usr/bin/php
$ which python
/usr/bin/python

So, for example, the first line of a Bash script would be:

#!/bin/bash

A Example Bash Script

#!/bin/bash
function show_input () {
echo " Input is $1"
}
while [ ! -z "$1" ]
do
show_input $1
shift
done

The first line of the script is obviously the shebang line - telling the system which interpreter to use; the script then:

  • defines a function - in this case it simply formats the input to the function. One important thing to note about Bash functions is that they do not need input variables defined - they have in-built variables: $1 is the first input variable; $2 is the second; and so on
  • the script then loops through any variables passed to the script - it uses the shift function to move variables to the left - in this way only the first variable needs to be checked.

The script can then be called from the command line:

$ myscript 1 2 3
Input is 1
Input is 2
Input is 3

An Example Perl Script

#!/usr/bin/perl
sub show_details
{
return "Input is $_[0]";
}
while (@ARGV != 0) {
$y = shift;
$x = show_details($y);
print "$x\n";
}

The Perl script is fundamentally the same as the Bash script - it just uses Perl syntax rather than Bash, and all of the input variables are included in an array - ARGV.

An Example PHP Script

#!/usr/bin/php
<?php
function sayHello ($ip) {
return "Input is " . $ip;
}
for ($i = 1; $i < $argc; $i++ ) {
echo sayHello($argv[$i]) . "\n";
}
?>

Having seen the Bash and Perl scripts the PHP script is straightfoward - the only real difference is that as well as the argv array there is an argc variable - this contains a count of the number of input variables.

An Example Python Script

#!/usr/bin/python
import sys
def show_input(ip):
 return "Input is " + ip
for arg in sys.argv:
 op = show_input (arg)
 print op

The Python script is similar to the Perl and PHP scripts - again it's just the format that is different - this time using indentation to create functions and command structures.

Conclusion

The key to the perfect Linux shell script is not necessarily which actual language is used by the script - that just comes down to the programmer's personal choice (or ability); the key to the perfect Linux shell script is the shebang line -by using this the programmer can ensure that the user needs know nothing about the language being used - the user just needs to know that the perfect Linux shell script actually exists.

Related Reading

Any Microsoft Windows programmers interested in scripting may like to read:

And any Linux programmers may be interested in:


The copyright of the article An Introduction to Linux Shell Scripting in Linux Programming is owned by Mark Alexander Bain. Permission to republish An Introduction to Linux Shell Scripting in print or online must be granted by the author in writing.


Linux Shell Scripting, Mark Alexander Bain
A Linux Shell Script in Perl, Mark Alexander Bain
Using Cywin to run a Linux Shell Script, Mark Alexander Bain
   


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo

Comments
Oct 1, 2008 8:35 PM
Guest :
Just a note, the term 'shell script' refers to a script written to be interpreted by a shell, usually making heavy use of Unix/Linux command line utilities. Common shells include bash, as you mentioned, along with the original bourne shell, zsh, etc.

Scripts written in Perl, PHP, Python, or other scripting languages are not shell scripts. They're Perl scripts, PHP scripts, and Python scripts. By definition, a shell script is written in a shell language.
Oct 2, 2008 2:54 AM
Mark Alexander Bain :
You've raised an interesting point there - what is a shell? If we look at Wikipedia's definition (http://en.wikipedia.org/wiki/Es_shell) then we find that they define a shell as "a piece of software that provides an interface for users". If that's the case then Perl, PHP and Python are all shells. Also, if you look at Wikipedia's comparison of shells (http://en.wikipedia.org/wiki/Comparison_of_command_shells) then it tells us that "a command shell is a command line interface computer program to an operating system"; and you'll find that in the comparison Python is listed with more functionality than many of the 'traditional' shells.

So, whilst I agree that these are not shells that you'd choose to use for most of your day to day work (my personal favourite is Korn), they are all shells.
2 Comments