Shell Scripts - Automating the Command Line
Basic
Part 1 : Introduction
Why?
Top three reasons:
Automation
Automation
Automation
Other reasons
- Speed
- Human error
- Boredom
Anything you can type at the command line can be saved in a script
and ran later.
This is how we automate tasks.
Demo - First Shell Scripts
demos/01-first_script.sh
Part 2 : Automate a tedious task
Script development
- If you open up two terminals, you can edit your script and run it
without closing the editor.
- This is good practice…
Demo - (Possibly) Useful Shell Scripts
Let’s write a shell script to automatically download and untar the
sandbox for this module from the website.
demos/02-sandbox_download.sh
Part 3 : Improve
Improvements
Let’s update our script to delete any old tarballs for this module
before we download the new one, so we don’t get multiple copies.
Demo - Sandbox Download Improvement
demos/03-sandbox_download-redux.sh
Part 4 : Variables
Variables
- Variables allow us to store pieces of text that we are going to want
to use repeatedly.
- Syntax:
- Assignment: Set the variable name equal to a value
x=value
y="value with spaces"
- Variable name, equal sign, and value cannot have
spaces between them. Otherwise the shell thinks you are trying to run a
command.
x= value
`/bin/sh: 1: value: not found`
x =value
`/bin/sh: 1: x: not found`
x = value
`/bin/sh: 1: x: not found`
- Expansion: Put variable name in a
${}
.
msg="Hello World"
echo ${msg}
`Hello World`
Variables
- Variables can be:
num=10
echo "There are ${num} data points."
output: `There are 10 data points.`
x=10
y=${x}
msg="x and y are ${x} and ${y}"
echo ${msg}
output: `x and y are 10 and 10`
msg="Hi."
msg="${msg} My name is..."
msg="${msg} What?"
msg="${msg} My name is..."
echo "${msg} Huh?"
output: `Hi. My name is... What? My name is... Huh?`
Demo - Variables
Let’s update our download-and-untar script to use a variable for the
module number…
demos/04-variables.sh
Part 5 : Allowing Arguments
Positional Parameters
- Your shell script can accept arguments from the command line.
- The shell will pass any arguments given to your script into the
“positional parameters”
- These are variables named
$1
, $2
,
$3
, etc.
Demo - Positional Parameters
demos/05-positional_parameters.sh
Part 6 : Errors, loops, and conditionals
Bailing on errors
If a command in a script fails, the script continues to run the next
commands.
This can be bad…
If ~/tmp
does not exist, this will delete everything in
the home directory!
Bailing on errors
The shell has an option we can set to say “if there is an error, stop
running commands”
#! /bin/bash
echo "going to home..."
cd
echo "going to missing"
cd missing
echo "inside missing"
pwd
going to home...
going to missing
inside missing
/home/cclark
Bailing on errors
The shell has an option we can set to say “if there is an error, stop
running commands”
#! /bin/bash
set -e
echo "going to home..."
cd
echo "going to missing"
cd missing
echo "inside missing"
pwd
going to home...
going to missing
Running commands repeatedly
Sometimes we want to run the same commands for several different
arguments.
The shell provides for loop for this:
for text in one two three
do
echo ${text}
done
Running commands repeatedly
for text in 1 2 3 4
do
echo file-${text}.txt file-${text}.txt.bak
done
file-1.txt file-1.txt.bak
file-2.txt file-2.txt.bak
file-3.txt file-3.txt.bak
file-4.txt file-4.txt.bak
Demo - For loops
demos/07-loops.sh
Running commands conditionally
Sometimes we need to check if something is true and run some commands
if it is (or is not).
This can be useful for file handling
cd
if [ -e tmp ]
then
echo "tmp exists"
else
echo "tmp does NOT"
fi
if [ -f tmp ]
then
echo "tmp is a file"
else
echo "tmp is NOT a file"
fi
if [ -d tmp ]
then
echo "tmp is a directory"
else
echo "tmp is NOT a directory"
fi
tmp exists
tmp is NOT a file
tmp is a directory
Demo - Conditionals
demos/08-conditionals.sh
Part 7 : Running scripts as commands
The PATH Variable
- When type a command at the command line, the shell looks for an
executable file with the command name in a list of directories.
- The list of directories that will be searched is the
PATH
variable.
- You can see what your
PATH
variable looks like by
echo
ing it
PATH
is just one of many environment variable.
There are others.
echo ${HOME}
echo ${USER}
echo ${RANDOM}
- Lets modify our path variable so that we can use any script we put
in
~/bin
as if they were commands.
Demo - The Path Variable
demos/10-path.sh
Misc - The date
command
The date command will print out the current date.
$ date
Mon Apr 14 10:31:22 AM CDT 2025
Last Slide