Bash Shell Scripting
Crash Course
crowfly.net

<<   >>

Introduction

GNU_Tools

Example1

Example2

Execution_bit

Bang_bin_bash

Example3

The_dot

Example4

Example5

Example6

Example7

Example8

Background

Forking

Example9

Bash_profile

Pattern_match

Regex

Debug

Gory_details

References

<<   >>
Pattern Matching

Bash shell supports these crazy pattern matching syntaxes:

Assume variable mojo is set as follows:

  mojo="/home/dlink/i.am.mojo.jojo"

Then these statements give the value on the left:

  ${mojo##/*/}              i.am.mojo.jojo
  ${mojo#/*/}         dlink/i.am.mojo.jojo
  ${mojo}       /home/dlink/i.am.mojo.jojo
  ${mojo%.*}    /home/dlink/i.am.mojo
  ${mojo%%.*}   /home/dlink/i

Here's how you would use this:

Say, you want to rename all *.JPG files to *.jpg files:

  dlink@viddev1> for f in *.JPG; do g=${f%%.*}; mv -v $f $g.jpg; done
  `DSC_0709.JPG' -> `DSC_0709.jpg'
  `DSC_07250001.JPG' -> `DSC_07250001.jpg'
  `DSC_07430001.JPG' -> `DSC_07430001.jpg'
  `DSC_076700011.JPG' -> `DSC_076700011.jpg'
  `HudsonRiver03.JPG' -> `HudsonRiver03.jpg'
  `HudsonRiver08.JPG' -> `HudsonRiver08.jpg'
  `HudsonRiver18.JPG' -> `HudsonRiver18.jpg'
  `HudsonRiver26.JPG' -> `HudsonRiver26.jpg'

It is better to use regular expressions whenever possible. See next slide ...