conditionals
If statements
Test statement equality as so:
1
2read t1
3read t2
4if test $t1 != $t2; then
5 echo 'variables do not match'
6else
7 echo 'variables match'
8fi
9exit 0
Case Structure
These deal with multiple states rather than forking conditions.
Example:
1
2#!/bin/bash
3
4# Simple case demonstration
5
6echo "What's your favourite creature?"
7read CRE
8case $CRE in
9 human | humanoids ) echo "Why is $CRE always standard?"
10;;
11 troll | monsters ) echo "Not exactly known for their character ..."
12;;
13 owlbears | monsters ) echo "Really you're a wizard fan"
14;;
15esac
While and Until
This prints from 1 until 9.
1COUNTER=1
2while [ $COUNTER -lt 2 ]; do
3 > ((COUNTER++))
4 > echo $COUNTER
5> done
There's also 'until', which stops when something is true, rather than keeping going when something is true.
For
1for i in $( ls ); do
2> du -sh $i
3> done
Sequences
The sequences tool counts up from X in jumps of Y to number Z.
Count from 1 to 10.
1seq 10
Count from 4 to 11.
1seq 4 11
Count from 1 to 100 in steps of 5.
1seq 1 5 100