Intro to Linux Shell Scripting

34
Intro to Shell Scripting Vern Ceder Canterbury School Shell Scripting An Introduction to Linux Shell Scripting Vern Ceder Canterbury School, Fort Wayne

description

Slides for "Intro to Linux Shell Scripting" given at the Hoosier Educational Computer Coordindator\'s meeting, Indianapolis, 90/13/2009

Transcript of Intro to Linux Shell Scripting

Page 1: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

Shell Scripting

An Introduction to Linux Shell Scripting

Vern CederCanterbury School, Fort Wayne

Page 2: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

This talk ISN'T for

# Experienced shell scripters

# People who own T­shirts saying “Go away or I will replace you with a very small shell script”

# Anyone with a favorite shell hack they MUST share... ;)

Page 3: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

This talk is for you if...

# You can open a terminal

# You can type

# You're interested in how to combine those two things to make your life easier 

Page 4: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

What's Coming

# Why use the shell?

# Basic shell commands

# Combining shell commands

# Creating simple scripts

# Automation with cron

Page 5: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

Do you...

Page 6: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

 $ Need to manage computers not on your desk?

$ Need to perform complex operations, on lots of files?

$ Need to repeat the same operations on a lot of machines?

$ Wish that some of this routine stuff could just happen?

Page 7: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

What's a “shell”?

# A program that uses (text) commands to talk to the OS

# DOS (e.g.)

# sh, bash, csh, ksh, zsh, etc...

# This talk will assume bash

Page 8: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

Basic Shell Commands

# Files – ls, cp, mv, rm, mkdir

# Access – chmod, chown

# Disks – df, mount 

# Processes – ps, kill, fg, bg

# Network – ifconfig, ping, tracepath

Page 9: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

Take ls, for example

# ls   

# ls ­l

# ls ­a

# ls ­t

# ls ­R

# ls ­latR

list files

list more info

list ALL

list by date

list Recursive

or combine

Page 10: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

More file commands

# cp a b  

# mv a b

# rm a

# rm ­rf a

copy files

move (rename)

delete 

remove EVERYTHING (danger, danger)

Page 11: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

Access commands

# chown user:group a  

# chmod ugo+rwx a

change file owner

change permissions

Page 12: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

disk commands

# df   

# du /folder

# mount

get free space

get amount used

show/change disks mounted

Page 13: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

process commands

# ps ax  

# kill processid

# fg 

# bg 

show processes

kill a process

foreground

background

Page 14: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

network commands

# ifconfig  

# ping

# tracepath 

show interfaces

ping a host

follow a packet

Page 15: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

So you can...

# Manage files

# Monitor disks

# Control user access

# Manage/debug network connections

Page 16: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

DANGER! 

You can also...

Completely destroy your system

Don't experiment/practice as root!

Page 17: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

But how do you know what does what?

# Man – the best way to find out what a command does

# Apropos – the way to find the right command

# tldp.org – man pages and howto's

# Google ;) 

Page 18: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

So?

How is this better than using a file manager?

# Powerful – you can do ANYTHING

# Remote – you can do it from ANYWHERE

# “Combinable” (that's next)

Page 19: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

The “unix idea”

# Small, specific utilities

# Text input/output

# “pipes” to connect one utility to another

Page 20: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

Commands to pull it all together

# cat   

# grep 

# cut

# sort

# uniq

# head, tail

# more, less

dump file

find a pattern

cut out a field

sort

remove dupes

get part of file

page through file

Page 21: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

Introducing the “pipe”

# | connects the output of one command to the input of another

# > (and >>) puts the output into a file

# < puts a file to input

Page 22: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

For example...

Find out who is hogging the home disk...

# du 

# sort 

# tail 

# du ­cks * | sort ­rn | head

Page 23: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

Another example...Who's tried to log into your box?

# cat 

# grep 

# cut 

# sort 

# uniq 

Page 24: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

cat /var/log/auth.log | grep "Failed password" | grep ­v "invalid" | cut ­d " " ­f 9,11 | sort | uniq

Page 25: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

Scripts

# To avoid retyping (and sometimes refiguring out)

# To handle more complex chores

Page 26: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

Creating a simple script

# Text file (text editor)

# The “shebang” line

# Shell commands

# Make it executable

Page 27: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

“space hog” as script

#!/bin/bash

du ­cks $1 | sort ­rn | head

Page 28: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

“login finder” as script

Login finder as a script

cat $1 | grep "Failed password" \ | grep ­v "invalid" | \

cut ­d " " ­f 9,11 |grep $2 | \

sort | uniq

Page 29: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

What I didn't cover(a very partial list)

# find

# tar

# environment vars

# echo 

# sleep 

# editors 

# Sed & awk

# cron & crontab

# loops 

# if 

# test ( [ ] )

Page 30: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

But what about automation?

What if you need to repeat the same script every month? Every week? Every day? Every hour? Every minute?

Page 31: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

Automating with cron

# cron lets you run scripts at any time, as any user

Page 32: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

Print Resources

# Classic Shell Scripting, Robbins & Beebe, O'Reilly

# Think Linux, Jon Lasser, QUE

# UNIX Hints and Hacks, Kirk Waingrow, QUE

# Linux in a Nutshell, Ellen Siever, et al. O'Reilly

# Many books on Linux/UNIX automation and administration

Page 33: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

Web Resources

# See http://tech.canterburyschool.org/tech/shell 

# Google “linux shell scripting” or the like

Page 34: Intro to Linux Shell Scripting

Intro to Shell Scripting

Vern CederCanterbury School

Video Resources

# http://video.google.com 

Search for “linux shell scripting tutorial”