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

Lesson 10 - Writing Scripts

This document discusses writing and customizing scripts. It covers objectives like writing simple scripts and automating system administration tasks by scheduling jobs. It provides information on scripting basics like variables, conditional statements, loops, functions and scheduling scripts to run periodically using cron jobs. The document also includes exercises for readers to practice scripting.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lesson 10 - Writing Scripts

This document discusses writing and customizing scripts. It covers objectives like writing simple scripts and automating system administration tasks by scheduling jobs. It provides information on scripting basics like variables, conditional statements, loops, functions and scheduling scripts to run periodically using cron jobs. The document also includes exercises for readers to practice scripting.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Lesson 10: Writing Scripts

This document is created by Nguyen Hoang Chi [email protected] Page 1


Objectives covered

o 105.2 Customize or write simple scripts (weight: 4)


o 107.2 Automate system administration tasks by scheduling jobs
(weight: 4)

This document is created by Nguyen Hoang Chi [email protected] Page 2


Customize or write simple scripts

This document is created by Nguyen Hoang Chi [email protected] Page 3


Global environment variables

This document is created by Nguyen Hoang Chi [email protected] Page 4


Local environment variables

This document is created by Nguyen Hoang Chi [email protected] Page 5


Setting local and global variables

Local variables Global variables

This document is created by Nguyen Hoang Chi [email protected] Page 6


Locating System Environment Variables

1. /etc/profile
2. $HOME/.bash_profile $HOME/.bashrc $BASH_ENV variable
3. $HOME/.bash_login
4. $HOME/.profile

Login shell Interactive shell Noninteractive shell

This document is created by Nguyen Hoang Chi [email protected] Page 7


Using Command Aliases

Using Command Aliases

This document is created by Nguyen Hoang Chi [email protected] Page 8


Shell features

This document is created by Nguyen Hoang Chi [email protected] Page 9


Shell script format

Shebang sign

Commands Comment

This document is created by Nguyen Hoang Chi [email protected] Page 10


Running shell scripts

This document is created by Nguyen Hoang Chi [email protected] Page 11


Shell variables

Global environment variables

Local variables

This document is created by Nguyen Hoang Chi [email protected] Page 12


Shell script arguments

This document is created by Nguyen Hoang Chi [email protected] Page 13


Time for labs

Execise 20: Writing scripts – Part 1

This document is created by Nguyen Hoang Chi [email protected] Page 14


Shell script – Accept input from user

This document is created by Nguyen Hoang Chi [email protected] Page 15


Shell script – Accept input from user

Limit the character for reading


Timing out reading

This document is created by Nguyen Hoang Chi [email protected] Page 16


Shell script – Accept input from user

Silent reading

This document is created by Nguyen Hoang Chi [email protected] Page 17


Shell script – Exit status

Command completed successfully have the exit value 0

Control the exit value of shell script with exit command

This document is created by Nguyen Hoang Chi [email protected] Page 18


Shell script programming

Command subtitution

Performing math

This document is created by Nguyen Hoang Chi [email protected] Page 19


Shell script programming

This document is created by Nguyen Hoang Chi [email protected] Page 20


Shell script programming

if [ condition ]
then
commands
fi

This document is created by Nguyen Hoang Chi [email protected] Page 21


Shell script programming

if [ condition ]
then
commands
fi

This document is created by Nguyen Hoang Chi [email protected] Page 22


Shell script programming

case variable in
pattern1) commands1;;
pattern2 | pattern3) commands2;;
*) default commands;;
esac

This document is created by Nguyen Hoang Chi [email protected] Page 23


Time for labs

Execise 21: Writing scripts – Part 2

This document is created by Nguyen Hoang Chi [email protected] Page 24


Shell script programming

for variable in series ; do


commands
done

This document is created by Nguyen Hoang Chi [email protected] Page 25


Shell script programming

while [ condition ] ; do
commands
done

This document is created by Nguyen Hoang Chi [email protected] Page 26


Shell script programming

function name {
commands
}

Or

name() {
commands
}

This document is created by Nguyen Hoang Chi [email protected] Page 27


Running script in background

This document is created by Nguyen Hoang Chi [email protected] Page 28


Automate system administration
tasks by scheduling jobs

This document is created by Nguyen Hoang Chi [email protected] Page 29


Scheduling script to run – at

at [-f filename] time


Time format

This document is created by Nguyen Hoang Chi [email protected] Page 30


Scheduling script to run – at

at send output of the script via mail

This document is created by Nguyen Hoang Chi [email protected] Page 31


Scheduling script to run – at

List pending jobs

Remove job

This document is created by Nguyen Hoang Chi [email protected] Page 32


Scheduling script to run – cron table

min hour dayofmonth month dayofweek command

Command Description
$ crontab –l List all the entries on the cron table of your
user
$ export Set the EDITOR variable value to vi editor
EDITOR=vi for using when edit the crontab
$ crontab –e Edit the crontab with the EDITOR pre set

This document is created by Nguyen Hoang Chi [email protected] Page 33


Time for labs

Execise 22: Writing scripts – Part 3

This document is created by Nguyen Hoang Chi [email protected] Page 34


Question...!

This document is created by Nguyen Hoang Chi [email protected] Page 35

You might also like