Using user input to determine how many times a for loop iterates

mike3759

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.

terdon

The problem is this line:

for number2 in {1..${loops}}

This is a question of precedence. As explained in man bash:

The order of expansions is: brace expansion; tilde expansion, parameter and variable expansion, arithmetic expansion, and command substitution (done in a left-to-right fashion); word splitting; and pathname expansion.

So, brace expansion happens before variable expansion. Therefore, when the braces are expanded, $loops has not been evaluated to 5. This means that {1..$loops} (you don't need the second {}) expands to the string {1..5} and not the sequence of numbers from 1 to 5. The same thing happens if you use a string instead of a variable:

$ echo {1..5}  ## works: prints a sequence
1 2 3 4 5
$ echo {1..foo} ## fails: prints a string with no expansion
{1..foo}

As a workaround, you can use seq:

for number2 in $(seq 1 "$loops"); do ...

Or, even better, the right loop for the job:

for((number2=0;number2<=$loops; number2++)); do ...


Note: As a general rule, you should also always quote your variables.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Loop iterates through array too many times Java

From Dev

JavaScript for loop iterates too many times through array

From Dev

how many times an number digit is shown on user integer input

From Dev

How to continually ask for user input for as many times as an int is holding?

From Dev

using a loop get a user to input an integer 10 times then get the minimum

From Dev

How to determine how many points a user has collected using aggregate

From Dev

How many times can a word be created using the input string?

From Dev

While loop in C iterates twice before asking for user input

From Dev

How many times this loop will run?

From Dev

Determine how many times dask computed something

From Dev

How to store user input in a variable using a For Loop?

From Dev

How to determine if user input is an number using a boolean function in C

From Dev

How do I let an user input how many times a richtextbox pastes a message?

From Dev

Find the largest number input by the user and identify how many times the largest number was inputted

From Dev

How to pass an pass user input into an array however many times they want and then print the data in PHP?

From Dev

How to run python function as many times as user input + read valid lines?

From Dev

how to count how many times a user logs in?

From Dev

how to count how many times a loop was executed?

From Dev

Outputs how many times you input

From Dev

how to cube a number many times depending on the input

From Dev

Using a loop to read a file and search for content, allow user to input multiple times to find a match

From Dev

Finding out how many times a user is processed?

From Dev

Counting how many times a loop runs

From Dev

How many times is this loop body repeated?

From Dev

How many times will the while loop be executed?

From Dev

How many times function executes inside for loop?

From Dev

How many times 'a' is printed? (includes fork in a loop)

From Dev

How to determine how many times an element is present in a list?

From Dev

How to determine how many times midnight is crossed during an Interval

Related Related

  1. 1

    Loop iterates through array too many times Java

  2. 2

    JavaScript for loop iterates too many times through array

  3. 3

    how many times an number digit is shown on user integer input

  4. 4

    How to continually ask for user input for as many times as an int is holding?

  5. 5

    using a loop get a user to input an integer 10 times then get the minimum

  6. 6

    How to determine how many points a user has collected using aggregate

  7. 7

    How many times can a word be created using the input string?

  8. 8

    While loop in C iterates twice before asking for user input

  9. 9

    How many times this loop will run?

  10. 10

    Determine how many times dask computed something

  11. 11

    How to store user input in a variable using a For Loop?

  12. 12

    How to determine if user input is an number using a boolean function in C

  13. 13

    How do I let an user input how many times a richtextbox pastes a message?

  14. 14

    Find the largest number input by the user and identify how many times the largest number was inputted

  15. 15

    How to pass an pass user input into an array however many times they want and then print the data in PHP?

  16. 16

    How to run python function as many times as user input + read valid lines?

  17. 17

    how to count how many times a user logs in?

  18. 18

    how to count how many times a loop was executed?

  19. 19

    Outputs how many times you input

  20. 20

    how to cube a number many times depending on the input

  21. 21

    Using a loop to read a file and search for content, allow user to input multiple times to find a match

  22. 22

    Finding out how many times a user is processed?

  23. 23

    Counting how many times a loop runs

  24. 24

    How many times is this loop body repeated?

  25. 25

    How many times will the while loop be executed?

  26. 26

    How many times function executes inside for loop?

  27. 27

    How many times 'a' is printed? (includes fork in a loop)

  28. 28

    How to determine how many times an element is present in a list?

  29. 29

    How to determine how many times midnight is crossed during an Interval

HotTag

Archive