A question about script

ksix

New Member
Joined
Aug 13, 2018
Messages
1
Reaction score
0
Credits
0
hello,
I am new to Linux.I have encountered some questions when i wrote scripts.The loop can`t be executed,but i don`t know how to solve it.Please help me.Thanks very much!
Here is my code:
Code:
  1 #!/bin/bash
  2 read -p "Enter your number:"  num
  3 read -a number
  4 echo "your numbers are:"
  5 for i in "${number[num]}"
  6 do
  7     echo "$i "
  8 done
  9 echo " you want to input $num numbers,but in fact ,you just input $a "
 
Last edited:


wizardfromoz

Administrator
Staff member
Gold Supporter
Joined
Apr 30, 2017
Messages
7,204
Reaction score
6,115
Credits
24,183
G'day @ksix and welcome to linux.org :)

I am moving your Thread to Command Line, where it will hopefully attract more experienced help from a wider audience.

Good luck!

Chris Turner
wizardfromoz
 

JasKinasis

Well-Known Member
Joined
Apr 25, 2017
Messages
1,464
Reaction score
2,082
Credits
10,249
hello,
I am new to Linux.I have encountered some questions when i wrote scripts.The loop can`t be executed,but i don`t know how to solve it.Please help me.Thanks very much!
Here is my code:
Code:
  1 #!/bin/bash
  2 read -p "Enter your number:"  num
  3 read -a number
  4 echo "your numbers are:"
  5 for i in "${number[num]}"
  6 do
  7     echo "$i "
  8 done
  9 echo " you want to input $num numbers,but in fact ,you just input $a "

What is your script supposed to be doing?
I assume the first number is supposed to be the number of numbers to enter? And then you are trying to store the numbers in an array and then display them on screen?

You could do it in a single readline, without asking for the number of numbers by doing this:
Code:
#!/usr/bin/env bash

read -p "Enter your numbers:" -a numbers
for num in ${numbers[@]}
do
   if [[ $num =~ ^[0-9]+$ ]]
   then
       echo $num
   fi
done

What that does is allow you to enter numbers in a single line. Storing each word of the user input (separated by spaces) in an array called numbers. The section which outputs the numbers uses a simple regex that checks that all characters in the word are numeric and only outputs the numeric members of the array.

e.g.
if you entered:
1 2 3 4 5

That would yield the output:
1
2
3
4
5

The if statement in the code just checks that each word that was entered is completely numeric.
So if you entered the following:
1 2 3rd 4 5tre453 6 words will be ignored 7
The output would be:
1
2
4
6
7

The invalid numbers "3rd" and "5tre453" and the words "words will be ignored" will be ignored.

That's one way of doing it.

To do it your original way, asking the user for the number of numbers AND using some error checking to ensure that the user enters valid numbers each time. You could use something like this:
Code:
#!/usr/bin/env bash

# storage for the last good user-entered number
lastgoodnumber=""

# Get a number from the user and store it in lastgoodnumber
# endlessly prompts the user until a number is entered.
# If a parameter is passed, it is used as the prompt message for read
function readnumber
{
   msg="Enter a number"
   # if we received a parameter, then use that as the prompt msg
   if [[ $# -eq 1 ]]; then
       msg="$1"
   fi
    usernumber=""
   while [[ ! $usernumber =~ ^[0-9]+$ ]]; do
       read -p "$msg : " usernumber
       if [[ ! $usernumber =~ ^[0-9]+$ ]]; then
           echo "That was not a number - try again!"
       fi
   done
   lastgoodnumber="$usernumber"
}

# Get the number of numbers the user wishes to enter
readnumber "Enter number of numbers"
numberofnumbers="$lastgoodnumber"

# Get each number and store it in an array
numberarray=()
for i in $(eval echo "{1..$numberofnumbers}")
do
    readnumber
   numberarray+=(${lastgoodnumber})
done

# Display the numbers that were entered by the user
echo "The $numberofnumbers numbers entered by the user were :"
for number in ${numberarray[@]}
do
   echo $number
done

NOTE - The above example will only accept positive integers. If you want to expand it to allow floating point numbers or to allow negative numbers - you would have to update the regex in the error checks inside the function to check for additional conditions.
At the moment, it checks that each line of input entered by the user is entirely numerical.
 
Last edited:
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Staff online

Members online


Top