0
0
Bash Scriptingscripting~5 mins

if-then-fi structure in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Afi
Bend
Cdone
Dstop
Which of these is the correct way to start an if statement in bash?
Aif ( condition ) then
Bif [ condition ]; then
Cif { condition } then
Dif condition then
What happens if the condition in an if statement is false?
AThe commands inside <code>then</code> run
BThe script restarts
CThe script stops immediately
DThe commands inside <code>then</code> do not run
Which symbol is used to test conditions inside the if statement?
A[]
B()
C{}
D<>
How do you write an if statement that checks if a variable x equals 5?
Aif [ $x == 5 ]; then
Bif ( $x = 5 ) then
Cif [ $x = 5 ]; then
Dif $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.