0% found this document useful (0 votes)
117 views

Module 6

This document provides an introduction to shell scripting in Linux. It discusses that a shell script is a file containing shell commands that are executed sequentially to perform automation tasks. It describes different types of shells like sh, bash, ksh and csh. It also explains how to write basic shell scripts using commands like echo, create scripts using control structures like if-then and for loops, and take user input. The document concludes with discussing aliases and command history.

Uploaded by

Mihai S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views

Module 6

This document provides an introduction to shell scripting in Linux. It discusses that a shell script is a file containing shell commands that are executed sequentially to perform automation tasks. It describes different types of shells like sh, bash, ksh and csh. It also explains how to write basic shell scripts using commands like echo, create scripts using control structures like if-then and for loops, and take user input. The document concludes with discussing aliases and command history.

Uploaded by

Mihai S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

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

You might also like