[an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] Skip to the content of the web site.

Executable Scripts

Contents Previous Topic No Next Topic

This topic is only for experienced Unix users. It is provided for information purposes.

Scripts

A collection of shell commands can be collected into a text file which can be executed. There are two methods of doing this: explicit execution of the scripts in the current shell and the implicit running of a script as an atomic unit.

If you store a text file myscript which contains a sequence of Unix commands, then you execute them in the current shell using the command source myscript. This is executed as if you had cut-and-paste the commands into the current shell.

Alternatively, you can make the text file executable by first changing the permission modifiers to 700 or 755 and then indicating the shell which is to execute the commands in the first line following a !#, for example, the following script removes all executables.

#!/usr/bin/tcsh

rm `find ~ -name a.out`

While this command may run in both tcsh and bash shells, it is very easy to create a script which is shell dependent, and therefore, anyone can execute this script, whether they are running tcsh or bash, as in either case, the script will be run using tcsh.

Storing Executable Scripts

It is common practice to put scripts into a directory ~/bin and to include this directory in your path in your .tcshrc or .bashrc file.

Philosophy of Scripts

The basic variable for shell scripts is a file, and very often, the goal of one command in a shell script is how to create a file which can be used by the next command. Shell variables are used, however, they do not support such features as arrays or other higher-level data structures.

If you are interested in programming scripts as if you were programming, for example, C++, consider looking into perl.

Execution of Scripts

You may wonder how Unix treats, for example, a perl script or a shell script. Such a script is simply a sequence of commands which are interpreted by the appropriate program. If you look at the top of any perl script, you will see something like:

#!/usr/local/bin/perl

Following the #! is the path of an executable file, in this case, perl. When you execute a file, the first line of which starts with #!, the shell will execute the following executable file and pass the name of the file as an argument.

To see how the Unix operating system interprets #!, save Program 1 to interpreter.c file and compile it to mine (gcc interpreter.c -o mine).


Program 1. A simple program which prints its arguments.

#include <stdio.h>

int main( int argc, char * argv[] )
{
	printf( "Hello!\n" );

	int i;

	for ( i = 0; i < argc; ++i ) {
		printf( "%d:  %s\n", i, argv[i] );
	}

	return 0;
}

Next, store Program 2 in a file y.my.


Program 2. A simple program which prints its arguments.

#!./mine
Bye

Make this file executable and then execute it, possibly giving it additional options:

{ecelinux:4} chmod 700 y.my
{ecelinux:5} ./y.my
Hello!
0:  ./mine
1:  ./y.my
{ecelinux:6} ./y.my -opt1 -opt2 arg1
Hello!
0:  ./mine
1:  ./y.my
2:  -opt1
3:  -opt2
4:  arg1
{ecelinux:7}

The only difference with a Perl script is that the first line of a perl script has the path to the perl executable following the #!:

{ecelinux:7} which perl
/usr/local/bin/perl
{ecelinux:8}

Perl would then access the file corresponding to name provided by the second argument and interpret its contents as a perl script. This is why, if you are copying a perl file from one system onto another, you may have to edit the first line.

Contents Previous Topic No Next Topic

Copyright ©2005-2012 by Douglas Wilhelm Harder. All rights reserved.

[an error occurred while processing this directive]