Lesson 10: Writing Scripts
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com 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 Chi.Nguyen.e4w@gmail.com Page 2
Customize or write simple scripts
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com Page 3
Global environment variables
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com Page 4
Local environment variables
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com Page 5
Setting local and global variables
Local variables Global variables
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com 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 Chi.Nguyen.e4w@gmail.com Page 7
Using Command Aliases
Using Command Aliases
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com Page 8
Shell features
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com Page 9
Shell script format
Shebang sign
Commands Comment
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com Page 10
Running shell scripts
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com Page 11
Shell variables
Global environment variables
Local variables
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com Page 12
Shell script arguments
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com Page 13
Time for labs
Execise 20: Writing scripts – Part 1
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com Page 14
Shell script – Accept input from user
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com Page 15
Shell script – Accept input from user
Limit the character for reading
Timing out reading
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com Page 16
Shell script – Accept input from user
Silent reading
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com 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 Chi.Nguyen.e4w@gmail.com Page 18
Shell script programming
Command subtitution
Performing math
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com Page 19
Shell script programming
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com Page 20
Shell script programming
if [ condition ]
then
commands
fi
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com Page 21
Shell script programming
if [ condition ]
then
commands
fi
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com Page 22
Shell script programming
case variable in
pattern1) commands1;;
pattern2 | pattern3) commands2;;
*) default commands;;
esac
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com Page 23
Time for labs
Execise 21: Writing scripts – Part 2
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com Page 24
Shell script programming
for variable in series ; do
commands
done
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com Page 25
Shell script programming
while [ condition ] ; do
commands
done
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com Page 26
Shell script programming
function name {
commands
}
Or
name() {
commands
}
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com Page 27
Running script in background
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com Page 28
Automate system administration
tasks by scheduling jobs
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com Page 29
Scheduling script to run – at
at [-f filename] time
Time format
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com Page 30
Scheduling script to run – at
at send output of the script via mail
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com Page 31
Scheduling script to run – at
List pending jobs
Remove job
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com 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 Chi.Nguyen.e4w@gmail.com Page 33
Time for labs
Execise 22: Writing scripts – Part 3
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com Page 34
Question...!
This document is created by Nguyen Hoang Chi Chi.Nguyen.e4w@gmail.com Page 35