Counting variables in bash script using $#

In summary, to check if the correct number of variables were entered after reading in a data file and converting it into an array, one can use the '[ -z "$x" ] || [ -z "$y" ] || [ -z "$z" ]' statement in the if statement to ensure that x, y, and z are not null before continuing with the rest of the script. This is a useful technique to use in bash scripting when dealing with user input.
  • #1
jf22901
55
1
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 'read $1 $2 $3' and 'read $x $y $z', but doing so results in echo "$#" = 0. 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!)

I've attached a copy of the script as a text file in case the above makes no sense! The data file it reads is just a file of random numbers I am practising with.

Many thanks in advance!
 

Attachments

  • testscript.txt
    1.2 KB · Views: 448
Technology news on Phys.org
  • #2
Problem solved!

I have added '[ -z "$x" ] || [ -z "$y" ] || [ -z "$z" ]' to the if statement, to make sure x, y or z aren't null, and it seems to be working!
 

1. How do I use the $# variable in a bash script?

The $# variable in bash stores the number of arguments passed to the script. To use it, simply reference it in your script as "$#".

2. Can I use the $# variable to count the number of files in a directory?

Yes, you can use the $# variable to count the number of files in a directory by passing the directory name as an argument to the script and then using the command "ls -l | wc -l" to count the number of lines in the output.

3. How do I check if the correct number of arguments have been passed to my script using the $# variable?

You can use the $# variable in an if statement to check if the correct number of arguments have been passed. For example:
if [ "$#" -ne 2 ]
then
echo "Incorrect number of arguments passed."
fi

4. Can I use the $# variable to count the number of lines in a file?

No, the $# variable only counts the number of arguments passed to the script. To count the number of lines in a file, you can use the command "wc -l < filename" or use a loop to iterate through the file and increment a counter variable.

5. What happens if no arguments are passed to the script and I use the $# variable?

If no arguments are passed to the script, the value of the $# variable will be 0. It is important to check for this when using the $# variable in your script to avoid errors or unexpected results.

Similar threads

  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
19
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
Replies
11
Views
994
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
15
Views
1K
Replies
16
Views
2K
  • Programming and Computer Science
Replies
4
Views
609
  • Set Theory, Logic, Probability, Statistics
Replies
3
Views
1K
Back
Top