05 Bash Script Intro
05 Bash Script Intro
introduction
NB: all examples use emulator in
Linux essentials, not Linux
unhatched
• automate tasks
• reduce risk of errors
• combine sequences of commands
into a single command
• share procedures between users
• provide controlled access for users
• rapid prototyping
Available shell options
will use
w e
s h which
a
/bin/b efault
d
is the
• text editor
• arrow keys to move around
• delete and backspace to
delete text
• context-sensitive options at
bottom of screen
• CTRL&X to quit
• prompted to save file
Variables
Variables
(Ess 11.4.1)
e spaces
• variables store values within a script or terminal session
there must not b
v aria ble n ame, = and
be tw een
e w ill be an error
• to declare a variable with the value "Jan"
v a lue o r th er
name="Jan"
ded
quotes are not nee • to reference a variable inside a string
t but
in echo statemen
ity
improve readabil echo "My name is $name"
Environment variables
• PATH variable contains a list of directories the shell searches for commands
• directories are separated by :
• paths searched in order they are listed
• if command not found in any directory, shell will output "command not found"
• normally due to invalid command
• but can be directory containing command not in PATH variable
An aside about PATH variable (2 of 2)
• can add directories containing any custom software to the PATH variable
• must append or prepend values to PATH variable, not overwrite it
• otherwise will lose access to existing commands
Quotes
Quotes
(Ess 5.6)
• assign name="Jan"
Double quotes
(Ess 5.6)
commands must be
enclosed in parentheses
es variable
backslash overrid
bstitution
and command su
Single quotes
(Ess 5.6)
• TO DO:
• create directory called week5 create file
• change to week5 directory
• how?
make file
• create new file with sh as file extension executable
• how? sh indicates that it
is a shell script
• make file executable
• how?
run file
• run file
• using ./ followed by name of file
DEMO: first script (1 of 3)
(Ess 11.2)
• save file
DEMO: first script (2 of 3)
(Ess 11.2)
why is there
• check permissions with ls -l an error?
what does
this mean?
DEMO: first script (3 of 3)
(Ess 11.2)
• change permissions
chmod u+x hello.sh
what does
this do?
• run script
./hello.sh
#!/bin/bash
echo -n "What is your name? " -n in an echo statement
read name prevents newline at end
echo "Hello $name!"
• change permissions
• run file
do not forget to
pass a parameter
Numeric comparisons
-gt
[ is a com
mand,
-le equivalent
to test, wh if [ $age -eq 21 ]
requires a ich
-lt matching if test $age -eq 21
closing squ
are bracke
-ne t
r e e q uiv alent
these a
DEMO: compare numbers
if [ $# -eq 0 ]; then
echo "You must provide at least 1 argument"
fi
• change permissions
• run file
String comparisons
rue
returns t
DEMO: compare strings
#!/bin/bash
echo -n "What is your username? "
read name
if [ $USER = $name ]; then
echo "Correct username"
else
echo "Incorrect username"
fi
Complex conditions
if [ 01 = 1 ] && [ 01 -eq 1 ]
if [ 01 = 1 ]; then
if [ 01 -eq 1 ]; then
#executed if both true
File existence and permissions
• in addition to checking exit code from other commands, can also return
exit code from scripts
exit 1
DEMO: check file existence (1 of 3)
• prompt the user for a filename and delete the file if it is empty, outputting
suitable messages if the file does not exist or is not empty and returning exit
codes
• create an empty file called empty.txt
• create a file called notempty.txt containing "This file is not
empty"
• how?
(1 of 2) Jan)
echo "Delivers lectures and tutorials"
;;
Dan|Russell)
echo "Tutorials"
;;
*)
echo "Not a lecturer on WDOS"
m o re
is i t
esac
dab le?
rea
DEMO: check
strings using
case
(2 of 2)
• basic scripting • programs
• variables • hello.sh
• environment variables • greetings.sh
What we •
•
PATH variable
quotes
•
•
whoareyou.sh
findfiles.sh
have covered • exit codes • checkuser.sh
• arguments • checkargs.sh
• checkusername.sh
• deletefile.sh
• if statements • checkstaff.sh
• evaluate return value of command
• compare numbers
• compare strings
• check file existence, file
permissions
• complex conditions
• case statement
Any questions?