Operating Systems
Lab 02
Shell Scripts
Lab Instructor: Zainab Iftikhar
Shell Scripting
Shell Scripting
Shell Script is series of command written in plain
text file. Shell script is just like batch file is MS-DOS but
have more power than the MS-DOS batch file."
Why to write shell scripts?
o Shell script can take input from user, file and output them
on screen.
o Useful to create our own commands.
o Save lots of time.
o To automate some task of day today life.
o System Administration part can be also automated.
How to write shell scripts
Use any editor to write shell script
Set the execute permission for your script through
o Graphical interface
o chmod +x script-name
o chmod 755 script-name
My First Shell Script
Code:
Save the file as s1.sh in Home
Output:
My Second Shell Script
Code:
Save the file as s1.sh in Home
Commands to run on terminal
Output:
Variables in Shell
In Linux shell there are three types of variables
System variables
User defined variables
Parametric variables
User Defined Variables
Rules for defining variables
o Name must start with an alphanumeric character
o Can use underscore in between the characters
o Don't put spaces on either side of the equal sign
when assigning value to variable
var=10
o NULLvariable declaration
var=
var=“”
Do not use ?,* to name your variable names
User Defined Variables
Defining user variables
o no=10
o Variable „no‟ is assigned value 10
Accessing/printing variables
o Toaccess variables use „$‟
o echo $no
User Defined Variables Example
Code:
Output: