The opening syntax of a shell script is #!/bin/bash, which indicates that it is to be run in the bash shell.
The script should typically have a .sh extension, and will need to be made executable using the chmod command (eg chmod a+x noobot.sh)
Commands can be declared as variables by encasing them in quotes or $() notation.
variable1 = 'date' #sets variable1 to the date command variable2 = $(date) #also sets variable2 to the date command
Executes another program; often found at the end of wrapper scripts. Replaces the current process; where no other program is defined, a new shell process is created.
exec > mrnoobot.txt #output from all subsequent commands will be redirected to a text file until the exec session is halted using the exit command.
for, in, do, done
for is used to iterate over a list and perform the code defined in the do/done block on each item
for name in Bob Sue Jane Eric do echo "Hello $name!" done #displays 'Hello ' with persons name for each person
if, then, elif, else fi
if opens a conditional statement. then defines what happens if the condition is met; else defines what happens if it is not. elif is used to add secondary conditions. fi closes the conditional.
if [ $USER = 'mrnoobot' ] then echo 'Hi mrnoobot!' elif [ $USER = 'mrsnoobot' ] then echo 'Hello Mrs Noobot!' else echo 'Hmm. You don't seem familiar.' fi
Input validation can be performed using grep
read -p 'Enter a number: ' number #prompts user to enter a number if echo $number | grep -E '^[0-9]+$' > /dev/null 2> /dev/null #if the command completes, the conditional will be True then echo 'Thank you for entering a number.' else echo 'That's not a number.' fi
Can be used to send information to a superuser.
echo 'Have a great day!' | mail mrnoobot
Read prompts the user for input via a flashing cursor and stores the result to a variable.
read noobot #stores user input to the variable noobot read -p 'What's your name? ' noobot #the -p option provides a text prompt
Used to generate integer lists, similar to range in Python.
#Syntax seq start increment stop #increment is optional and can be negative (decrement) seq 0 2 20 #display all even numbers from 0 to 20 INCLUSIVE
Checks for a condition, returning True or False. Can be called using  square brackets but must be padded with spaces. Commonly found in IF statements.
#Syntax test option soloargument test arg1 option arg2 #Examples test -n string test 0 -eq 4 Options -z #True if length of string is zero -n #true if length of string is NOT zero = #True if strings are equal != #True if strings are NOT equal -eq #True if integers are equal -ne #True if integers are NOT equal -gt #True if int1 > int2 -ge #True if int1 >= int2 -lt #True if int1 < int2 -le #True if int1 <= int2 -d #True if file is directory -f #True if file is simple file -e #True if file exists -r #True if file has read permission for current user -w #True if file has write permission for current user -x #True if file has execute permission for current user -a #AND to combine two conditions -o #OR to combine two conditions ! #checks if a condition is False if test $var -eq 999 #equates to if [ $var -eq 999 ] #note the whitespace padding!
while, do, done
while can be used to repeatedly check the state of a conditional and execute code accordingly, via the do/done code block
read -p 'Enter a number: ' number #prompts user to enter a number while echo $number | grep -v - E '^[0-9]+$' > /dev/null 2> /dev/null #the -v grep option inverts the search, so this while statement will only be True if $number is NOT a number do echo 'That's not a number. Please try again.' read -p 'Enter a number: ' number done echo 'Thank you for correctly entering a number.'
A wrapper script is used to configure and launch another program.