rottlog-develop
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Rottlog-develop] parse_period


From: stefko5
Subject: [Rottlog-develop] parse_period
Date: Thu, 19 Sep 2002 12:11:51 +0200
User-agent: Mutt/1.2.5i

This is a first draft of what will be parse_period function.
It lacks long weekday and monthnames, stamp_file input variable and various
check for all options.

See if it works:

----------------------------- CUT -----------------------------
#!/bin/bash

#period mon-fri !15d 10:00, 23:00


parse_period () {
  local today=$(date +%d)
  local name_today=$(LANG=en date +%a|tr A-Z a-z)
  local name_month=$(LANG=en date +%b|tr A-Z a-z)
  local now_hour=$(date "+%H")
  local now_min=$(date "+%M")
  local last_day_of_month=$(cal -1|tail -2|head -n 1|rev|cut -d' ' -f1|rev)
  local exit_t=
  OLDIFS="$IFS"
  IFS=","
    for opt in $1; do
      [ -z "$opt" ] && continue 
      [ -z "$2" ] && criteria= || criteria="$2"
      dont_eval=
      opt=$(echo $opt|tr -s ' ')
      ltoken=0
      IFS="$OLDIFS"
      if [ "${opt:0:1}" = " " ]; then
        opt=${opt:1}
      fi
      for token in $opt; do
        ltoken=$[ ltoken + ${#token} ]
        rest=${opt:ltoken}
        if [ "${token:0:1}" = '!' ]; then
          echo "Operatore NOT"
          NOT='!'
          token=${token:1}
          rest=${opt:ltoken+1}
        fi
        case $token in
          *-*) 
              echo "Expanding interval"
              begin_t=$(echo "$token"|cut -d'-' -f1)
              end_t=$(echo "$token"|cut -d'-' -f2)
              
              case $token in
                *mon*|*tue*|*wed*|*thu*|*fri*|*sat*|*sun*)
                  default_begin="mon"
                  default_end="sun"
                  list_elem="mon tue wed thu fri sat sun"
                  ;;
                *jan*|*feb*|*mar*|*apr*|*may*|*jun*|*jul*|*ago*|*sep*|*oct*|\
                *nov*|*dec*)
                  default_begin="jan"
                  default_end="dec"
                  list_elem="jan feb mar apr may jun jul ago sep oct nov dec"
                  ;;
                esac
                # Se è un intervallo tipo -xxx
                if [ -z "$begin_t" ]; then
                  begin_t="$default_begin"
                fi
                # Se è un intervallo tipo xxx-
                if [ -z "$end_t" ]; then
                  end_t="$default_end"
                fi
                append=
                for i in $list_elem; do
                  [ $i = "$begin_t" ] && append=1
                  [ ! -z $append ] && exit_t="$exit_t, $NOT$i $rest"
                  [ $i = "$end_t" ] && append=
                done
              ;;
        esac
        if [ ! -z "$exit_t" ]; then
          parse_period "$exit_t" "$criteria"
          if [ $? -eq 1 ]; then
            return 1
          fi
          exit_t=
          dont_eval=1
          break
        fi
        echo "checking token=$token"
        case $token in
          [[:digit:]][[:digit:]]:[[:digit:]][[:digit:]])
              echo "--> HH:MM"
              check_h="$(echo $token|cut -d':' -f1)"
              check_m="$(echo $token|cut -d':' -f2)"
              criteria="$criteria && [ \"$check_h:$check_m\" $NOT= 
\"$now_hour:$now_min\" ]"
              ;;
          mon|tue|wed|thu|fri|sat|sun)
              echo "--> abbreviated weekday"
              check_wday="$token"
              criteria="$criteria && [ $check_wday $NOT= $name_today ]"
              ;;
          jan|feb|mar|apr|may|jun|jul|ago|sep|oct|nov|dec)
              echo "--> abbreviated monthname"
              check_month="$token"
              criteria="$criteria && [ $check_month $NOT= $name_month ]"
              ;;
          [[:digit:]]d|[[:digit:]][[:digit:]]d)
              echo "--> days period"
              check_pday=$(echo $token|cut -d'd' -f1)
              check_pday=$[ 86400 * check_pday ]
              criteria="$criteria && [ $NOT $check_pday -lt $stamp_file ]"
              ;;
          [[:digit:]]M|[[:digit:]][[:digit:]]M)
              echo "--> month period"
              check_pmonth=$(echo $token|cut -d'M' -f1)
              check_pmonth=$[ 2592000 * check_pmonth ]
              criteria="$criteria && [ $NOT $check_pmonth -lt $stamp_file ]"
              ;;
          [[:digit:]]|[[:digit:]][[:digit:]])
              echo "--> Exact day"
              if [ -z "$NOT" ]; then
                criteria="$criteria && [ $today -eq $token ]"
              else
                criteria="$criteria && [ $today -ne $token ]"
              fi
              ;;
          *)
              echo "Error in period definition. Token: $token"
              exit 3
              ;;
        esac
        NOT=
      done
      # Qui faccio l'eval criteria per controllare se la condizione è verificata
      # Se si -> exit; se no -> continua.
      if [ -z "$dont_eval" ]; then
        criteria=${criteria:4}
        echo "evaluating:"
        echo "$criteria"
        eval "if $criteria; then return 1; fi"
      fi
    done
  IFS="$OLDIFS"
  return 0
}


parse_period "$1"
if [ $? -eq 1 ]; then
  echo "VERIFIED"
else
  echo "not verified"
fi

----------------------------- CUT -----------------------------



-- 
Saluti,
  Stefano




reply via email to

[Prev in Thread] Current Thread [Next in Thread]