2. Contents:
What is Shell?
Brief introduction to Shell Programming
What are Shell Variables?
Some Simple Shell Programs
Wildcards
3. What is Shell?
A shell is a computer program which works as the interface to
access the services provided by an OS.
A shell is a command-line interpreter.
It performs operations such as file manipulation, program
execution and printing text.
4. Introduction to Shell Programming:
Shell programming is a computer program designed to be run by UNIX/ LINUX shell.
We write shell scripts to avoid repetitive work and automation.
A shell script comprises of Shell Keywords, Shell Variables, Shell Commands, functions and
Control Flow Statements.
Shell script is saved with ’.sh’ file extension for example ’myscript.sh’.
5. Shell Variables:
A variable is a character string to which we assign a value.
The value assigned could be a number, text, filename, device, or any other type of data.
The shell enables you to create, assign, and delete variables.
Variables have naming convention such as the name of a variable can contain only letters (a to z or A
to Z), numbers ( 0 to 9) or the underscore character ( _).
‘_VAR’ ‘TOKEN_A’ ‘VAR_1’ ‘VAR_2’ These are valid variable names.
‘2_VAR’ ‘-VARIABLE’ ‘VAR1-VAR2’ ‘VAR_A!’ These are invalid variable names.
6. Shell Variables (Cont.):
Defining Variables:
SYNTAX – For example –
variable_name = variable_value; NAME = “Gaurav Bisht”
ROLLNO = 7
Accessing Variables:
INPUT – OUTPUT –
NAME = “Gaurav Bisht” Gaurav Bisht
echo $NAME
7. Shell Variables (Cont.):
Variables in shell do not use special characters like ‘!’, ‘#’, ‘?’ or ‘$’ since there exists predefined
special variables in shell scripting.
S. No. Special
Variable
Description Input Output
1. $0 Display file name of current script echo “File Name: $0” File Name: ./test.sh
2. $# Display number of arguments
supported to a script
echo “Total no. of
parameters: $#”
Total no. of parameters: 2
3. $? The exit status of the last command
executed
echo $? 0
4. $$ The process number or the Process ID
(PID) of the current shell
echo $$ 29949
5. $! The process number of the last
background command
echo $! 1342
8. Some Simple Shell Programs:
Program 1 – Adding 2 numbers.
echo "Enter first number:"
read x
echo "Enter second number:"
read y
sum=x+y
echo "The result of addition =$sum"
9. Some Simple Shell Programs (Cont.):
Program 2 – Concatenation of 2 strings.
str1=“Hello”
str2=“World”
str3=“$str1 $str2”
echo $str3
10. Some Simple Shell Programs (Cont.):
Program 3 – Comparison of 2 strings.
read -p "Enter first string: " VAR1
read -p "Enter second string: " VAR2
if [[ "$VAR1" == "$VAR2" ]]
then
echo "Strings are equal."
else
echo "Strings are not equal."
fi
11. Some Simple Shell Programs (Cont.):
Program 4 – Maximum of 3 numbers.
echo " Enter A value: "
read a
echo " Enter B value: "
read b
echo " Enter C value: "
read c
if [ $a -gt $b ] && [ $a -gt $c ]
then
echo "A value is greater"
elif [ $b -gt $a ] && [ $b -gt $c ]
then
echo "B value is greatest "
else
echo "C value is greatest "
fi
12. Wildcards:
A wildcard is a character that can be used as a substitute for any of a class of characters in a
search, thereby greatly increasing the flexibility and efficiency of searches.
There are 3 types of wildcards:
1. Star Wildcard (*)
2. Question mark Wildcard (?)
3. Square Bracket Wildcard ([])
13. Wildcards (Cont.):
Star Wildcard –
- It is most frequently used and most useful wildcard in shell programming.
- The star wildcard has the broadest meaning of any of the wildcards, as it can represent zero
characters, all single characters or any string.
For example –
- file * => It will display all the files present in the directory along with their type.
- ls *.txt => It will list all the files with extension ‘.txt’.
14. Wildcards (Cont.):
Question mark Wildcard –
- The question mark wildcard is used to represent exactly one character, which can be any
single character.
- Two question marks in succession would represent any two characters .
- Three question marks in succession would represent any string consisting of three
characters.
For example –
- file ??? => It will display data in the present directory whose name is exactly 3 characters in length
irrespective if the extension.
- file ? ?? ??? => It will display data on all objects whose names are one, two or three characters in
length
15. Wildcards (Cont.):
Square Bracket Wildcard –
- The square bracket wildcard is used to represent any of the characters enclosed in the
square brackets.
For example –
- file *[xyz]* => It will display information about all objects in the current directory that have an x, y or z
in them.
- ls *.[xyz]* => It will list all files that had an extension that begins with x, y or z.