0
0
Bash Scriptingscripting~10 mins

while loop in Bash Scripting - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print numbers from 1 to 5 using a while loop.

Bash Scripting
#!/bin/bash
count=1
while [ $count [1] 6 ]
do
  echo $count
  ((count++))
done
Drag options to blanks, or click blank then click option'
A-eq
B-gt
C-lt
D-ne
Attempts:
3 left
💡 Hint
Common Mistakes
Using -gt (greater than) will cause the loop not to run as expected.
Using -eq (equal) will only run if count equals 5, which is not the goal.
2fill in blank
medium

Complete the code to stop the loop when the user enters 'exit'.

Bash Scripting
#!/bin/bash
input=""
while [ "$input" != "exit" ]
do
  read -p "Type something (or 'exit' to quit): " [1]
done
Drag options to blanks, or click blank then click option'
Aanswer
Btext
Cline
Dinput
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name than the one in the condition causes the loop to never end.
Forgetting to quote the variable in the condition can cause errors.
3fill in blank
hard

Fix the error in the loop condition to correctly check if count is less than or equal to 10.

Bash Scripting
#!/bin/bash
count=1
while [ $count [1] 10 ]
do
  echo $count
  ((count++))
done
Drag options to blanks, or click blank then click option'
A-le
B-eq
C-lt
D-ge
Attempts:
3 left
💡 Hint
Common Mistakes
Using -lt will stop the loop before count reaches 10.
Using -ge or -eq will cause the loop to behave incorrectly.
4fill in blank
hard

Fill both blanks to create a loop that prints even numbers from 2 to 10.

Bash Scripting
#!/bin/bash
num=2
while [ $num [1] 10 ]
do
  echo $num
  num=$((num [2] 2))
done
Drag options to blanks, or click blank then click option'
A-le
B+
C-
D-lt
Attempts:
3 left
💡 Hint
Common Mistakes
Using -lt will exclude 10 from the output.
Subtracting 2 will cause an infinite loop.
5fill in blank
hard

Fill all three blanks to create a loop that counts down from 5 to 1 and prints each number.

Bash Scripting
#!/bin/bash
count=5
while [ $count [1] 0 ]
do
  echo [2]
  count=[3]
done
Drag options to blanks, or click blank then click option'
A-gt
B$count
C$((count - 1))
D-lt
Attempts:
3 left
💡 Hint
Common Mistakes
Using -lt will cause the loop to never run.
Not decreasing count will cause an infinite loop.