0% found this document useful (0 votes)
74 views20 pages

Shell Scripting

This document provides information on shell scripting including: 1. Shell scripts can automate repetitive tasks like backups, software installation, monitoring, and notifications. 2. The kernel acts as an interface between hardware and software, while the shell is an interface between the user and the kernel. Common shells include sh, bash, csh, and ksh. 3. The document discusses shell script basics like variables, conditionals, loops, reading/writing files, and exit statuses to handle errors.

Uploaded by

kiranmailbox7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
74 views20 pages

Shell Scripting

This document provides information on shell scripting including: 1. Shell scripts can automate repetitive tasks like backups, software installation, monitoring, and notifications. 2. The kernel acts as an interface between hardware and software, while the shell is an interface between the user and the kernel. Common shells include sh, bash, csh, and ksh. 3. The document discusses shell script basics like variables, conditionals, loops, reading/writing files, and exit statuses to handle errors.

Uploaded by

kiranmailbox7
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 20

Shell Scripting

Uttam Kiran
Why Shell scripts

Automate repetitive tasks such as

1. Automate Daily Backup


2. Automate Installation and patching of software on multiple servers
3. Monitor system periodically
4. Raise alarm and send notifications
5. Troubleshooting and Audits

Many More
Kernel And Shell

1. What is Kernel
2. What is Shell
3. Types of shells
4. Starting a Shell
5. How to run a shell script
Kernel & Shell

Kernel : Interface between hardware and

software

Operating System = Kernel + Shell

Software = Shell + Application (Browser + SendMail)

cat /proc/version & uname -r

GUI interface is Windows Shell


CLI (Terminal) Is a shell in Linux

# echo $0
Shell Script & Type of Shells

Keeping all the instructions in a file and execute them with mentioned shell.

On windows user do it with GUI.

Linux provide you Command line OS the user to perform the operations on hardware.

GNOME & KDE - GUI desktop interface

sh - bourne shell - Unix original shell

bash -Bourne Again Shell

csh , tcsh - C shells no linux commands work on it

ksh - korn shell developed by David korn


Basic Tasks

#!/bin/bash

echo Hello world

echo “This script performs few basic tasks”

ls

pwd whomai date cal touch a b c

echo “end of script”


Basic Administration Tasks

top , df -h , free -m, uptime, iostat

system_check.sh to check the system health


Define Variables

A variable is a character string to which we assign a value. The value assigned could be a number, text, filename,
device, or any other type of data.
Input/Output

Create a script that wait for user input

read

echo
if-then scripts
if-then statements

if this happens = do this

Otherwise = do that

#!/bin/bash

echo please enter the number

read count

echo $count

if [ $count -eq 100 ]


if-then condition
#!/bin/bash

clear

file=`pwd`/error.txt

echo $file

if [ -e $file ]

then

echo File exist

else

echo File doesn\'t exist


Case Statements scripts

.Case
#!/bin/bash

If option a is selected = do this echo


echo "Please enter your choice"
If option b is selected = do this echo
echo "a= Display date and Time"
If option c is selected = do this echo "b= List all the files in current Directory"
echo "c= who logged in"
echo "d= Check server uptime"

read choices
case $choices in
a) date;;
b) ls;;
For loop scripts

For loop
#!/bin/bash
Keep running the scripts until
for i in 1 2 3 4
specified no of variable do
echo "The value is $i"
Eg: variable=10 run the script 10 times Done
—-----------------------------
#!/bin/bash

for i in {1..5}
do
touch $i
done
do…while
#!/bin/bash
The while condition continuously executes
count=0
A block of statement while a particular condition while [ $count -lt 10 ]
do
is true or false echo $count
count=`expr $count + 1`
count = $[$count +1 ]
while [ condition ] done
echo Done
do —-------------------------------------------------------------
#!/bin/bash

Condition 1 count=0
num=10
while [ $count -lt 10 ]
Condition 2 do
echo
done echo $num seconds left to stop this process $1
echo
Exit Status

0 - OK or Successful

1 - minor problem

2 - 255 - serious problem

echo $?

1 - indicates that a container shut down, either because of an application failure or because the image pointed to an
invalid file (cat asdasdasd 000 )

127 - command not found

130 - The job was terminated by the owner.


Real life programs

Finding errors, WARN,debug from a file and writing to a file

grep -i error /../../../syslog.1 > ../../error.log

grep -i warn /../../../syslog.1 > ../../warn.log

grep -i debug /../../../syslog.1 > ../../warn.log

Use of awk → format the output of the script | awk ‘{$print $1}’ | wc -l
Script for server Connectivity

Ping the servers from script


#!/bin/bash

#!/bin/bash hosts=172.31.81.34
hosts="/home/ubuntu/prof-s ping -c1 $hosts &> /dev/null
cripts/hosts" if [ $? -eq 0 ]
for ip in $(cat $hosts) then
do echo $hosts is OK
ping -c1 $ip &> /dev/null else
if [ $? -eq 0 ] echo $hosts Not OK
then fi
echo $ip is OK
else
echo $ip Not OK

Script for server Connectivity

Create a hosts file in the present directory and save all IPs in the file.

HW: configure this script as a cronjob

crontab -e

#44 09 * * * /home/ubuntu/prof-scripts/ping-script-all > ping-output


Find old files (move,Remove)script

# find <directory_path> -mtime +90 -exec ls -l {} \; —> to find 90 days old files.

#find <directory_path> -mtime +90 -exec rm {} \; → delete 90 days old files

# find <directory_path> -mtime +90 -exec mv {} {}.old \; → rename 90 days old files

Create files with old date

touch -d “Thu, 1 March 2018 12:30:00” a

touch -d “Thu, 1 March 2018 12:30:00” b

touch -d “Thu, 1 March 2018 12:30:00” c

#find /sourcedirectory -maxdepth 1 -mtime +365 -exec mv "{}" /destination/directory/ \;


Backup FileSystem

Create the back up of /etc and /var using ‘tar’ command

Compress .tar file with gzip command

Write a script to automate backups

# tar -cvf /tmp/backup.tar /etc /var

You might also like