Noob question: How to check the total number of inputs entered by user?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Noob question: How to check the total number of inputs entered by user?
# 1  
Old 12-12-2019
Noob question: How to check the total number of inputs entered by user?

Say I have this line:
Code:
read -p "Enter 3 numbers: " num1 num2 num3;

I want to write a while loop that repeatedly asks for input if the number of inputs entered is not equal to 3.
I don't know the correct command to find the number of inputs entered. Help, please?
# 2  
Old 12-12-2019
Welcome on board,

I think the beginning would be to understand the line of code you posted, first...
what will read do? Why read -p?
# 3  
Old 12-12-2019
I'm using the
Code:
read

command to read 3 inputs, namely num1, num2, and num3. I want to write a while loop that repeatedly asks for the correct number of inputs, which is 3.

Here's the pseudocode:
read user input here (must be 3)
while the number of inputs entered is less than or greater than 3, ask again for input.
Once it exits the while loop, do the rest of the script's purpose.

Last edited by rbatte1; 12-12-2019 at 10:10 AM..
# 4  
Old 12-12-2019
Hi jejemonx...
This is what vbe was referring to assuming you are using 'bash':
Code:
Last login: Thu Dec 12 13:15:18 on ttys000
AMIGA:amiga~> bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.
AMIGA:amiga~> 
AMIGA:amiga~> 
AMIGA:amiga~> help read
read: read [-ers] [-u fd] [-t timeout] [-p prompt] [-a array] [-n nchars] [-d delim] [name ...]
    One line is read from the standard input, or from file descriptor FD if the
    -u option is supplied, and the first word is assigned to the first NAME,
    the second word to the second NAME, and so on, with leftover words assigned
    to the last NAME.  Only the characters found in $IFS are recognized as word
    delimiters.  If no NAMEs are supplied, the line read is stored in the REPLY
    variable.  If the -r option is given, this signifies `raw' input, and
    backslash escaping is disabled.  The -d option causes read to continue
    until the first character of DELIM is read, rather than newline.  If the -p
    option is supplied, the string PROMPT is output without a trailing newline
    before attempting to read.  If -a is supplied, the words read are assigned
    to sequential indices of ARRAY, starting at zero.  If -e is supplied and
    the shell is interactive, readline is used to obtain the line.  If -n is
    supplied with a non-zero NCHARS argument, read returns after NCHARS
    characters have been read.  The -s option causes input coming from a
    terminal to not be echoed.
    
    The -t option causes read to time out and return failure if a complete line
    of input is not read within TIMEOUT seconds.  If the TMOUT variable is set,
    its value is the default timeout.  The return code is zero, unless end-of-file
    is encountered, read times out, or an invalid file descriptor is supplied as
    the argument to -u.
readonly: readonly [-af] [name[=value] ...] or readonly -p
    The given NAMEs are marked readonly and the values of these NAMEs may
    not be changed by subsequent assignment.  If the -f option is given,
    then functions corresponding to the NAMEs are so marked.  If no
    arguments are given, or if `-p' is given, a list of all readonly names
    is printed.  The `-a' option means to treat each NAME as
    an array variable.  An argument of `--' disables further option
    processing.
AMIGA:amiga~> _

AND, an example of how it works:
Code:
Last login: Thu Dec 12 13:15:49 on ttys000
AMIGA:amiga~> read a b c
1
AMIGA:amiga~> echo "${a} ${b} ${c}"
1  
AMIGA:amiga~> read a b c
1 2
AMIGA:amiga~> echo "${a} ${b} ${c}"
1 2 
AMIGA:amiga~> read a b c
1 2 3
AMIGA:amiga~> echo "${a} ${b} ${c}"
1 2 3
AMIGA:amiga~> _

