Simple Bash Scripting
Sequence, selection and Iteration: https://en.wikipedia.org/wiki/Structured_programming#Theoretical_foundation
-
Sequence
- Ordered statements
- a = b * 3
- print "stuff"
- foo = sin(3)
-
Selection
- Choose one or another set of statements
- if i = 7 then
- else
- do something else or perhaps nothing
-
Iteration
- Do a set of statements repeatedly
#! /bin/bash
# Structured programming
## https://en.wikipedia.org/wiki/Structured_programming#Theoretical_foundation
#! /bin/bash
# sequence
a=5
b=3
echo $a
echo $b
echo $((a + b))
declare -i c;
c="5 + 4"
echo "$c"
c=$c+1
echo "$c"
## parameters
echo "$1" "$2"
echo -----------------------
#! /bin/bash
# selection
## about [
if [ "$1" = 'foo' ]; then
echo you have foo
else
echo you are fooless
echo but you have "$1" and "$2"
fi
if [ -e "$2" ]; then
echo the file "$2" exists
else
echo the file "$2" does not exist here.
fi
echo -----------------------------------
#! /usr/bin/env bash
# iteration
## about let https://www.computerhope.com/unix/bash/let.htm
n=0
m=$1
while [ $n -lt "$m" ]; do
echo $n
let n=n+1
done
Find Bugs in Your Shell Scripts
https://www.shellcheck.net/