Shell Programming - Part 1: I. Schell Script Overview
Shell Programming - Part 1: I. Schell Script Overview
The linux shell (/bin/sh) works in an interactive mode and non-interactive mode.
1. Interactive Mode:
Shell is the interface between the user and the Unix System.
2. Non-Interactive Mode:
In this mode, the user is able to group the instructions in files called Shell scripts. In such way,
the user can define his own instructions
1|Page
2.1.Shell script components
If the file containing the script does not have the “execute” right:
sh <filename> [<argument> …]
The sh instruction will execute the set of instructions included in the file.
<argument>: the list of inputs which are necessary to the execution of the script
./<filename> [<argument> …]
In this case, we don‟t need to call the sh instruction. The file is already executable and it is
considered as a new UNIX instruction.
Example :
2|Page
II. Variables in Shell
A shell variable is identified by its name. Shell manages only alphanumeric variables.
User variables
Predefined Variables
1. User Variables
To call a user variable, we have to preced its name by the „$‟ character.
<variable_name>=<variable_value>
Example:
To concatenate two user variables in UNIX, we have to call them one after the other
Example:
3|Page
1.3.Associate a UNIX instruction to a user variable
<variable_name>=`<instruction>`
According to this syntax, the instruction is first executed and then its result will be stored in the
variable value.
Example:
2. Predefined variables
The predefined variables are the variables defined by the Linux OS, such as:
Try the env instruction in your computer. What is your logname? .................................................
4|Page
3. Variables affected by the shell during the script execution
$0 Name of the running procedure
$1…$9 Value of the 1st argument….Value of the 9th argument
$* List of arguments
$# Number of arguments
Example:
Create a new file called “tryvar” and put the following instructions on it:
5|Page