带有Linux shell的IF子句中的数学表达式

时间:2012-03-08 20:46:12

标签: linux shell

我想在IF子句中使用数学表达式 - 类似这样:

         #!/bin/sh

         now=`date +%H%M`

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

         perl ...

对此有什么正确的语法

2 个答案:

答案 0 :(得分:3)

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

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

请注意,您根本不需要if子句,并且可以执行

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

答案 1 :(得分:1)

假设bash:

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