how to specify number of argument in shell

Hi I am new in shell,

I am trying to create a small script that can do exit if a script is executed when argument not 2

#!/bin/sh
if [ $# -gt 2]; then
        echo greater
        exit 1;
elif [ $# -le]; then
        echo less
        exit 1;
fi

it keeps returning me
[: 10: missing ]
[: 10: missing ]
whatever number of argument I specify..

Can anyone point out?

Thank you

A space is missing 2 and ]
The operand is missing in the second elif statement

Try this... -ne means "not equal to"...

#!/bin/sh
if [ $# -ne 2 ]; then
    echo "Invalid no. of arguments... Exiting..."
    exit 1
fi

--ahamed

1 Like