LAB 2
Shell Scripting
Shell Programming
A shell program is a series of Linux commands and utilities that have been put into a file
by using a text editor.
Example:
Write a script to print message
clear
echo "Hello World!"
save
chmod 755 program
./program
Steps to run shell scripts
1. Create a new file name program1 through vi editor
2. Write a script to clear screen and display your text Hello World
3. Save your script program1
4. Check the permissions of script such that owner (read, write, execute), group
and other users (read and execute) permission.
5. Execute the script name program1
1) System variables - Created and maintained by Linux itself. This type of variable
defined in CAPITAL LETTERS.
2) User defined variables (UDV) - Created and maintained by user. This type of
variable defined in lower letters.
System Variable
Meaning
BASH=/bin/bash
Our shell name
BASH_VERSION=1.14.7(1)
COLUMNS=80
HOME=/home/vivek
Our shell version name
No. of columns for our screen
Our home directory
LINES=25
No. of columns for our screen
LOGNAME=students
OSTYPE=Linux
Our logging name
Our Os type
PATH=/usr/bin:/sbin:/bin:/usr/sbin
PS1=[\u@\h \W]\$
PWD=/home/students/Common
SHELL=/bin/bash
Our path settings
Our prompt settings
Our current working directory
Our shell name
Operating System
Author: Nausheen Shoaib
Page 1
USERNAME=vivek
User name who is currently login to this PC
How to define User defined variables (UDV)
variable name=value
Don't put spaces on either side of the equal sign when assigning value to variable.
Variables are case-sensitive.
script to test MY knowledge about variables
myname=Vivek
myos = TroubleOS
myno=5
echo "My name is $myname"
echo "My os is $myos"
echo "My number is myno, can you see this number"
Quotes
Name
"
Double Quotes
'
Single quotes
Back quote
Meaning
"Double Quotes" - Anything enclose in
double quotes removed meaning of that
characters (except \ and $).
'Single quotes' - Enclosed in single quotes
remains unchanged.
`Back quote` - To execute command.
The read Statement
Use to get input (data from user) from keyboard and store (data) to variable.
Syntax: read variable1, variable2,...variableN
#Script to read your name from keyboard
echo your first name; read fname
echo Your name is $fname
More command on one command line
Syntax: command1;command2
To run two command with one command line.
$ date;who
test command
test command or [ expr ] is used to see if an expression is true, and if it is true it return
zero(0), otherwise returns nonzero(>0) for false.
Syntax: test expression OR [ expression ]
Operating System
Author: Nausheen Shoaib
Page 2
# Script to see whether argument is positive
if test $1 -gt 0
then
echo "$1 number is positive"
fi
Mathematical Operators
Mathematical
Operator in Shell
Script
Meaning
Normal
Arithmetical/
Mathematical
Statements
But in Shell
-eq
is equal to
-ne
is not equal
5 != 6
to
For test
For [ expr ]
statement with statement with
if command
if command
if expr [ 5 -eq 6
if test 5 -eq 6
]
if expr [ 5 -ne 6
if test 5 -ne 6
]
-lt
is less than
5<6
if test 5 -lt 6
if expr [ 5 -lt 6 ]
-le
is less than
or equal to
5 <= 6
if test 5 -le 6
if expr [ 5 -le 6 ]
-gt
is greater
than
5>6
if test 5 -gt 6
if expr [ 5 -gt 6 ]
-ge
is greater
than or
equal to
5 >= 6
if test 5 -ge 6
if expr [ 5 -ge 6
]
5 == 6
NOTE: == is equal to , != is not equal.
Conditional Statement if...else...fi
# Script to see whether argument is positive or negative
if [ $# -eq 0 ]
then
echo "$0 : You must give/supply one integers"
exit 1
fi
Operating System
Author: Nausheen Shoaib
Page 3
Iterative Statement
for loop Syntax:
for { variable name } in { list }
do
execute one for each item in the list until the list is
not finished (And repeat all statement between do and done)
done
Lab Task { Use vi editor}
1. Write scripts /commands / syntax to print a message Welcome to the World of
Shell Scripting
2. Write scripts /commands / syntax to print user information who currently login,
current date and time
3. Write scripts /commands / syntax to take input and print your name, degree
program information, batch No and course title
4. Write scripts /commands / syntax which take backup of all programs.
5. Write scripts /commands / syntax that take input and check the number is
positive or negative.
6. Write scripts /commands / syntax that print a message 10 times.
7. Write scripts /commands / syntax that lists number of users created in system.
8. Write scripts /commands / syntax that print uptime of system, users logged in
system and list running processes by particular users.
Operating System
Author: Nausheen Shoaib
Page 4