4

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"
1
  • Were any of the answers helpful?
    – jobin
    May 12, 2014 at 18:53

3 Answers 3

4

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
3
  • 2
    isn't the 1st check in the if obsolete? (how can varONE be '' if varTWO is not?)
    – Rinzwind
    Apr 21, 2014 at 20:16
  • @Rinzwind: varONE may be '' when no arguments have been provided and varTWO may be '' when only one argument is provided.
    – jobin
    Apr 21, 2014 at 20:17
  • @Rinzwind: Oh yes, not sure what I was thinking while I scripted this :/
    – jobin
    Apr 21, 2014 at 20:22
0

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...
2
  • How does this answer the question?
    – jobin
    Apr 21, 2014 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, 2014 at 18:48
0

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)"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .