0
0
Bash Scriptingscripting~5 mins

Infinite loops in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an infinite loop in bash scripting?
An infinite loop is a loop that never stops running because its exit condition is never met or it has no exit condition.
Click to reveal answer
beginner
How do you write a simple infinite loop using while in bash?
You can write it as:
while true; do
  # commands
 done
This runs the commands forever until you stop it.
Click to reveal answer
beginner
What command can you use to stop an infinite loop running in the terminal?
Press Ctrl + C to send an interrupt signal that stops the running infinite loop.
Click to reveal answer
intermediate
Why might infinite loops be useful in scripting?
They are useful for tasks that need to run continuously, like monitoring a system or waiting for events.
Click to reveal answer
intermediate
Show an example of an infinite loop using for in bash.
Example:
for (( ; ; )); do
  echo "Looping forever"
  sleep 1
 done
This loops forever because there are no conditions.
Click to reveal answer
Which of these is a correct way to write an infinite loop in bash?
Awhile true; do echo "Hi"; done
Bwhile false; do echo "Hi"; done
Cfor i in 1..10; do echo "Hi"; done
Dif true; then echo "Hi"; fi
How do you stop a running infinite loop in the terminal?
APress Ctrl + C
BPress Ctrl + Z
CClose the terminal window
DType 'exit' inside the loop
What will this loop do?
for (( ; ; )); do echo "Hello"; done
APrint 'Hello' once
BPrint 'Hello' forever
CCause an error
DPrint 'Hello' 10 times
Why should you be careful when writing infinite loops?
AThey always run faster than normal loops
BThey automatically stop after 10 minutes
CThey use less memory
DThey can crash your system if not controlled
Which command inside an infinite loop can help avoid high CPU usage?
Als
Becho "Running"
Csleep 1
Dexit
Explain what an infinite loop is and how you can create one in bash.
Think about loops that never stop running.
You got /3 concepts.
    Describe how to safely stop an infinite loop running in your terminal and why you might want to use infinite loops.
    Consider both stopping and using infinite loops.
    You got /3 concepts.