If + expr

Hi

I have bellow script:

t1=`cat node1.txt | grep thread1 | cut -f2 -d '-'`
t2=`cat node2.txt | grep thread2 | cut -f2 -d '-'`

t3=`cat node1_rcat.txt | grep thread1 | cut -f2 -d '-'`
t4=`cat node2_rcat.txt | grep thread2 | cut -f2 -d '-'`

if [[ `expr $1 + $2` == `expr $3 + $4` ]]; then
echo "no restore" >> log.log
exit;
fi
echo "need restore"  >> log.log

when I execute this script I have this error:

expr: 0402-050 Syntax error.
expr: 0402-050 Syntax error.

pls help

I think the above should be

if [[ `expr $t1 + $t2` == `expr $t3 + $t4` ]]; then

no :confused:

$1 $2 $3 $4 are positional parameters...

That would not necessarily lead to syntax errors. These are because the parameters are unset/empty. Try double quoting them whatever they are: $(expr "$t1" + "$t2") .

what can I say
my blunder:)

thanks

Unrequried use of cat, and use $(expr) rather than `expr` , that is old.

t1=`cat node1.txt | grep thread1 | cut -f2 -d '-'`

becomes

t1=$(grep thread1  node1.txt | cut -f2 -d '-')

So do t2,3,4..

You could check if there less than 4 positional parameters, and exit if so (stay with 4 or more)...

[[ $# -lt 4 ]] && echo "Missing arguments..." && exit 1

Hope this helps

Thank's sea, I will remove 'cat' from my script,

If $t1 , $t2 , $t3 , and $t4 expand to integer values, any shell that recognizes:

if [[ `expr $t1 + $t2` == `expr $t3 + $t4` ]]; then

should also be able to perform arithmetic expansions (which are shell built-ins) instead of invoking the expr utility twice. Try changing the above line to:

if [[ $(($t1 + $t2)) == $(($t3 + $t4)) ]]; then

If you're using a 1993 or later version of ksh , this will also work if those variables expand to floating point values.

Even if your shell doesn't support arithmetic expansions or if you're dealing with floating point values and you aren't using a recent ksh , you could get rid of one invocation of expr using:

if [[ `expr $t1 + $t2 - $t3 - $t4` == 0 ]]; then

or, preferably, the non-obsolescent form:

if [[ $(expr $t1 + $t2 - $t3 - $t4) == 0 ]]; then

If you'd show us some sample contents of your node*.txt files, we can probably also help you replace your four grep and cut pipelines with something faster.