Print Error for Missing Argument

Print Error for Missing Argument

I am trying to write a script which will check number of arguments for first and second number; if both variable entered, it will do the calculation; if one argument is missing, it will print error message. Here what I've done so far:

#!/bin/bash
echo -n "Enter the first number: "
read num1
echo -n "Enter the second number: "
read num2

if [ $# -le 1 ]; then
    echo "Illegal number of arguments"
    exit
else
    echo "The sum is: " $(( num1 + num2 ))
fi

I am always getting error message even though I enter both of the numbers. What am I missing? Please help.

3 Answers

Test Your Assigned Variables, Not Positional Parameters

Your variables num1 and num2 aren't positional parameters, and so the special parameter $# is probably not what you think it is. You should change your conditional to check that both variables are set. For example:

declare -i num1 num2

read -p 'Enter the first number: '  num1
read -p 'Enter the second number: ' num2

if [ -z "$num1" ] || [ -z "$num2" ]; then
    echo "Illegal number of arguments" >&2
else
    echo "The sum is: $((num1 + num2))"
fi
4

Looks like you are messing up command line arguments and variables you are reading interactively. $# has nothing to do with variables you declared and/or read from command line. It is the number of command line arguments. You need to check the variables you attempted to read from console:

#!/bin/sh

echo -n "Enter the first number: "
read num1
echo -n "Enter the second number: "
read num2

[ -z $num1 ] || [ -z $num2 ] && echo "Illegal number of arguments" && exit 1
echo "The sum is: " $(( num1 + num2 ))

On the other hand, if you really want to check command line arguments, the script will be even simpler:

#!/bin/sh

[ -z $2 ] && echo "Illegal number of arguments" && exit 1

echo "The sum is: " $(( $1 + $2 ))

Here $1 refers to the first argument, $2 refers to the second argument and so on. $0 refers to the name of the script itself.

So, the way you have your program setup right now, it takes input through the read command, but that isn't the same as passing in an argument.

You pass an argument through the CLI, for instance: ./sum.sh 5 2 # => 7 Where sum.sh is the name of the file and 5 and 2 are your arguments.

So, the reason you keep getting "Illegal number of arguments" is because in bash the $# variable holds the number of arguments, but since you're reading the values in from the code, $# will always less than 1 because no arguments have been provided.

What I think you're looking for is something like this:

#!/bin/bash

if [ $# -le 1 ]; then
   echo "Illegal number of arguments"
   exit
else
   echo "The sum is: " $(( $1 + $2 ))
fi

This article is pretty good if you want to learn more:

Maya Lin-Takahashi
Author

Maya Lin-Takahashi

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.