bash if statement with time - bash

I am trying to test a date ...if it's between those 2 time frames i want to print them...
But i get errors..This is my script
today_time=$(date +%H:%M:%S --date '-1 sec')
today_dow=$(date +"%u")
today_less=$(date +%H:%M:%S --date '-1 min')
today_more=$(date +%H:%M:%S --date '+1 min')
echo $today_less
echo $today_time
echo $today_more
if [ $today_less -gt $today_time ] && [ $today_time -lt $today_more ]; then
echo "$today_less"
echo "$today_time"
echo "$today_more"
else
echo no
fi
I have tried -ge with -le and gives me: Integer expression expected
-gt and -lt: Integer expression expected
I also used '<' and '>' and gives me: No such file or directory.
I need to make this work with time dates only...not with int
Cause the time i will extract it from the db and replace today_time with the time from db.

Your output is not the digits, but strings like:
[sahaquiel#sahaquiel-PC ~]$ date +%H:%M:%S --date '-1 min'
13:40:30
Convert it to Unixtime and compare clear numbers, for example:
today_time=$(date +%H:%M:%S --date '-1 sec')
today_time_u=$(date +%s --date '-1 sec')
today_dow=$(date +"%u")
today_less=$(date +%H:%M:%S --date '-1 min')
today_less_u=$(date +%s --date '-1 min')
today_more=$(date +%H:%M:%S --date '+1 min')
today_more_u=$(date +%s --date '+1 min')
echo $today_less
echo $today_time
echo $today_more
if [ $today_less_u -gt $today_time_u -a $today_time_u -lt $today_more_u ]; then
echo "$today_less"
echo "$today_time"
echo "$today_more"
else
echo no
fi
And once more, in your example, $today_less is current date - 1 minute; $today_time is current date - 1 second; So, $today_time > $today_less every time and your if [ $today_less_u -gt $today_time_u part will be false every time, so you will not be able get anything but echo no in else statement.

Related

bash automate the user input <enter> in the loop

#!/bin/sh
dom=$(date '+%d') # 01-31, day of month
year=$(date '+%Y') # four-digit year
month=$(date '+%m') # two-digit month
nworkdays=0
for d in $( seq 1 $dom)
do
today=$(date -d "$year-$month-$d" '+%x') # locale's date representation (e.g. 12/31/99)
dow=$(date -d "$year-$month-$d" '+%u') # day of week: 1-7 with 1=Monday, 7=Sunday
if [ "$dow" -le 5 ] && grep -vq "$today"
then
workday=Yes
nworkdays=$((nworkdays+1))
else
workday=
fi
done
[ "$workday" ] && [ "$nworkdays" -eq 6 ] && echo "$nworkdays $today $workday"
~
I wanted to execute the script but the script goes inside the loop and asks for user input.
Expected result
on execution display workday
Best Regards,
Shalini
As mentioned by #urbanespacemen kindly remove the grep command
#!/bin/sh
dom=$(date '+%d') # 01-31, day of month
year=$(date '+%Y') # four-digit year
month=$(date '+%m') # two-digit month
nworkdays=0
for d in $( seq 1 $dom)
do
today=$(date -d "$year-$month-$d" '+%x') # locale's date representation (e.g. 12/31/99)
dow=$(date -d "$year-$month-$d" '+%u') # day of week: 1-7 with 1=Monday, 7=Sunday
if [ "$dow" -le 5 ]
then
workday=Yes
nworkdays=$((nworkdays+1))
else
workday=
fi
done
[ "$workday" ] && [ "$nworkdays" -eq 6 ] && echo "$nworkdays $today $workday"
Now should work

number of days remaining in the year in bash

This kills my linter in vs-code.
Is there a one-liner that could better perform this?
echo -n "$((($(date -j -f "%Y%m%d" "$(( $(date +"%Y0101")+10000 ))" "+%s") - $(date "+%s"))/(60*60*24)))";
echo " days until $(( $(date +"%Y")+1 ))"
prints:
308 days until 2020
I would use python:
python -c 'from datetime import datetime as dt;y=dt.now().year+1;print('{} days until {}'.format((dt(y,1,1)-dt.now()).days,y))'
which is the one-linerized version of:
from datetime import datetime as dt
next_year = dt.now().year + 1;
days = (dt(next_year, 1, 1) - dt.now()).days
print('{} days until {}'.format(days, next_year))
Output (using the UTC timezone):
306 days until 2020
Pure Bash one-liner (tested on Linux, no Mac here)
eoy=$([ -n "$(date -d 'Feb 29' '+%-j' 2>/dev/null)" ] && echo 366); echo "$((${eoy:-365} - $(date '+%-j'))) days until $(($(date -d 'Dec 31' '+%Y') +1))"
Result:
307 days until 2020
Testing a leap year:
eoy=$([ -z "$(date -d 'Feb 29 2020' '+%-j' 2>/dev/null)" ] && echo 366); echo "$((${eoy:-365} - $(date '+%-j'))) days until $(($(date -d 'Dec 31 2020' '+%Y') +1))"
Result:
308 days until 2021
Same in 2 lines:
# year days if leap year
eoy=$([ -z "$(date -d 'Feb 29' '+%-j' 2>/dev/null)" ] && echo 366)
# ${eoy:-365} -> if $eoy is not set or empty string use default 365
echo "$((${eoy:-365} - $(date '+%-j'))) days until $(($(date -d 'Dec 31' '+%Y') +1))"

How to use korn shell get the first/last day of previous month

my ksh version is not allowed date -d.
The bellow code has problem when current month is January.
#!/bin/ksh
yy=`date +%Y`
mm=`date +%m-1|bc`
[ $mm -lt 1 ] && (mm=12;yy=`expr $yy - 1`)
[ $mm -le 9 ] && mm="0$mm"
getcal=`cal $mm $yy`
last_dd=`echo $getcal|awk '{print $NF}'`
dd1=`echo $getcal|awk '{print $10}'`
first_dd="0$dd1"
echo $yy$mm$last_day
echo $yy$mm$first_day
You're using a subshell: any variable changes you make will not survive the subshell exiting:
$ m=5; (m=10); echo $m
5
Use a different grouping construct
[ $mm -lt 1 ] && { mm=12; ((yy -= 1)); }
If you have GNU date, you can do:
$ first_of_this_month=$(date "+%Y-%m-01")
$ date -d "$first_of_this_month - 1 day" "+%Y-%m-%d"
2014-03-31
$ date -d "$first_of_this_month - 1 month" "+%Y-%m-%d"
2014-03-01

How to list date of every monday and thrusdays in shell scripting?

If I give month and year as input the output should be dates of mondays and thrusdays of that month.
eg: If input is
year = 2013
month = 02
output should be
2013-02-04
2013-02-07
2013-02-11
2013-02-14
2013-02-18
2013-02-21
2013-02-25
2013-02-28
how to use date function for this in shell scripting?
This method requires only 2 call to date, the rest is just arithmetic
year=2013
month=2
last=$(date -d "$year-$month-1 + 1 month - 1 day" +%d)
wday=$(date -d "$year-$month-1" +%w)
for (( day=1; day<=$last; day++ )); do
if (( wday == 1 || wday == 4 )); then
printf "%4d-%02d-%02d\n" $year $month $day
fi
(( wday = (wday + 1) % 7 ))
done
2013-02-04
2013-02-07
2013-02-11
2013-02-14
2013-02-18
2013-02-21
2013-02-25
2013-02-28
year=2013
month=02
#Monday=1,Sunday=7
display_days="1 4"
for day in $(seq 1 31)
do
dow=$(date -d "$year-$month-$day" +%u 2>/dev/null)
for d in $display_days
do
if [ -z $dow ]; then continue; fi
if [ "$dow" -eq "$d" ]
then
date -d "$year-$month-$day" +%y-%m-%d
fi
done
done
Or just defer to ncal.
LC_ALL=C ncal "$#" | sed -n 's/\(Mo\|Th\)//p'
Weekdays are subject to localization so I added a C locale just in case.
Another way:
year=2013 month=02
cal $month $year |
sed '1,2d; s/^.. \(..\) .. .. \(..\).*/\1\n\2/' |
sed "/^ *$/d; s/^ /0/; s/^/$year-$month-/"

If time is more than X and less than Y, between daybreaks?

In one of my loops, I've made my Bash script do things depending on the time:
if [ $(date +%k%M) -ge 2200 ] && [ $(date +%k%M) -lt 2300 ] ; then echo "Betwen 22 and 23!"; fi
However, I just got a problem I didn't expect (although I should have). The if doesn't work when 23:59 becomes 00:00.
if [ $(date +%k%M) -ge 2359 ] && [ $(date +%k%M) -lt 200 ] ; then CURRENTFOLDER="$TARGETFOLDER/$BLOCK1"; fi
200 is numerically less than 2359, but 02:00 is later than 23:59.
What can I do?
Converting to seconds makes this type of 'arithmetic' much easier and more reliable. Use date's '--date=' option to specify that next day's window:
date --date="Today HH:MM" +"%s"
date --date="+1 day HH:MM" +"%s"
Also, for simplicity's sake, call the current time once and use it for comparison :)
NOW=$(date +"%s")
Examples:
(( ${NOW} >= $(date --date="Today 22:00" +"%s") )) && (( ${NOW} < $(date --date="Today 23:00" +"%s") ))
(( ${NOW} >= $(date --date="Today 23:59" +"%s") )) && (( ${NOW} < $(date --date="+1 day 02:00" +"%s") ))
You can also use seconds field to do file modification comparisons, etc., using output from 'find' or 'stat'.
Depending on how many actions you take, you'd be better using combining into a case statement.
Good luck.

Resources