I am having difficulty getting a for loop to take user input in the form of an argument to a function I created.
#!/bin/bash
number1="$1" #Assume user input 5
loops="$2" #Assume user input 5
if [[ ${number1} && ${loops} =~ ^-?[0.00-9.99]+$ ]];then
for number2 in {1..${loops}}
do
Product=$(echo "${number1} * ${number2}" | bc -l)
echo "${number1} * ${number2} = ${Product}"
if test ${number2} -eq 7;then
{ echo "----------------------"; echo "${number2} is the best number."; echo "----------------------"; continue; }
elif test ${number2} -eq 11;then
{ echo "------------------------------"; echo "${number2} is pretty awesome as well."; echo "------------------------------"; continue; }
fi
done
else
{ echo "Please try again with a valid numerical entry."; exit 1; }
fi
This script does not run when the user inputs an integer for the variable "loops", and any number for "number1". number1 = 5 in this case. It returns the error:
(standard_in) 1: syntax error
5 * {1..5} =
/home/$USER/For_Loop_Multiplication.sh: line 9: test: {1..5}: integer expression expected
/home/$USER/For_Loop_Multiplication.sh: line 11: test: {1..5}: integer expression expected
If I instead directly input an end bound on the number of loops directly into the script like the following, and number1 = 5, it runs fine:
#!/bin/bash
number1="$1" #Assume user input 5
if [[ ${number1} =~ ^-?[0.00-9.99]+$ ]];then
for number2 in {1..5}
do
Product=$(echo "${number1} * ${number2}" | bc -l)
echo "${number1} * ${number2} = ${Product}"
if test ${number2} -eq 7;then
{ echo "----------------------"; echo "${number2} is the best number."; echo "----------------------"; continue; }
elif test ${number2} -eq 11;then
{ echo "------------------------------"; echo "${number2} is pretty awesome as well."; echo "------------------------------"; continue; }
fi
done
else
{ echo "Please try again with a valid numerical entry."; exit 1; }
fi
It returns the result:
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
Essentially what I think is happening is that the script seems to be placing the entire range as the value number2, which is defined as the iterator in the for loop. Since the input for the variable "loops" is an integer, there should be no difference between asking the user for a range and placing it in the loop, or simply setting it as a default when I write the script. However, there apparently is a difference... Any insight into solving this problem is appreciated. Thanks.
[[ ${number1} && ${loops} =~ ^-?[0.00-9.99]+$ ]]
means: if${number1}
expands to anything other than an empty string and${loops}
matches an optional dash followed by one or more characters matching a dot or a digit in the range0-9
, then execute the block below.$number1
will always expand to something as long as an argument was passed,not sure if that's intended, but the regex is wrong. You should check if${loops}
is in the0.00-9.99
range in another way, there's no easy way to do that using regexes.[0-9](.[0-9][0-9]?)?
should match a number in that range, didn't test though). I.e.0.00-0.99
is not a valid range, you can only specify ranges of integers (0-1
,2-3
,4-5
,0-5
and so on).[0.00-9.99]
means: match a0
digit or a dot or a0
digit or a digit in the range0-9
or a dot or a9
digit or a9
digit (which sums up to: match a digit or a dot).