S
S
Sergey2017-06-06 13:34:07
linux
Sergey, 2017-06-06 13:34:07

Time condition not working in bash script?

time condition does not work in bash script
table structure4f18deb2e7324b53a7b5d0870f70a7b1.PNG

#!/bin/sh
echo "Начинаем..."
#####забираю значения из баз данных
client=$(PGPASSWORD='postgres'   psql   -U postgres -d freeswitchdb -c "SELECT client FROM autodialer WHERE   status =  '1' LIMIT 1" | cat -n | awk '{print$2}' |  awk 'NR == 3')
start_time=$(PGPASSWORD='postgres'   psql   -U postgres -d freeswitchdb -c "SELECT interval_time_start FROM autodialer WHERE client = '$client' and status =  '1' " | cat -n | awk '{print$2}' |  awk 'NR == 3')
end_time=$(PGPASSWORD='postgres'   psql   -U postgres -d freeswitchdb -c "SELECT interval_time_end FROM autodialer WHERE client = '$client' and status =  '1' " | cat -n | awk '{print$2}' |  awk 'NR == 3')
current_time=$(date | cat -n | awk '{print$5}')

################вывожу значения
echo "Клиент...$client"
echo "Start time ... $start_time"
echo "End time ...$end_time"
echo "Current time ...$current_time"


if [ "$current_time"=>$start_time and "$current_time"=>end_time ]
then
    echo "попали под условие ..."
else
    echo " не попали под условие ..."

fi

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Saboteur, 2017-06-06
@Kle6800

Use a timestamp, bash can't compare dates.
Use declare -i, otherwise all bash variables default to a string, not a number.
the --date option allows you to specify which date you want to use
date --date="2011/12/12 12:12:12"
Monday, 12 december 2011 12:12:12 +0200
+%s option allows you to display the desired date using date date as "seconds since 1970", get integers that bash can compare. Simple example:

#!/bin/bash

declare -i MYDATE1=`date --date="2011/12/12 12:12:12" +%s`
declare -i MYDATE2=`date --date="2017/07/06 11:11:11" +%s`
declare -i CURRDATE=`date +%s`

echo $MYDATE1 $MYDATE2 $CURRDATE

if [ $MYDATE1 -lt $CURRDATE -a $MYDATE2 -gt $CURRDATE ]; then
  echo "Сейчас больше чем $MYDATE1 и меньше чем $MYDATE2"
else
  echo "не-а"
fi

E
Eugene, 2017-06-06
@immaculate

The command dateis able to issue the date in the required format, it is not necessary to pervert with cat and awk: date +%H:%M:%S. Bash does not know how to compare dates, you can convert them to Unix epoch and compare them as numbers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question