문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top