CL205 - Lab2
CL205 - Lab2
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
LINES=25
LOGNAME=students
OSTYPE=Linux
PATH=/usr/bin:/sbin:/bin:/usr/sbin
PS1=[\u@\h \W]\$
PWD=/home/students/Common
SHELL=/bin/bash
Operating System
Author: Nausheen Shoaib
Page 1
USERNAME=vivek
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.
Operating System
Author: Nausheen Shoaib
Page 2
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
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