Question

I have a shell script containing this:

var1=`expr $RANDOM % 100`
var2=`expr $RANDOM % 1000 \* 60`
...
...
sleep `expr $var2- `date -t` + $var1`

It gives me this error:

sleep:invalid number of operands 
expr error: invalid syntax
+ cannot execute no such file or directory

Why? What does the error mean?

Était-ce utile?

La solution

Because backticks don't nest.

If your shell supports the more modern $(...) syntax, try this:

var1=$(expr $RANDOM % 100)
var2=$(expr $RANDOM % 1000 \* 60)
...
...
sleep $(expr $var2 - $(date -t) + $var1)

If not, you can store the intermediate value in another variable:

var1=`expr $RANDOM % 100`
var2=`expr $RANDOM % 1000 \* 60`
...
...
date=`date -t`
sleep `expr $var2 - $date + $var1`

(I've also added a space, changing $var2- to $var2 -.)

Incidentally, I was unable to try this, since on my system the date command has no -t option.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top