Monday 8 August 2011

How to create a simple shell script

Shell scripts are short programs that are written in a shell programming language and interpreted by a shell process. A shell is a program that provides the traditional, text-only user interface for Unix-like operating systems. Its primary function is to read commands that are typed into a console or terminal window and then execute (i.e., run) them. The default shell on Linux is the very commonly used and highly versatile bash. 
A feature of bash and other shells used on Unix-like operating systems is that each contains a built-in programming language, referred to as a shell programming language or shell scripting language, which is used to create shell scripts. 
A First Script
The following example provides a useful introduction to creating and using shell scripts. The script clears the monitor screen of all previous lines and then writes the text Good morning, world. on it.
For that open a text editor such as gedit or vi, and type the following three lines exactly as shown on a new, blank page:
#!/bin/bash
clear
echo "Good morning, world."
 
After saving this plain text file, with a file name such as morning or anything else desired, the script is complete and almost ready to run. Scripts are typically run by typing a dot, a forward slash and the file name (with no spaces in between) and then pressing the ENTER key. Thus, for example, if the above script were saved with the name morning, an attempt could be made to execute it by issuing the following command:
./morning
However, the script probably will not run, in which case an error message will appear on the screen such as bash: ./morning: Permission denied. This is because the permissions for the file first have to be set to executable. The problem can easily be solved by using the chmod command with its 755 option (which will allow the file creator to read, write and execute the file) while in the same directory as that in which the file is located as follows:
chmod 755 morning
Now the script is ready to run by typing the following, again while in the same directory, and then pressing the ENTER key:
./morning
Now it display a message on the screen
Good morning, world.
At the same time the script clears all previous lines from the screen.
How it works?
The first of the three lines tells the operating system what shell to use to interpret the script and the location of the shell. The shell is bash, which is located in the /bin directory. Thus the line contains /bin/bash. This instruction is always preceded by a pound sign and an exclamation mark in order to inform the operating system that it is providing the name and location of the shell.
The second line tells the shell to issue the clear command. This is a very simple command that removes all previous commands and output from the console or terminal window in which the command was issued. 
The third line tells the shell to write the phrase Good morning, world. on the screen. It uses the echo command, which instructs the shell to repeat whatever follows it. The quotation marks are not necessary in this case.

No comments:

Post a Comment