0

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

Marco A. Braghim
  • 516
  • 1
  • 5
  • 19

3 Answers3

2

Assuming dateutils.dtest is just an ordinary executable that uses its arguments to perform some sort of comparison, you want something like

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

This assumes, for example, that dateutils.dtest has an exit status of 0 if $thetime is after 9:30:00, and a non-zero exit status otherwise.

The braces ({ ... }) act as grouping operators since && and || have equal precedence in shell; note the semicolon before each closing } is required.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • The other answer attempts to execute the output of `dateutils.dtest` as an additional command (unless they produce no output, in which case the command substitutions aren't necessary). – chepner Feb 25 '19 at 16:56
0

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
-2

I would lose the colons (:) and do the following comparison:

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

if [ "$thetime" -gt "093000" ] && [ "$thetime" -lt "110000" ] || [ "$thetime" -gt "130000" ] && [ "$thetime" -lt "170000" ]; then
  iptables -A OUTPUT -d 31.13.85.36 -j REJECT
else
  iptables -A OUTPUT -d 31.13.85.36 -j ACCEPT
fi