Find the answer to your Linux question:
Results 1 to 10 of 10
Hi all. If I read in variables entered by the user, how can I check to make sure the correct number of variables were entered? For example, after reading in ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2010
    Location
    England
    Posts
    5

    [SOLVED] Counting variables in bash script


    Hi all.

    If I read in variables entered by the user, how can I check to make sure the correct number of variables were entered? For example, after reading in a data file and making it into an array, I have:

    echo "To check the data, enter the first element number, last element number and step size as x y z:"
    read x y z

    It then goes on to start a loop, but what I would like now (before the loop) is a check to see if three variables have been entered, before the rest of the script continues.

    I've tried specifying the variables as $1, $2 and $3, but if I echo $#, the value comes out as zero, so it's obviously not working.

    Can anyone please offer any help?

    Thanks in advance,

    Jack

  2. #2
    Just Joined! barriehie's Avatar
    Join Date
    Apr 2008
    Location
    The Desert!
    Posts
    85
    Since you're reading x y z wouldn't you have to use $x $y and $z? $1 $2 and $3 would be if they're entered on the command line with the script name.

    HTH

  3. #3
    Just Joined!
    Join Date
    Nov 2010
    Location
    England
    Posts
    5
    Hi barriehie

    Do you mean like this (for example):

    #!/bin/bash
    echo "Enter the first element number, last element number and step size as x y z:"
    read $x $y $z
    echo "$#"

    I have tried this method too, but it still won't 'count' the number of variables. I'm sure it's something simple, but I can't fathom it out. (In my defence, I did only learn bash scripting last week!)

  4. #4
    Just Joined! barriehie's Avatar
    Join Date
    Apr 2008
    Location
    The Desert!
    Posts
    85
    Quote Originally Posted by jf22901 View Post
    Hi barriehie

    Do you mean like this (for example):

    #!/bin/bash
    echo "Enter the first element number, last element number and step size as x y z:"
    read $x $y $z
    echo "$#"

    I have tried this method too, but it still won't 'count' the number of variables. I'm sure it's something simple, but I can't fathom it out. (In my defence, I did only learn bash scripting last week!)
    More like:
    Code:
    read x y z
    Since you're not entering anything on the CLI after the script name then $# will be 0; $0 will contain the script name.

    To test whether or not x, y, and z were all entered you'll have to test them against being NULL.

    Code:
    ./scriptname value1 value2 value3
    $0 = ./scriptname
    $1 = value1
    $2 = value2
    $3 = value3
    etc., etc., etc.

  5. #5
    Just Joined!
    Join Date
    Nov 2010
    Location
    England
    Posts
    5
    Thanks barriehie.

    I have now added the following, which tests to make sure neither x, y nor z are null.:

    if [ -z "$x" ] || [ -z "$y" ] || [ -z "$z" ]; then
    echo "Error: Run script again and enter search parameters correctly" && exit
    fi

    It seems to be working, so I'll leave it at that!

  6. #6
    Just Joined! barriehie's Avatar
    Join Date
    Apr 2008
    Location
    The Desert!
    Posts
    85
    Glad you got it! Another [Solved].

  7. #7
    Just Joined! barriehie's Avatar
    Join Date
    Apr 2008
    Location
    The Desert!
    Posts
    85
    So is this thread solved???

  8. #8
    Just Joined!
    Join Date
    Nov 2010
    Location
    England
    Posts
    5
    Quote Originally Posted by barriehie View Post
    So is this thread solved???
    It is indeed! Am I meant to click on something, or mark it as solved??

    Jack

  9. #9
    Just Joined! barriehie's Avatar
    Join Date
    Apr 2008
    Location
    The Desert!
    Posts
    85
    Quote Originally Posted by jf22901 View Post
    It is indeed! Am I meant to click on something, or mark it as solved??

    Jack
    If you would, makes it easier for the next ?. I think you have to either edit the title to mark it solved or it's under thread tools.

  10. #10
    Just Joined!
    Join Date
    Nov 2010
    Location
    England
    Posts
    5
    Done.

    Thanks for all your help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •