Scripting

In this section you will learn a little about three important scripting languages.

Bash Scripting

chmod and bash file for changing directories

Often you want to make changes to the current environment. You can use export to do this, but often it is simpler to use the source or . command. Suppose you have a bash script called gosource that looks like this:

#!/bin/bash 

pushd /home/ccalvert/kylix/source 

This simple file changes you current directory to the directory home/ccalvert/kylix/source. Instead of using cd to make the change, you use pushd. If you use pushd, then you will be able to get back to the directory from which you started by entering the command popd. Here is an example:

/home/ccalvert/$ pushd /usr/local 
/usr/local/$
/usr/local/$ popd 
/home/ccalvert/$ 

The user started out in /home/ccalvert, used pushd to change to the /usr/local directory, then used popd to move back to the /home/ccalvert directory. Given this knowledge, our bash script would appear to move us into the /home/ccalvert/kylix/source directory. But if you simply enter the gosource command at the bash prompt, it will appear that you script does not work.In fact, it does work, but the problem is that it starts a new bash shell, changes you to the directory you requested, then ends the shell, thereby switching back to where you started. For all intents and purposes, the script does not work, even though it does do the job it asks you to do. Consider the following command:

 . gosource 

Notice that there is a period and a space before the command. If you press enter after issuing this command, it will have the effect you wanted. You can, of course, embed the ., or source, command, inside a bash script file.

Perl

Overview

Quality

A Simple First Program

Here is a very simple Perl script.

#!/usr/bin/perl

print("The Blue Begonia Express!\n");

To run it, first make sure it has the proper access permissions.

You can start it one of two ways:

perl simple.pl
./simple.pl

File IO

ReadEmAndWeep.pl
#!/usr/bin/perl

#Sam();

print("Summer time blues!");
Sam();

sub Sam
{
    print("\n");
    open(WORDS, "stuff");
    while ($name = )
    {
      print("$name");
    }
    print("\n");
}
SimpParse
#!/usr/bin/perl

DoIt();

sub DoIt()
{
    $i = 0;
    open (MESS, "messages");
    while ($name = )
    {
    	$name =~ s/\W.*//;
      print ("$name", $i);
      $i++;
    }
}

Parsing HTML Example

Here is a simple Perl script for finding all the NAME tags in a file and creating a unordered list from them.

.
#!/usr/bin/perl -w
#
# $Revision: 1.4 $
#
# $Date: 2002/09/02 15:36:45 $

my $file = shift;

die "Can't find file \"$file\""
  unless -f $file;
    
my $count = 0;
my $isAName = 0;
my $jumpName = "";
my $fileName = "Jumps.html";

open(OUT, ">$fileName") or die "can't open $fileName: $!";
print("$file\n");
open(IN, "$file") or die "can't open $file: $!";
print(OUT "");
close(OUT);

################
## End of main
################

sub getJumpName
{
  my ($position, $myStr, $value);

  $value = shift;

  $position = index($value, "a name=");
  $myStr = substr($_, $position + 8);
  $myStr = reverse($myStr);
  $position = index($myStr, ">\"");
  $myStr = substr($myStr, $position + 1);
  $myStr = reverse($myStr);

  return $myStr;
}


sub getLabel
{
  my ($pos, $myStr, $value);
  $value = shift;

  $position = index($value, "\">");
  $myStr = substr($value, $position + 1);
  $myStr = reverse($myStr);
  $position = index($myStr, ">a/<");
  $myStr = substr($myStr, $position);
  $myStr = reverse($myStr);

  return $myStr;
}

Python