Answer the question
In order to leave comments, you need to log in
Time condition not working in bash script?
time condition does not work in bash script
table structure
#!/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
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question