Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Shell Scripting OS LAB

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 41

What is shell?

A shell is a program that takes commands typed by


the user and calls the operating system to run those
commands.
Shell accepts your instruction or commands in English and translate it into computers
native binary language.
Kind of Shells
• Bourne Shell
• C Shell
• Korn Shell
• Bash Shell
• Tcsh Shell
This is what Shell Does for
US
Changing Your Default Shell
 Tip: To find all available shells in your
system type following command:
$ cat /etc/shells
 The basic Syntax :
chsh username new_default_shell
 The administrator can change your
default shell.
Shell Scripting
Shell script is a series of command(s) stored
in a plain text file.

A shell script is similar to a batch file in MSDOS,


but is much more powerful.
Practical examples where shell
scripting actively used:
1. Monitoring your Linux system.
2. Data backup and creating snapshots.
3. Find out what processes are eating up your system resources.
4. Find out available and free memory.
5. Find out all logged in users and what they are doing.
6. Find out if all necessary network services are running or not.
Create a script
As discussed earlier shell scripts stored in plain text file, generally one
command per line.

◦ vi myscript.sh

Make sure you use .bash or .sh file extension for each script. This
ensures easy identification of shell script.
Setup executable permission
• Once script is created, you need to setup executable permission on a
script. Why?
• Without executable permission, running a script is almost impossible.
• Besides executable permission, script must have a read permission.
• Syntax to setup executable permission:

◦ $ chmod +x your-script-name.
◦ $ chmod 755 your-script-name.
Run a script (execute a
script)
Now your script is ready with proper executable permission on it. Next,
test script by running it.
◦ bash your-script-name
◦ sh your-script-name
◦ ./your-script-name
 Examples
◦ $ bash bar
◦ $ sh bar
◦ $ ./bar
Using Exit code($?)
Every Linux or Unix command executed by the shell script or
user has an exit status.
Exit status is an integer number. 0 exit status means the
command was successful without any errors.
A non-zero (1-255 values) exit status means command was a
failure.
Example

date test 1 –gt 0 ;


echo $? echo $?
Variables in Shell
In Linux (Shell), there are two types of
variable:
◦ System variables - Created and
maintained by Linux itself. This type of
variable defined in CAPITAL
LETTERS.
◦ User defined variables (UDV) –
Created and maintained by user. This
type of variable defined in lower letters.
System Variables
Variable Description

PATH This variable contains a colon (:)-


separated list of directories in
which your system looks for
executable files. The search path.

USER The username


HOME Default path to the user's home
directory
EDITOR Path to the program which edits
the content of files

UID User's unique ID


TERM Default terminal emulator
SHELL Shell being used by the user
User defined variables
(UDV)
To define UDV use following syntax:
◦ variable name=value
◦ $ no=10
Rules for Naming variable name
• Variables must begin with a letter.
• Spaces are not allowed.
• Underscore can be allowed.
• No special character in variable name.
• Variables are case-sensitive.
Print or access value of
UDV
To print or access UDV use following
syntax :
◦ $variablename.
 Examples:
◦ $vech=Bus
◦ $ n=10

◦ $ echo $vech
◦ $ echo $n
Don’t try
◦ $ echo vech
◦ it will print vech instead its value 'Bus‘.
◦ $ echo n
◦ it will print n instead its value '10‘.
“You must use $ followed by variable name.”
Class work
1. Define variable x with value 10 and print it on
screen.

2. Define variable xn with value SUST and print it on


screen.

3. print sum of two numbers, let's say 6 and 3 .


Example of UDV
A=10
Ba=20
BA=30
HOSTNAME=$ (hostname)
DATE=`date`
123var=333
wrong@var=False
Hyphen-var=Falsehyphen value
Shell Arithmetic
Syntax:
◦ expr op1 math-operator op2
 Examples:
◦ $ expr 1 + 3
◦ $ expr 2 – 1
◦ $ expr 10 / 2
◦ $ expr 20 % 3
◦ $ expr 10 \* 3
◦ $ echo `expr 6 + 3`
Shell Relational
Test command or [expr]
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 ]
Example: Using the ! Operator
#!/bin/bash

read -p "Enter years of work: "Years

if [ ! "$Years" -lt 20 ]; then

echo "You can retire now.“

else

echo "You need 20+ years to


retire“

fi
if condition
Syntax:
if condition
then
command1 if condition is true or if
exit status
of condition is 0 (zero)
fi
Example
$ vim myscript.sh

read choice
if [ $choice -gt 0 ]; then
echo "$choice number is positive"
else
echo "$ choice number is negative"
fi
Nested if-else-fi
$ vi nestedif.sh

echo "1. Unix (Sun Os)"


echo "2. Linux (Red Hat)"
echo -n "Select your os choice [1 or 2]? "
read osch
if [ $osch -eq 1 ] ; then
echo "You Pick up Unix (Sun Os)"
else
if [ $osch -eq 2 ] ; then
echo "You Pick up Linux (Red Hat)"
else
echo "What you don't like Unix/Linux OS."
fi
fi
Loops in Shell Scripts
Bash supports:
1. for loop.
2.while loop.
Note that in each and every loop:
◦ First, the variable used in loop condition must be
initialized, then execution of the loop begins.
◦ A test (condition) is made at the beginning of
each iteration.
◦ The body of loop ends with a statement that
modifies the value of the test (condition) variable.
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
Example
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done
for Loop
Syntax:
for (( expr1; expr2; expr3 ))
do
repeat all statements between
do and done until expr2 is TRUE
Done
Example
for (( i = 0 ; i <= 5; i++ ))
do
echo "Welcome $i times"
done
Nesting of for Loop
$ vi nestedfor.sh

for (( i = 1; i <= 5; i++ ))


do
for (( j = 1 ; j <= 5; j++ ))
do
echo -n "$i "
done
while loop
Syntax:
while [ condition ]
do
command1 command2
command3 .. ....
done
Example
i=1
while [ $i -le 10 ]
do
echo "$n * $i = `expr $i \* $n`"
i=`expr $i + 1`
done
The case Statement
Syntax:
case $variable-name in
pattern1) command…..;;
pattern2) command…..;;
pattern N) command….;;
.
*) command ;;
esac
Example
read var
case $var in
1) echo “One”;;
2) echo “Two”;;
3) echo “Three”;;
4) echo “Four”;;
*) echo "Sorry, it is bigger than
Four";;
esac

You might also like