LinuxFest Northwest 2013

Bellingham, WA April 27th & 28th

Platinum Sponsors

Beginning Shell Scripting with Bash

morris's picture

The Shell (bash) is a command-line interface for invoking other programs on the Linux system.  It is a also a powerful programming language for controlling complex job flows using pipelines and conditional/looping constructs.  Combined with the GNU coreutils programs and perhaps a few simple filter programs written in Perl or Python, complex programs can be composed in just a few lines.

This session will provide a live demo showing how fairly complex systems can be built from the available primitive operations.  For example, to count down the top ten words (Letterman-style) occurring in a file, say mydata.txt, you may type this command line:

cat mydata.txt | sed -r -e 's/[^A-Za-z]+/\ /g' | sed -e '/^$/d' | sort | uniq -c | sort -n -r | head -10 | cat -n | tac

Better yet, save it in a script file, top-ten-words.sh that you can reuse:

#! /bin/bash cat $* | sed -r -e 's/[^A-Za-z]+/\ /g' | sed -e '/^$/d' | sort | uniq -c | sort -n -r | head -10 | cat -n | tac

Looks complex and possibly even slightly intimidating at first, but we'll build it up piece by piece.

 

Slides are available at: https://docs.google.com/presentation/d/1W1plgpZ4S9qgvt-5MWyAbty26uHpu-K-Fe5qfM61u3I/

Demo examples: https://docs.google.com/presentation/d/1ZNdlLJ6txF1znvG8Ha_VfA5xcPll7kdhspH_XgA7lHU/edit?usp=sharing

 

About the presenter

Morris Bernstein is an experienced software engineer, most recently Founder and President of Systems Deployment, LLC (http://www.systems-deployment.com ), a training and consulting service focusing on Open Source Software .  He is currently teaching Data Structures and Algorithms at UWB (http://courses.washington.edu/css343/bernstein ).  For additional information, consult his linkedin profile:  http://www.linkedin.com/pub/morris-bernstein/52/519/6a0

Experience level: 

Beginner

Speaker(s): 

morris

Session Time Slot(s): 

Saturday, April 27, 2013 - 10:00 to 10:50

Track: 

Comments

uuoc

So, not to be overly nit-picky, but your examples begin with a glaring "useless use of cat". https://en.wikipedia.org/wiki/Cat_%28Unix%29#Useless_use_of_cat. Instead of running can on mydata.txt, just run sed on it directly.

re: uuoc

Meh.  As I mentioned on the slide, cost of an extra process vs. readability enhancement of a left-right scan of a complex pipeline .  YMMV.  Besides, it makes it easy to replace specific file name on the command line with the more general $* in the saved script.