Chapter 5: Bash Scripting Flashcards

1
Q

What do bash scripts begin with?

A

!/bin/bash

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do we execute a script called “Hello-World.sh”?

A

./Hello-World.sh

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do we make the script executable?

A

chmod -x script.sh

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do we set a variable?

A

first_name=good

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do we call the variable?

A

$first_name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is wrong with the follow command? good=Hello World

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the difference between single and double quotes in bash scripting?

A

Single quotes processes every character literally while double quotes processes every character except for $ ` and . Without quotes, spaces function as command delimiters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is command substitution?

A

Command substitution is setting the value of a variable to be the output of a certain program or command.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do we use command substitution? Use an example.

A

An example would be user=$(whoami)

The command must be surrounded by brackets and preceded outside the brackets with a dollar sign.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do we write the first and second arguments within a bash script?

A

$1 and $2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How many arguments can be put into a bash script?

A
  1. Using $1 … $8 $9
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Which command allows us to capture user input? Use an example.

A

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”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Within the read command, how do we specify a prompt?

A

-p

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Within the read command, how do we specify a prompt and make the input silent as the user is typing it in?

A

-sp

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Explain the if statement. Give an example.

A

The if statement checks to see if a condition is true. The syntax needs to be precise. For example:

if [ ]
then

fi

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the relevance of the two square brackets?

A

These allow us to utilise operators within the test command. We do not need to use square brackets.

17
Q

What is the general syntax for the else statement?

A

if
then
else

18
Q

Explain the elif statement and how it works?

A

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.

19
Q

What are the two bash boolean operators?

A

&& is AND and || is OR.

20
Q

Explain the AND operator. When will it execute?

A

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.

21
Q

Explain the OR operator. When will it execute?

A

The OR operator is the opposite to the AND operator. It will execute only if the previous command failed.

22
Q

Explain loops. What are they used for?

A

Loops help us with repetitive tasks that we need to run until a certain criteria is met.

23
Q

Explain for loops. What are they used for?

A

for loops are used for completing a set of commands on items within a list.

24
Q

What is an example of a for loop?

A

for ip in $(seq 1 10); do echo 10.11.1.$ip; done

25
Q

Explain while loops. When do the execute?

A

while loops need some condition to be true in order to execute. they are similar to if in this respect.

26
Q

What is an example of a while loop?

A

while [ ]
do

done

27
Q

Explain the double parenthesises construct. What does it do?

A

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.

28
Q

What is a function?

A

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.

29
Q

How do you write a function? Use an example.

A

function_name () {
echo “Hi”
}

function_name

30
Q

What is the difference between a local variable and a global variable?

A

A global variable can be accessed throughout the entire script while a local variable is only accessible within the function.