As you can see whether you input variables 'b' and 'c' or not they have 'NULL' values so there will always be 3 variables.
You could always use 3 separate 'read's and check each one for validity and restart the loop as required if one or more is not a number...
# 5  
Old 12-12-2019
So your one-liner will not help you if you have more than 3, as it will not take account or will all be in num3, and if someone enters:
1,23,45 you are stuck too...
If you continue with your one-liner read, you will need num4 to be able to check more than 3 numbers were entered, after it does not matter...and you will have to check if you have a valid value in num 1-3 as what is stopping someone to enter "A"?
And while you don't have your 3 numerics in num1-3, you reinitialize your variable to 0 and start again...
---
wisecracker has also shown you what I had in mind and his suggestion solves partially your issue, only this more easy design looks like it is not an option here, as this seems more to be homework to see how smart you are to sort yourself out of this tricky issue when using in such manner the read command...
And so unless you can justify this is not homework and as such should have been posted in the adequate room and following the special rules there I will close this thread

Last edited by vbe; 12-12-2019 at 11:59 AM.. Reason: typos
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Total Noob BASH scripting question

Hello All, I have a file of ip addresses called activeips.txt What I'm trying to do is run a simple bash script that has a loop in it. The loop is a cat of the IP addresses in the file. The goal is to run 2 nmap commands to give me outputs where each address in the list has an OS... (11 Replies)
Discussion started by: Dirk_Pitt
11 Replies

2. Shell Programming and Scripting

How to check user entered correct file format or not?

Hi Experts, path=/db/files/ format=$1 User can enter any file format.compare the user file format with actual file format existed in the directory /db/files. User enter all characters as "A" apart from date format. example1: user will be entering the file format AAA_AA_YYYYMMDD.AAA Actual... (6 Replies)
Discussion started by: nalu
6 Replies

3. Shell Programming and Scripting

Print Unknown Number of User Inputs in awk

Hello, I am new to awk and I am trying to figure out how to print an output based on user input. For example: ubuntu:~/scripts$ steps="step1, step2, step3" ubuntu:~/scripts$ echo $steps step1, step2, step3 I am playing around and I got this pattern that I want: ... (3 Replies)
Discussion started by: tattoostreet
3 Replies

4. Shell Programming and Scripting

How to accept command line argument as character or text if number is entered?

Hello Does the unix korn shell provide a function to convert number entered in command line argument to text or Character so that in next step i will convert Chr to Hex (6 Replies)
Discussion started by: aadityapatel198
6 Replies

5. Shell Programming and Scripting

Help with sum total number of record and total number of record problem asking

Input file SFSQW 5192.56 HNRNPK 611.486 QEQW 1202.15 ASDR 568.627 QWET 6382.11 SFSQW 4386.3 HNRNPK 100 SFSQW 500 Desired output file SFSQW 10078.86 3 QWET 6382.11 1 QEQW 1202.15 1 HNRNPK 711.49 2 ASDR 568.63 1 The way I tried: (2 Replies)
Discussion started by: patrick87
2 Replies

6. Shell Programming and Scripting

only a number can be entered no letters?

ok the user can only enter a number if a letter is entered it shouldnt be accepted This is what i have so far read -p "How many cars to enter:" cars until do read -p "Invalid number. Please re-enter:" $tags done (5 Replies)
Discussion started by: gangsta
5 Replies

7. Shell Programming and Scripting

Total noob script

Hi i am a total noob at shell scripting. i was wondering if somebody could help me with my script. i want the script to search the dev folder for the burner file because they are different between distrubutions? as i under stand it. this i the script. #!/bin/bash echo "Script för att bränna 360... (4 Replies)
Discussion started by: MatsO
4 Replies

8. High Performance Computing

PBS - restrict total number of processors per user

Hello everyone! I am a bit inexperienced with administering queueing programs. I installed Torque (a PBS derivative) on a Linux cluster and it is running well. There is one annoying problem though: users can run massively parallel jobs and serial jobs too. Almost all users do a mix of the two. I... (0 Replies)
Discussion started by: gnuplot
0 Replies

9. Shell Programming and Scripting

validating user entered date

I need the date validation. I searched in the google but i didn't find my requirements. requirements: 1) user has to enter the date in YYYY/MM/DD format 2) MM validations 3) DD validations. and if the month is april it should allow 30 days only and for May month it should allow 31 days like... (1 Reply)
Discussion started by: KiranKumarKarre
1 Replies

10. Shell Programming and Scripting

Bash : how do i check the user input and make sure is only chracter or only number ?

Bash : how do i check the user input and make sure is only character or only number ? (7 Replies)
Discussion started by: CheeSen
7 Replies
Login or Register to Ask a Question