Shell Scripting Crash Course
Shell Scripting Crash Course
Course
Travis Phillips
JAX LUG
Overview
What is a shell script
What can I do with a shell script
How to build shell scripts
Syntax basics
Basic useful shell commands
Pipes and redirectors
Overview (Continued)
Shell variables
Getting user input
Special script variables
Inline command substitution
Logic flow control
Check if user is root
Using functions for modulation of code
What is a shell script
A shell script is simply a file
It contains a series of several shell commands
on it’s own lines that the user could run via hand
typing in the shell directly.
Instead of having to type all of these
commands, this provides a means to just invoke
the file and each command would be run.
Provides a means of programming just using
what is already provided to us natively at the
shell
What Can I Do With
Them
Why should I care?
Scripts are useful for many things:
Automating common task.
Automating large task.
Simplifying a chain of several commands.
○ Some commands in Linux can become difficult to
remember once you add in all the options and switches.
Providing tools that ensure everyone is doing thing in
a uniform manner.
In short, ANYTHING YOU CAN DO WITH A
LINUX SHELL… Which is EVERYTHING!
Quick Warning On Automation
Computers are very fast.
Computers can process things in bulk.
Computers can automate your mistake…
VERY QUICKLY
AND IN BULK!!!
Use caution when you decide to
automate things and test it on a non-
production machine.
A Quick Look At A Shell
Script
#!/bin/bash
mkdir ~/test
touch ~/test/test1 ~/test/test2 ~/test/test3
ls -l ~/test
rm -rf ~/test/*
rmdir ~/test
A Quick Look At A Shell
Script
Save it in a file such as test.sh
Give it execute permissions
chmod +x test.sh
Finally, run it!
./test.sh
Pound Is Your Friend
Pound signs are used to place
comments. Anything after the pound is a
comment.
Comments are ignored by the shell and
are their for our purposes to comment
code and leave notes.
It is a good Idea to comment your code!
In fact, ALWAYS comment your code!
Shebang
As you may have noticed the first line was
“#!/bin/bash”.
A shebang is the “#!” part. It was followed with
the path to the bash binary
This tells the script that the correct binary to
execute this with is /bin/bash. The bash shell.
Useful to ensure the shell has the functionality you
are requesting.
MUST BE ON THE VERY FIRST LINE!!!
This is so it knows it’s not a comment since it starts
with a pound sign.
Shell Basics
Quick Guide of useful commands.
ls – used to List files and directories and
return info on them.
mkdir – Makes a directory.
cp – Copies a file or directory
mv – Moves a file or directory. Can also be
used to rename a file.
echo – takes input information and outputs
it to the screen.
○ Note: -e and -n are useful with this one.
Shell Basics
sleep – makes the shell pause and wait a given
amount of seconds.
cut – A tool used to extract info from a given byte
range or delimited field.
grep – A useful tool to locate string or regular
expression matches.
sed – a stream editing tool that is useful for find and
replace operations. Supports regular expressions.
man – the manual. Contains info on commands
Always RTFM before asking questions to avoid being
flamed.
Shell Basics
Pipes -- |
Pipes are used to take one programs output and
feed it into another program.
Useful for stacking program functionality such as
grep.
○ “Put that in your pipe and grep it!” Hacksonville
Slogan.
Unix follows the idea that a tool should do one thing,
one thing only, do that one thing well, but be able to
play nicely with others.
Example to view only TCP ports in netstat
○ netstat -l | grep “^tcp”
Shell Basics
Redirectors -- > <
Redirectors are used to send stderr and/or
stdout somewhere else such as a file.
This can be useful for logging purposes.
○ netstat > ~/netconnections.txt
Or to silence a commands output if you wish
to keep it suppressed.
○ sudo apt-get install nmap >
/dev/null
Shell Basics
Escape characters -- \
Backslash is used to escape special
characters we don’t want the shell to
interrupt and instead just treat as a string.
○ Example would be redirectors or pipes for
example.
Also escape spaces with it as the shell uses
spaces as a delimiter between arguments.
Backslash is also escaped with a backslash.
○ Think double negatives: \\
Working With Shell
Variables
Shell variables are a place to store data.
Can only contain A-Z, 0-9, and
underscores (_).
By convention should be uppercase.
Set Static using var_name=value
Example: MYVAR=Stuff
Once set, use in your script by putting a
dollar sign.
Example: echo $MYVAR
Working With Shell Variables
You can also read values dynamically at
runtime from the user using the (wait for
it….) read command
Example: read MYVAR
This will read till the user hits enter and
place the user input in the variable MYVAR.
Special Shell Variables to Be
Aware of
$0 – Current script
$[number] – arguments passed to the script.
Example: myscript.sh testing this out
○ $0 would be “myscript.sh
○ $1 would be “testing”
○ $2 would be “this”
○ $3 would be “out”
○ $4, $5, $6, etc would “”
$# - Gets the total number of arguments
passed to a script.
Special Shell Variables to Be
Aware of
$* - Passes in all of the arguments. This is
useful for FOR loops.
Example: myscript.sh test testing
○ These are the same:
echo $1 $2
echo $*
Values AND OR
TRUE:TRUE True True
TRUE:FALSE False True
FALSE:FALSE False False
So Say We Wanted to Check If a
User Was Root
Check the UID.
If it’s 0, proceed.
If not, throw error.
This in a Script Would
Be…
#!/bin/bash