|
||||||
An Introduction to Linux Shell ScriptingHow to Use Bash, Perl, PHP or Python in a Linux Script
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 LinuxThere are numerous scripting languages for Linux - too many to cover here - and so this article will just give examples of four of them:
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 LineThe 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 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:
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. ConclusionThe 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 ReadingAny 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.
Comments
Oct 1, 2008 8:35 PM
Guest :
Oct 2, 2008 2:54 AM
Mark Alexander Bain :
2 Comments
|
||||||
|
|
||||||
|
|
||||||