35
Shell Scripting Crash Course Travis Phillips JAX LUG

Shell Scripting Crash Course - JaxHax

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Shell Scripting Crash

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 variablesGetting 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 binaryThis 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.txtOr 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 commandExample: read MYVARThis 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 $2echo $*

$? – Gets the error code (exit() status) of the last program executed.This is useful if you need to determine if a

command was successful or not and respond differently depending on that.

Special Shell Variables to Be Aware of

$$ - Gets the PID of the current shell. Since a shell script executes in the shell, that would be the PID of the script.

$! – Gets the PID of the last background process. Useful for managing timeout threads on background threads.

Special Shell Variables to Be Aware of

$EUID – Gets The EFFECTIVE UID number of the scripts execution. This is useful if you need to check that the user is running as root (or used sudo) or to check that a script is only executed by a application user.

Command Substitutions

If we need to do a command substitution (that is, execute a command and drop it’s output directly inline) we have two options.First is using $()

○ $(command)Second is using backticks

○ `command`

Useful tool for setting a variable with the output of a command.

Logic Control using if

The If command enables us to determine logic flow using a true or false statement.

Terminates with fi command. else controls how it flows if it doesn’t

return true. elif to stack another if in the else

statement.

If Compare Operators

-eq : is equal to -ne : is not equal to -lt : is less than -gt : is greater than -le : is less than or equal to. -ge : greater than or equal to.

If Switches

-s : file exists and is not empty -f : file exists and is not a directory -d : directory exist -x : file is executable -w : file is writable -r : file is readable

And Or Operators

&& : AND operator – Requires both sides of it be true

|| : OR operator – Requires one or both sides of it to be true

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

if [ $EUID -eq "0" ]; then

echo "Yay!!! You are Root! WOOT!"

else

echo "ERROR: SCRIPT MUST RUN AS ROOT!"

fi

Can’t Spell Functions Without FUN!

Functions provide us a means of modulating our code.This makes the code easier to debug…This makes the code more manageable…This makes your code smallerThis makes your code reusable!

Helps to follow the coding practice of “Don’t Repeat yourself”.

Can’t Spell Functions Without FUN!

Functions are created with a name. You later just call it by name and the block of code with in it will execute.

Can take input parameters and return a value as well.

Functions MUST BE CREATED BEFORE YOU USE THEMSo rule of thumb: Keep your functions at the

top of your script.

Example of When This is a Good Idea

Anatomy of a Shell Function

A Real World Example Syntax usage message. Make it a function.

Why not?We will print it if we don’t get arguments from the

userOr if the arguments are invalid throw it thereOr if we get -h or --help.

since it may be a few lines echo statements, why not put it in a function call showUsage()?

Simple to update changes to it in one spot rather than 3 or 4 spots, 1 or a few may be missed if you have to do that.

Recap The Linux shell is awesome

No I’m not biased.

A shell script is a file that has a stack of shell commands in it that will execute when invoked.

Functions make code easier to manage. Comments should be used often. Only limitations are really a lack of creative thinking. If you haven’t already, run the command

man bashRead it. It is length but a lot of really good info lives there.

Q&A Time