Shell scripts can be created in any simple text editor. You type the commands in the file, and then save it. Then you need to change the mode of file to executable to indicate that the file is executable so that shell can run it.
Now let us write our first shell script. Let's say you are working on a project and are using the first_project directory, which is a subdirectory of the /projects/shell/ directory. So each time you move to the directory you have to type
cd /projects/shell/first_project
You can create a shell script, say first_proj, to run the cd command so that you don't have to retype it each time you need to use the directory, and also say you also want to list the contents of this directory.
To create the file using the vi editor, you vi first_proj at the prompt, and press Enter. It's always good to provide a brief description of the functions of -rw-rw-r-- 1 nikkij staff 1060 Sep 24 12:24 Birds_of_Passageu003cbr />-rw-rw-r-- 1 adamf staff 1116 Oct 1 15:49 Changeu003cbr />-rw-rw-r-- 1 nikkij staff 11 Sep 30 14:51 Changedu003cbr />-rw-rw-r-- 1 nikkij staff 5 Sep 15 16:05 Childrenu003cbr />-rw-rw-r-- 1 nikkij staff 709 Sep 30 11:22 Fata_Morganau003cbr />-rw-rw-r-- 1 nikkij staff 8 Sep 15 16:06 Loss_and_Gainu003cbr />-rw-rw-r-- 1 nikkij staff 12 Sep 30 15:17 Snow_Flakesu003cbr />-rw-rw-r-- 1 nikkij staff 8 Sep 1 16:06 The_Sound_of_the_Seau003cbr />$u003cbr />u003cbr />You can use an editor to create and edit script files by first openingu003cbr />the file in the usual way:u003cbr />u003cbr />vi longfu003cbr />u003cbr />You can then type the commands that you want to execute in the script.u003cbr />",1] ); //--> the shell script as the first comment line in the file.
To enter a comment in the file, you need to start the comment line with a # character, and then enter your comment.
Next, you type the command cd /projects/shell/first_project in the file.
$ vi first_proj
Then type following in the editor window (You need to press ‘i’ before anything is actually typed in the vi editor. For more details check vi editor tutorials from any site or I will put mine in this site shortly)
# This file lists the directory contents of the first_project directory.
cd /projects/shell/first_project
And you type ls -l on the next line in the file to list the contents of the directory in long format.
To close and save the file, you press :wq.
You can view the contents of your first shell script as
$ cat > first_proj
# This file lists the directory contents of the first_project directory.
cd /projects/shell/first_project
ls -l
$
When your shell script is complete, you need to give the file execute permissions, by typing chmod u+x first_proj (chmod 777 first_proj to give all permissions) at the prompt.
Now to run your first shell script, you simply type first_proj at the prompt.
The output shows the results of the ls -l command on the first_project directory. If your out put is different open the file first_proj and it should look as below:
# This file lists the directory contents of the first_project directory.
cd /projects/shell/first_project
ls -l
~
~
~
~
~
~
~
~
"first_proj" 3 lines, xx characters
Using variables in shell scripts
You can use variables in shell scripts simply be preceding the variable with a dollar sign ($).
And to set a variable value, you use
=.
Naming conventions of variables in shell scripts:
A variable name begins with an alphabetic character, or an underscore.
A name cannot begin with a digit as the first character.
To assign a variable, you use the following syntax:
=
Please note:
There are no spaces on either side of the equal sign in variable assignments.
You cannot assign variable names that contain any punctuation characters.
Shell variable names are case-sensitive.
Now let us have a look at an example:
directory=first_project
The easiest way to access a variable is $, where is the variable name. The $ is replaced with the value of the variable when the shell comes across it.
$ directory=first_project
$ echo This is the $directory directory
This is the first_project directory
$
Let's say you have named your variable testvar, and assigned it a value of script.
By using ${testvar}, you can combine it with script to make the string ing, without any spaces between the words as follows:
$ testvar=script
$ echo This shell ${testvar}ing
This is shell scripting
$
To list an individual element from an array list, you use $. An array is a one dimensional data structure which may contain strings or integers and it can contain up to 512 or 1024 elements depending on your system
For example:
If you have set an array called programmers, with five elements.
set -A programmers Michael John Charlie Tom Smith
To select an element from the array, you type:
echo${programmers [0]}
This provides the first element in the array, which is Michael.
$ set -A programmers Michael John Charlie Tom Smith
$ echo ${programmers [0]}
Michael
$
Now if you want to list all the elements of the array, you use:
echo ${programmers[*]}
This provides a list of all the array elements set.
$ echo ${programmers[*]}
Michael John Charlie Tom Smith
$
There are number of other ways to reference variables.
Using $ passes any elements that are enclosed in double quotes, without splitting any spaces between the words.
$ is used if you need to generate something on expansion, even when the variable is not set.
For example, if you have a variable set as $programmer=Smith, and you
type echo the programmer is ${firstname:-unknown} $programmer.
The output is the programmer is unknown Smith.
${-} - by leaving out the colon, this tests for the variable's existence only.
Using the same example used for $, you will receive output showing The programmer is Smith.
${=} - if you do not enter a name, it is given the value provided. Then the value of name is substituted.
If there is a colon before the equal sign, name is reset if it is of zero length.
$ - stops execution if the variable name is not provided, and then it presents value as an error message.
If it's in a shell program, execution stops immediately.
${+} - this operates in the opposite way to
${-}. If ${} is defined, value is substituted. If it is not defined, a NULL string is provided.
$ - provides an integer count of the total words that result from $@.
$ - provides an integer count of the total words that result from $*.
${#} - provides the integer value of the length of the string in
the variable name.
$ and $ - provide the number of elements in the array variable name.
Automatically set Variables
There are some shell variables that are automatically set by the shell. Typically, these are used inside shell scripts.
For example
$0 provides the name of the command as in the following example:
$ cat some_file_name
echo "The name of this file is $0."
$ some_file_name
The name of this file is some_file_name.
$
$$ is the process number of the current shell instance as in the following example:
$ cat process_program
print "This shell's process number is $$"
$ process_program
This shell's process number is 876
$
Subshells
A subshell is a new shell program that is executed to run a command. For example, when you ask your login shell to run a command, it starts up a new shell to execute the program. And when a new shell runs, it has its own environment, with its own set of environment variables. However, the subshell has no knowledge of local variables of the parent shell and it cannot change any variable values of the parent shell.
For Example:
Suppose you have created a script file called select_programmer that sets the variable programmer as Smith within the script. But before it does so, it echoes any existing value of programmer to the screen using the following code:
$ cat select_programmer
echo first programmer is $programmer
programmer=Smith
echo second programmer is $programmer
$
When you execute the script, the variable programmer has no value in the parent shell.
The variable programmer is assigned a value of Smith within the subshell of select_programmer – this is echoed on the "second programmer" line.
$ select_programmer
first programmer is
second programmer is Smith
$
If you set a programmer variable as Michael in the parent shell and run the script again you can see that the local variable is not carried to the subshell.
The variable set as programmer=Michael is local to the parent shell and not to the subshell invoked by the select_programmer script.
$ select_programmer
first programmer is
second programmer is Smith
$ programmer=Michael
$ echo programmer is $programmer
programmer is Michael
$ select_programmer
first programmer is
second programmer is Smith
$
If you set the programmer variable to Tom and this time export the variable, it is passed to the subshell.
When you execute the select_programmerf script, you can see that the exported
variable programmer is passed from the parent to the subshell.
$ programmer=Tom
$ export programmer
$ echo programmer is $programmer
programmer is Tom
$ select_programmer
first programmer is Tom
second programmer is Smith
$
On returning to the parent shell you can see that the original value of the variable programmer is unchanged by the subshell, and is still Tom.
The following code is used:
$ echo $programmer
Tom
$