Shell Scripts - Automating the Command Line

Basic

Part 1 : Introduction

Why?

Top three reasons:

Other reasons

Anything you can type at the command line can be saved in a script and ran later.

This is how we automate tasks.

Writing Shell Scripts

Demo - First Shell Scripts

demos/01-first_script.sh

Part 2 : Automate a tedious task

Script development

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

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

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…

cd
cd tmp
rm * -rf

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  
       
one
two
three

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

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

Practice

Last Slide