Welcome To:
Module 6
Shell Scripting
Linux Kernel
• What is a Kernel?
• Interface between hardware and Software
Browser, sendmail
Software
GUI, bash, csh
Operating System
Program
CPU, Memory, HD
By: Imran Afzal
Introduction to Shell
• What is a Shell?
• Its like a container
• Interface between users and Kernel/OS
• CLI is a Shell
• Find your Shell
• echo $0
• Available Shells “cat /etc/shells”
• Your Shell? /etc/passwd
• Windows GUI is a shell
• Linux KDE GUI is a shell
• Linux sh, bash etc. is a shell
By: Imran Afzal
Types of Shell
• sh
• bash
• ksh
• csh
Starting a Shell
• Type shell name e.g. csh
• Type exit to exit out of shell
By: Imran Afzal
Shell Scripting
• What is a Shell Script?
A shell script is an executable file containing multiple shell commands that are executed
sequentially. The file can contain:
• Shell (#!/bin/bash)
• Comments (# comments)
• Commands (echo, cp, grep etc.)
• Statements (if, while, for etc.)
• Shell script should have executable permissions (e.g. -rwx r-x r-x)
• Shell script has to be called from absolute path (e.g /home/userdir/script.bash)
• If called from current location then ./script.bash
By: Imran Afzal
Shell Script – Basic Scripts
• Output to screen using “echo”
• Creating tasks
• Telling your id, current location, your files/directories, system info
• Creating files or directories
• Output to a file “>”
• Filters/Text processors through scripts (cut, awk, grep, sort, uniq, wc)
By: Imran Afzal
Input and Output of Script
• Create script to take input from the user
read
echo
By: Imran Afzal
if-then Scripts
• If then statement
If this happens = do this
Otherwise = do that
By: Imran Afzal
For Loop Scripts
• For loops
Keep running until specified number of variable
e.g: variable = 10 then run the script 10 times
OR
variable = green, blue, red (then run the
script 3 times for each color.
By: Imran Afzal
do-while Scripts
• do while
The while statement continually executes a block of statements while a
particular condition is true or met
e.g: Run a script until 2pm
while [ condition ]
do
command1
command2
commandN
done
By: Imran Afzal
Case Statement Scripts
• Case
If option a is selected = do this
If option b is selected = do this
If option c is selected = do this.
By: Imran Afzal
Check Other Servers Connectivity
• A script to check the status of remote hosts
By: Imran Afzal
Aliases
• Aliases is a very popular command that is used to cut down on lengthy and
repetitive commands
alias ls="ls -al“
alias pl=“pwd; ls”
alias tell=“whoami; hostname; pwd”
alias dir="ls -l | grep ^d"
alias lmar=“ls –l | grep Mar”
alias wpa= "chmod a+w"
alias d="df -h | awk '{print \$6}' | cut -c1-4"
By: Imran Afzal
Creating User or Global Aliases
• User = Applies only to a specific user profile
• Global = Applies to everyone who has account on the system
• User = /home/user/.bashrc
• Global = /etc/bashrc
alias hh=“hostname”
By: Imran Afzal
Shell History
• Command “history”
By: Imran Afzal