Basics of Bash Scripting
Bash commands can be used in a script to perform complex functions
The keyword ENDOFTEXT can be used to denote a multiline string
Standard Out/In
Standard Out and Standard In acts as buffers to for input and output
If statements
If statements can be implemented via the syntax
if ( [condition] ); then
# function
fi
Integer comparisons are represented by -ne(!=), -lt(<), -gt(>), -le(<=), and -ge(>=)
Mathematical Expression
Mathematical expressions can be represented by (( [expression] ))
Standard integer operations such as ++, // can be used
Constants
Constants can be denoted via the readonly modifier
Functions
Functions can be implemented via the syntax
afunction() {
# function
}
Arguments can be given by $1, $2, and so on.
Argument parsing
For advanced parsing of command line arguments, getopts can be used
while getopts ":s" option; do
case "$option" in
s) size_flag=1
;;
?) printf "Error: Unknown option '-%s'.\n" "$OPTARG" >&2
exit
;;
esac
done
This will check for the command line argument -s, the : in front of the s suppresses all built-in errors, and since there is no : after the s, there is should be no value after
Upon fail this will output to Standard Out and output with an error, the "$OPTARG" is the variable of the flag and 2 is the standard error flag
Arguments can be shifted via shift [number]
shift "$((OPTIND-1))"
Shift up until the first available argument (removes all flags)