Getting error -: more tokens expected in shell script

Hi
can someone help me to resolve the error

for this condition

if [[ $k -le $kkm ]] && [[ ${k:-0} -ne 0 ]]; then

i am passing the values $k and $kkm

i am getting the error like "-: more tokens expected"

Thanks in Advance

How are you passing the parameters, could you post the exact command? What shell are you using?

Run an xtrace and post result.

The most likely problem is that $k or $kkm expands to an empty string.

if [[ "$k" -le "$kkm" ]] && [[ ${k:-0} -ne 0 ]]; then

will probably get rid of that error message, but may give you a different error when it finds that one or both of the arguments to the -le operator are not numeric strings. It looks like your second test assumes that $k might be an empty string and adjusts for that fact; the first test does not. Maybe you wanted:

if [[ ${k:=0} -le ${kkm:=0} ]] && [[ $k -ne 0 ]]; then

which will set k and kkm , respectively, to 0 if they were originally unset or were set to empty strings. Note that this still won't protect you if $k or $kkm expands to a non-empty, non-numeric string.

Don got it right, already, but i want to know what the "&&" is supposed to do? If you want to do what i think this is supposed to do (combine two conditions with a logical AND) you might consider:

if [[ $k -le $kkm -a ${k:-0} -ne 0 ]]; then

I hope this helps.

bakunin

Yes, && between two commands evaluates to true if both commands evaluate to true.

But, the command:

if [[ $k -le $kkm ]] && [[ ${k:-0} -ne 0 ]]; then

is not equivalent to:

if [[ $k -le $kkm -a ${k:-0} -ne 0 ]]; then

There are a few problems with the above that include, but might not be limited to:

  1. -a is required to be accepted by POSIX conforming implementations of the utilities test and [ only if the implementation supports the X/Open System Interfaces option and even then is marked obsolescent. The standards don't currently define the [[ conditional expression syntax, but shells that do support -a as an operator in expression in commands of the form [[ expression ]] (including both bash and ksh ) treat -a in [[ expression ]] as an obsolescent synonym for the unary -e filename operator rather than the binary logical and operator that is supported by some versions of test and [ .
  2. It is still using unquoted (possibly unset or set to empty string) variables k and kkm .
  3. And, there is still no verification that k and kkm are numeric strings.

One way to reliably perform this test using any POSIX conforming shell is:

if [ "${k:=0}" != "${k#*[!0-9]}" ]
then    printf "k (%s) is not a numeric string\n" "$k" >&2
        exit 1
else    printf "k is a numeric string (%s)\n" "$k"
fi
if [ "${kkm:=0}" != "${kkm#*[!0-9]}" ]
then    printf "kkm (%s) is not a numeric string\n" "$kkm" >&2
        exit 2
else    printf "kkm is a numeric string (%s)\n" "$kkm"
fi
if [ $k -le $kkm ] && [ $k -ne 0 ]
then    printf "%s <= %s and %s != 0\n" $k $kkm $k
else    printf "%s > %s or %s == 0\n" $k $kkm $k
fi