Recall & Review
beginner
What is the purpose of the
if-then-fi structure in bash scripting?It allows the script to make decisions by running commands only if a certain condition is true.
Click to reveal answer
beginner
Write the basic syntax of an
if-then-fi statement in bash.if [ condition ]; then<br> commands<br>fi
Click to reveal answer
beginner
In bash scripting, what does the
fi keyword do?It marks the end of the
if block, telling the shell that the conditional commands are finished.Click to reveal answer
intermediate
How do you check if a file exists using
if-then-fi in bash?if [ -e filename ]; then<br> echo "File exists"<br>fi
Click to reveal answer
beginner
Why is it important to have spaces around the square brackets in
if [ condition ]?Because
[ is a command and needs spaces to separate it from the condition and brackets, otherwise the shell will give an error.Click to reveal answer
What keyword ends an
if block in bash scripting?✗ Incorrect
The
fi keyword is used to close an if block in bash.Which of these is the correct way to start an
if statement in bash?✗ Incorrect
The correct syntax uses square brackets with spaces and a semicolon before
then: if [ condition ]; then.What happens if the condition in an
if statement is false?✗ Incorrect
If the condition is false, the commands inside the
then block are skipped.Which symbol is used to test conditions inside the
if statement?✗ Incorrect
Square brackets
[] are used to test conditions in bash if statements.How do you write an
if statement that checks if a variable x equals 5?✗ Incorrect
In bash, use single equals
= inside square brackets with spaces: if [ $x = 5 ]; then.Explain how the
if-then-fi structure works in bash scripting.Think about how you decide to do something only if a condition is met.
You got /3 concepts.
Write a simple bash script using
if-then-fi to check if a file named 'data.txt' exists and print a message.Use the -e option inside the condition to check for file existence.
You got /4 concepts.