Search code examples
linuxbashfirewalliptables

Group various conditions inside one IF in a bash script


I'm trying to group these conditions but it's returning:

awaited conditional binary operator
waiting for `)'
syntax error next to `$thetime'
`  ( dateutils.dtest $thetime --gt '09:30:00' && dateutils.dtest $thetime --lt '11:00:00' ) ||'

I already try like:

https://unix.stackexchange.com/questions/290146/multiple-logical-operators-a-b-c-and-syntax-error-near-unexpected-t

Groups of compound conditions in Bash test

#!/bin/bash

thetime=$(date +%H:%M:%S)

if [[
  ( dateutils.dtest $thetime --gt '09:30:00' && dateutils.dtest $thetime --lt '11:00:00' ) ||
  ( dateutils.dtest $thetime --gt '13:00:00' && dateutils.dtest $thetime --lt '17:00:00' )
]]; then
  iptables -A OUTPUT -d 31.13.85.36 -j REJECT
else
  iptables -A OUTPUT -d 31.13.85.36 -j ACCEPT
fi


Solution

  • You can to the following:

    #!/bin/bash
    
    thetime=$(date +%H:%M:%S)
    
    if ( $(dateutils.dtest $thetime --gt '09:30:00') && $(dateutils.dtest $thetime --lt '11:00:00') ) || ( $( dateutils.dtest $thetime --gt '13:00:00' ) && $( dateutils.dtest $thetime --lt '17:00:00' ) ); then
      iptables -A OUTPUT -d 31.13.85.36 -j REJECT
    else
      iptables -A OUTPUT -d 31.13.85.36 -j ACCEPT
    fi