Here are the simple steps to create a bash script that takes a user-input number and print the first 10 multiples of given number using a for loop.
Create a new file as mentioned below.
vi multiples_script.sh
Copy and paste below simple Bash script that takes a user-input number and print the first 10 multiples of that number using a for loop:
#!/bin/bash # Prompt the user to enter a number read -p "Enter a number: " base_number # Check if the input is a valid number if [[ ! "$base_number" =~ ^[0-9]+$ ]]; then echo "Invalid input. Please enter a valid number." exit 1 fi # Use a for loop to print the first 10 multiples of the entered number echo "Multiples of $base_number:" for (( i = 1; i <= 10; i++ )); do result=$((base_number * i)) echo "$base_number x $i = $result" done
Here’s a breakdown of the script:
1. read -p “Enter a number: ” base_number: Prompts the user to enter a number and stores it in the variable base_number.
2. if [[ ! “$base_number” =~ ^[0-9]+$ ]]; then: Checks if the entered value is a valid number using a regular expression.
[[ ! “$base_number” =~ ^[0-9]+$ ]]: This is a conditional expression using the [[ … ]] construct, which is an enhanced version of the [ … ] test construct in Bash. It is used for more complex conditional expressions.
!: The exclamation mark ! is a logical NOT operator. It negates the result of the condition. So, ! “$base_number” checks if the value stored in the variable $base_number is not a number.
“$base_number”: This is the value of the variable base_number. The double quotes around it are used to handle cases where the variable contains spaces or other special characters.
=~: This is the regex matching operator. It checks whether the value on the left side matches the regular expression on the right side.
^[0-9]+$: This is a regular expression that asserts that the entire string consists only of digits (0-9).
^: Anchors the regex at the beginning of the string.
[0-9]+: Matches one or more digits.
$: Anchors the regex at the end of the string.
Putting it all together, the conditional expression checks if the value in $base_number is not a sequence of one or more digits. If it’s not a valid number, the condition is true, and the code block inside the if statement is executed.
echo “Invalid input. Please enter a valid number.”: If the condition is true, this line prints an error message indicating that the input is invalid.
exit 1: This line terminates the script with an exit status of 1, indicating an error. Exiting with a non-zero status is a convention to signal that an error occurred.
fi: This marks the end of the if statement.
In summary, this code block checks if the input provided in $base_number is not a valid number, and if so, it prints an error message and exits the script with an error status.
3. echo “Invalid input. Please enter a valid number.”: Prints an error message and exits if the input is not a valid number.
4. echo “Multiples of $base_number:”: Prints a message indicating that the following lines will show the multiples of the entered number.
5. for (( i = 1; i <= 10; i++ )); do: Initiates a for loop that iterates from 1 to 10.
6. result=$((base_number * i)): Calculates the multiple of the base number for the current iteration.
7. echo “$base_number x $i = $result”: Prints the multiplication expression and the result.
8. done: Marks the end of the for loop.
To run the script, make it executable:
chmod +x multiples_script.sh
And then execute it:
./multiples_script.sh
Enter a number when prompted, and the script will display the first 10 multiples of that number.
How do you feel about this post? Drop your comments below..