Chapter 5: Bash Scripting Flashcards
What do bash scripts begin with?
!/bin/bash
How do we execute a script called “Hello-World.sh”?
./Hello-World.sh
How do we make the script executable?
chmod -x script.sh
How do we set a variable?
first_name=good
How do we call the variable?
$first_name
What is wrong with the follow command? good=Hello World
There is a space between Hello and World which causes the command to declare a variable and process World as a separate command. This can be remedied with single or double quotes.
What is the difference between single and double quotes in bash scripting?
Single quotes processes every character literally while double quotes processes every character except for $ ` and . Without quotes, spaces function as command delimiters.
What is command substitution?
Command substitution is setting the value of a variable to be the output of a certain program or command.
How do we use command substitution? Use an example.
An example would be user=$(whoami)
The command must be surrounded by brackets and preceded outside the brackets with a dollar sign.
How do we write the first and second arguments within a bash script?
$1 and $2
How many arguments can be put into a bash script?
- Using $1 … $8 $9
Which command allows us to capture user input? Use an example.
The read command. The word proceeding it is turned into a variable later on in the script and can be called upon.
Example:
read answer
echo “Your answer was $answer”
Within the read command, how do we specify a prompt?
-p
Within the read command, how do we specify a prompt and make the input silent as the user is typing it in?
-sp
Explain the if statement. Give an example.
The if statement checks to see if a condition is true. The syntax needs to be precise. For example:
if [ ]
then
fi
What are the relevance of the two square brackets?
These allow us to utilise operators within the test command. We do not need to use square brackets.
What is the general syntax for the else statement?
if
then
else
Explain the elif statement and how it works?
elif serves as a second if. For example, if the condition for if to fire is not met, then a second condition test is undertaken using elif. This can happen repeatedly, in other words you can use multiple elif statements constantly testing for conditions until one fires.
What are the two bash boolean operators?
&& is AND and || is OR.
Explain the AND operator. When will it execute?
The AND operator will only execute if the previous command was successful.
For example: if a grep command failed to executed, the AND operator won’t execute the command after it. The first command must succeed.
Explain the OR operator. When will it execute?
The OR operator is the opposite to the AND operator. It will execute only if the previous command failed.
Explain loops. What are they used for?
Loops help us with repetitive tasks that we need to run until a certain criteria is met.
Explain for loops. What are they used for?
for loops are used for completing a set of commands on items within a list.
What is an example of a for loop?
for ip in $(seq 1 10); do echo 10.11.1.$ip; done
Explain while loops. When do the execute?
while loops need some condition to be true in order to execute. they are similar to if in this respect.
What is an example of a while loop?
while [ ]
do
done
Explain the double parenthesises construct. What does it do?
It performs arithmetic expansion and evaluation at the same time. In the counter=1 example in the course material - it is increasing the variable by 1.
What is a function?
In terms of bash scripting, we can think of functions as a script within a script.
You could also think of a function as a subroutine or a blackbox - performing an operation.
How do you write a function? Use an example.
function_name () {
echo “Hi”
}
function_name
What is the difference between a local variable and a global variable?
A global variable can be accessed throughout the entire script while a local variable is only accessible within the function.