2012-03-08 22 views
0

我想使用IF子句數學表達式IF子句 - 這樣的事情:數學表達式與Linux的shell

  #!/bin/sh 

     now=`date +%H%M` 

     if [ $now % 5 -gt 0 ] ; then 
      exit 
     fi 

     perl ... 

什麼是正確的語法是什麼?

回答

3
 
if expr $now % 5 \> 0 > /dev/null; then ... 

 
if test $(($now % 5)) -gt 0; then ... 

注意,你並不真正需要的,如果條款在所有,你可以做

 
test $(($now % 5)) -gt 0 && exit 
+0

再說了簡單,資源較少? – 2012-03-08 20:58:12

+0

「少資源」是什麼意思? expr是在大多數shell中內置的,但你可以這樣做:test $(($ now%5))-gt 0 – 2012-03-08 21:08:25

+0

This >> $((now%5))-gt 0 << or this >> $(($ now%5) )-gt 0 <<? – 2012-03-08 21:14:50

1

假設慶典:

if ((now%5 > 0)); then ... 
+0

This works >> if [$((now%5))-gt 0];然後... – 2012-03-08 21:12:40

+0

哪一個是正確的>> $現在還是現在? – 2012-03-08 21:15:24

+0

我不知道它在哪裏被記錄,但是在(())裏面,你不需要簡單變量的'$'。 – 2012-03-08 21:31:52