Scripting: An Overview
58Scripting Languages: An Overview
This article is in no way intended to be an in depth analysis of scripting languages. It should serve as an introduction to what sort of scripting is available. I will be adding articles to this series which will deal with these languages in more detail.
Writing a good script is very much like writing a good application. If you know exactly what you want the script to do and exactly how you want it to do it, you are bound to be successful. Having a logical structure and process flow will not only result in better coding, but also ensures that your program will do what it needs to in the most efficient way possible.
Code should not only be as logical and efficient as possible, it also needs to be as re-usable as possible. Try to think about other ways that you could use your script.
Lets take a look at some of the more common scripting languages, each of which will have a simple 'Hello World' type example showing a basic subroutine, how to get information and how to give information. These should serve as a starting point for those wanting to learn more about the language.
Bash
The Bourne Again SHell is probably the most used shell. Others include csh (C Shell), ksh (Korn Shell) and tcsh (TENEX C Shell).
Bash is developed by the GNU project and is an enhanced version of sh. While it is enhanced it will run most sh scripts unmodified.
Our enhanced 'Hello World' script might look like this in Bash:
#!/bin/bash
sayit() {
echo "Hello $name"
}
echo 'Please type your name: '
read name
sayit $nameLine 1 tells us what ti use to interpret our script. In this case it is a program called bash which lives in the /bin directory.
Line 3 is the declaration of our subroutine sayit.
Line 4 gives information back to the user of our script.
Line 5 simply ends our subroutine.
Line 7 gives information in the form of a request.
Line 8 reads the information given by the user and puts it into a variable.
Line 9 tells our script to run our subroutine and passes it the information given by the user.
PHP
PHP is more commonly used for developing web applications, but it can also be used to script programs. This brings all the power of PHP to the command line.
Here is our simple example adapted to use php instead.
#!/usr/bin/php
<?php
function sayit($name) {
fwrite (STDOUT, "Hello $name");
}
fwrite(STDOUT, 'Please type your name: ');
$name = fgets(STDIN);
sayit($name);
?>Line 1 tells us what program to use to interpret our script. We will be using php which is in the /usr/bin directory.
Line 2 tells the php interpreter that we are starting a block of code
Line 3 is the declaration of our subroutine sayit.
Line 4 gives information back to the user of our script.
Line 5 simply ends our subroutine.
Line 7 gives information in the form of a request.
Line 8 reads the information given by the user and puts it into a variable.
Line 9 tells our script to run our subroutine and passes it the information given by the user.
Line 10 tells the interpreter that we are done with this block of code.
Perl
Perl is an incredible language for working with text files and information. Perl stands for Practicle Extraction and Report Language which gives us a very good hint as to Perl's strengths. With modules, which are mostly available at CPAN, almost anything can be done with Perl.
#!/usr/bin/perl
sub sayit {
my $name=shift;
print "Hello $name";
}
print 'Please type your name: ';
$name = <STDIN>;
&sayit($name);Line 1 tells us what program to use to interpret our script. This time we will be using perl which is installed in the /usr/bin directory.
Line 3 is the declaration of our subroutine sayit.
Line 4 simply gets the variable sent to the routine.
Line 5 gives information back to the user of our script.
Line 6 simply ends our subroutine.
Line 8 gives information in the form of a request.
Line 9 reads the information given by the user and puts it into a variable.
Line 10 tells our script to run our subroutine and passes it the information given by the user.
Conclusion
While these three scripting languages can easily do this simple task, they do each have unique strengths. These need to be taken into account when choosing the scripting language for a particular task. If one uses re-usable code well you could even write a program which uses a combination of these languages. Each one performing a part in the overall task, something which suits the languages strengths.
PrintShare it! — Rate it: up down flag this hub








