Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: April 17, 2024
In Linux, while loops are essential tools for automating repetitive tasks and controlling the flow of programs. These loops enable us to execute a block of code repeatedly, provided that a specified condition is true. Moreover, while loops are versatile in that we can include user input as the specified condition, allowing a defined script to behave dynamically based on user input.
In this tutorial, we’ll take a look at using user input as a while loop condition in the Linux shell.
Before we dive into using user input, let’s first discuss the concept and syntax of a while loop:
while [condition]; do
# Code block
done
Let’s break down the syntax of a while loop:
As long as the while loop condition remains true, the code block within the loop is executed repeatedly. In contrast, the loop terminates if the condition evaluates to false.
In shell scripting, using user input as a condition for a while loop adds dynamic execution as it enables us to control the flow of the script based on their input.
To demonstrate, let’s utilize the read command to prompt the user:
$ read -p "message prompt" variable_name
Let’s analyze this general syntax:
This command reads a line of text from the standard input and saves it to the specified variable.
Now, we need to incorporate the captured user input in our loop condition to create an interactive experience. So, let’s first create the script.sh file and paste the content:
#!/bin/bash
read -p "Number of iterations: " iterations_count
counter=0
while [ $counter -lt $iterations_count ]
do
echo "Iteration: $counter"
((counter++))
done
echo "Loop completed."
Let’s discuss the instructions in the script.sh file:
Once the Bash script is saved, let’s execute it:
$ bash script.sh
Number of iterations: 4
Iteration: 0
Iteration: 1
Iteration: 2
Iteration: 3
Loop completed.
Here, we see that script.sh prompts the user to enter the number of iterations to run and then executes the while loop for that many iterations.
Once we capture the user input, we need to evaluate it to determine whether it meets the required criteria.
So, let’s modify the script.sh file:
#!/bin/bash
read -p "Number of iterations: " iterations_count
# Validate user input
if ! [[ $iterations_count =~ ^[0-9]+$ && $iterations_count -gt 0 ]]; then
echo "Invalid user input."
exit 1
fi
counter=0
...
Let’s break down this modification:
Above, if the user input iterations_count is not a positive integer, the script prints an error message:
$ bash script.sh
Enter the number of iterations: L
Error: Input must be a positive integer.
If the user input iterations_count is valid, the script continues execution.
In this article, we explored how to integrate user input into a while loop condition.
We learned that while loops provide flexibility when combined with user input. This is because this combination helps to create dynamic scripts that adapt to user preferences and requirements. Whether it’s managing loop iterations or terminating the execution of a script, making use of user input improves the usability of a script.