Bash

Hello world
while loop
read input
if loop
if multiple conditions loop
if -then else loop
Other useful commands
maths
&& ! and ;
run a command multiple times
Verbose output and running line by line
checking a parameter is passed
exit script on condition
Questions

Hello world

#!/bin/bash
echo "hello world"

while loop

add ; to make it run in one line

so while in a script you can add

while true
do
command
sleep 60
done

and if you want to run it at the command line run

while true ; do command ; sleep 60 ; done

or

$ while true
> do
> date
> sleep 5
> done
Sat Mar 5 14:32:11 GMT 2016
Sat Mar 5 14:32:16 GMT 2016
Sat Mar 5 14:32:21 GMT 2016

an example with a condition

#!/bin/bash
x=1
while [ $x -le 5 ]
do
echo $x
let x=x+1
done
~

for more info on true

read input

#!/bin/bash
echo "enter your name"
read name
echo $name

if loop

#!/bin/bash

echo "This scripts checks the existence of the messages file."
echo "Checking..."
if [ -f /var/log/messages ]
then
echo "/var/log/messages exists."
fi
echo
echo "...done.

#!/bin/bash
count=100
if [ $count -eq 100 ]
then
echo "Count is 100"
fi

if multiple conditions loop

using double pipes you can add more if conditions (rather than using lots of else conditions)

echo "enter your name"
read name
if [ $name = 123 ] || [ $name = 456 ] || [ $name = 789 ]
then
echo "correct"
else
echo "not"
fi

if -then else loop

If [ conditional expression ]
then
statement1
statement2
.
else
statement3
statement4
.
fi

#!/bin/bash
count=99
if [ $count -eq 100 ]
then
echo "Count is 100"
else
echo "Count is not 100"
fi

#!/bin/bash
#name=123
echo "enter your name"
read name
if [ $name = 123 ]
then
echo "correct"
else
echo "not"
fi

Other useful commands

a basic explanation of the true command
true

a basic explanation of the source command
source

 

maths

#!/bin/bash
x=5 # initialize x to 5
y=3 # initialize y to 3
add=$(($x + $y)) # add the values of x and y and assign it to variable add
sub=$(($x - $y)) # subtract the values of x and y and assign it to variable sub
mul=$(($x * $y)) # multiply the values of x and y and assign it to variable mul
div=$(($x / $y)) # divide the values of x and y and assign it to variable div
mod=$(($x % $y)) # get the remainder of x / y and assign it to variable mod

# print out the answers:
echo "Sum: $add"
echo "Difference: $sub"
echo "Product: $mul"
echo "Quotient: $div"
echo "Remainder: $mod"

&& ! and ;

lets you do something based on whether the previous command completed successfully

Furthermore, you also have || which is the logical or, and also ; which is just a separator which doesn't care what happened to the command before.
some examples

$ false || echo "Oops, fail"
Oops fail

$ true || echo "Will not be printed"
$

$ true && echo "Things went well"
Things went well

$ false ; echo "This will always run"
This will always run

run a command multiple times

for i in {1..5}; do date; done

or to introduce a time lag

for i in {1..5}; do date;sleep 3 ; done

Verbose output and running line by line

adding set -x to a script will show all the code as it runs

adding the following allows you to step through the code line by line

set -x
trap read debug

checking a parameter is passed

A useful little script so you can pass the script a variable and it will error if not i.e

./script test

#!/bin/bash
clear

if [ -z $1 ]; then
echo "please provide a parameter"
else
#stuff
fi

exit script on condition

set -e can be used to exit a script if there is an error ref http://stackoverflow.com/questions/1378274/in-a-bash-script-how-can-i-exit-the-entire-script-if-a-certain-condition-occurs

Questions

a few questions as a recap

1) write a hello world program
2) declare and write out a variable
3) do a basic if command
4) loop while a number is less that 10
5) read in an input and create an else if loop
6) run a loop to run the date command 5 times