Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a quick question relating to $# in the shell scripts.

So, I can use $# at the command line to check the number of a shell script arguments given by the user.

Example:

#  ./shellScript varONE varTWO
#  $#
>  2  <--  so output 2 here reflects varONE and varTWO

Is any command I can use for read command?

For example:

#!/bin/bash
read -p "please input value" var1 var2
echo $#  <--  however here will return 0 even user input two values at command line

Is any other command I can use here to return 2 so that I can perform a check on the user's input variables? Like:

[ $# -lt 2 ] && echo "failed"
share|improve this question
    
Were any of the answers helpful? –  i08in May 12 at 18:53

3 Answers 3

The best way to check if you have received a value in your varTWO is to check if varTWO is empty as follows:

read varONE varTWO
if [[ $varTWO = '' ]]; then
    echo "failed"
fi
share|improve this answer
    
WOW THANK YOU SO MUCH!!!! –  user195661 Apr 21 at 18:32
2  
isn't the 1st check in the if obsolete? (how can varONE be '' if varTWO is not?) –  Rinzwind Apr 21 at 20:16
    
@Rinzwind: varONE may be '' when no arguments have been provided and varTWO may be '' when only one argument is provided. –  i08in Apr 21 at 20:17
    
@Rinzwind: Oh yes, not sure what I was thinking while I scripted this :/ –  i08in Apr 21 at 20:22
1  
@user195661 If this answered your question, you may mark this answer as accepted, by clicking the green check mark next to it. That will indicate that your problem is solved. –  Radu Rădeanu Apr 21 at 20:26

Special Shell variable $# return the number of positional arguments given to this invocation of the shell.

if you want the arguments passed to the script are exactly 2:

if [ $# -ne 2 ]; then
    echo "failed" 
fi

#check if args are correct here...
share|improve this answer
    
How does this answer the question? –  i08in Apr 21 at 18:44
    
check if the number of arguments, there is no need to check if they are void. argument passing is positional, so if you have 2 are necessarily valid. –  girardengo Apr 21 at 18:48

The short answer is that you can't. You need to tell read how many arguments it should expect, it will only read as much as you tell it to. If you give it just one variable name, it will read everything into that variable. If you give it more than one, for example 3, it will read everything until the first space (or tab, or newline or anything else you have set in $IFS) into the 1st, everything to the next into the second and the rest of the line into the third. To illustrate:

$ cat file
one two three four five
$ while read line; do echo "$line"; done < file
one two three four five
$ while read first second rest; do echo "First: $first, Second:$second Rest:$rest"; done < file
First: one, Second:two Rest:three four five

So, read will read as many variables as you give it. If you can have variable length input, you will either need to test each to find out how many were passed as explained in @Jobim's answer, or you can use arrays.

As explained in help read:

Options:

-a array        assign the words read to sequential indices of the array variable ARRAY, starting at zero

This means that you can store the input values in an array and then get the length of the array with ${#ArrayName[@]}:

#!/usr/bin/env bash

read -a values -p "please input values: " 
echo "You gave ${#values[@]} value(s)"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.