Recall & Review
beginner
What is the basic syntax of an if-then-else statement in Bash?
The basic syntax is:<br>
if [ condition ]; then<br> commands<br>else<br> other_commands<br>fi<br>This checks the condition; if true, runs commands, else runs other_commands.
Click to reveal answer
beginner
How do you check if a file exists using if-then-else in Bash?
Use the test command with -e:<br>
if [ -e filename ]; then<br> echo "File exists"<br>else<br> echo "File does not exist"<br>fi
Click to reveal answer
beginner
What does the 'fi' keyword do in a Bash if-then-else statement?
'fi' marks the end of the if statement. It is 'if' spelled backward and tells Bash where the if block finishes.
Click to reveal answer
beginner
Can you use multiple commands inside the then or else blocks? How?
Yes, you can put many commands inside then or else blocks by writing them on separate lines:<br>
if [ condition ]; then<br> command1<br> command2<br>else<br> command3<br> command4<br>fi
Click to reveal answer
beginner
What happens if the condition in an if statement is false and there is no else block?
If the condition is false and no else block exists, Bash simply skips the then block and continues running the script after fi.
Click to reveal answer
Which keyword ends an if-then-else block in Bash?
✗ Incorrect
The keyword 'fi' is used to close an if statement in Bash.
What symbol is used to test a condition in an if statement?
✗ Incorrect
Square brackets [] are used to test conditions in Bash if statements.
How do you write an else block in Bash?
✗ Incorrect
In Bash, 'else' is followed by commands on the next lines before 'fi'.
What will this script print if the file 'test.txt' does not exist?<br>if [ -e test.txt ]; then echo "Found"; else echo "Not found"; fi
✗ Incorrect
If the file does not exist, the else block runs and prints 'Not found'.
Can you put multiple commands inside the then block?
✗ Incorrect
Multiple commands can be placed inside then block separated by new lines or semicolons.
Explain how an if-then-else statement works in Bash with an example.
Think about checking a condition and running different commands based on true or false.
You got /6 concepts.
Describe how to check if a file exists using if-then-else in Bash and what happens if the file does not exist.
Use the test command inside the if condition.
You got /5 concepts.