#!/bin/bash
if [ [ $a == $b + $c ] || [ $b == $a + $c ] || [ $c == $a + $b ] ]; then

result: too many arguments
What is the proper syntax?

Thanks!

Recommended Answers

All 4 Replies

Hello landog!

I'm not sure exactly where that particular error comes from, but there are a couple of issues with this.

First, when comparing integers you'll want to use the '-eq' operator. Use '=' to compare strings. I think '==' won't work in some shells.

Second, your 'if' statement is enclosed in an extra set of [brackets]. Knock those off and we're one step closer!

Third, and I could be wrong about this, but 'if' won't do math for you. The way to express a math problem in bash is $((x+y)) (or $(($x+$y)) ).

The 'Advanced Bash Scripting Guide' on the tldp.org site is just about the best bash scripting reference out there. Here's a link to the page on operators:

http://tldp.org/LDP/abs/html/comparison-ops.html

Here's a working example (in my shell anyway) of what I think you're trying to express:

#!/bin/bash

a=1
b=1
c=2

if [ $a -eq $((b+c)) ] || [ $b -eq $((a+c)) ] || [ $c -eq $((a+b)) ]; then
   echo 'Looks OK!'
else
   echo 'No Match :-('
fi

In this case the third expression should be true, triggering the 'Looks Ok!' result.

I hope this helps!
-G

Gromit,

Thank you. Much appreciated!

-dog

Its working only one things we have to take care is bracket, because system does not understand the meaning of given statement if we put wrong bracket in wrong position, so we need to define if with OR logical operator are as follows

if [ $c -eq $((a+b)) ] || [ $c -eq $((a+d)) ]; then

echo "looks ok"
else
echo "Not match"

fi

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.