How to judge whether today is Sunday in Linux Shell Scripts? - linux

I use these to judge Sunday in shell:
week=`date -d $day +%w`
month=`date -d "-1 day ago $day" '+%d'`
if [ "$week" = "0" ];then
if [ "$month" = "01" ];then
But when $day is 20171103 , It was regarded as Sunday , then the 20171105 was missed.
Then I run "date -d 20171105 +%w" in the server ,it shows 0, and the if caluse was true. Then I check the Time Zone , Time Zone was right ,+0800
Are there any better methods in shell that can solve this problem? More safer and robust to judge whether the string like YYYYMMDD is Sunday?

Why not just use date %u for day of the week?
There are plenty of options in the man page.

Related

Linux Date Function forward/backward time

Is there an option of date function?
How do I get LOAD_TEST_START to be 2 minutes forward?
How do I get LOAD_TEST_END to be 2 minutes back?
LOAD_TEST_START=$(date -u +%FT%TZ)
LOAD_TEST_END=$(date -u +%FT%TZ)
This answer helped me and works on the linux-version of date.
LOAD_TEST_START=$(date +%FT%TZ -d "2 minutes ago")
LOAD_TEST_END=$(date +%FT%TZ -d "2 minutes")
On macos/FreeBSD one can use
LOAD_TEST_START=$(date -v+2M +%FT%TZ)
LOAD_TEST_END=$(date -v-2M +%FT%TZ)
Not sure if this is what you want, but you can get the current epoch value and then add/subtract 120 seconds (2 minutes) from it and convert the results back to date.
dateval=`date +%s`
echo "orig "`date -d #$dateval +%FT%T%Z`
start=`expr $dateval + 120`
end=`expr $dateval - 120`
LOAD_TEST_START=`date -d #$start +%FT%T%Z`
LOAD_TEST_END=`date -d #$end +%FT%T%Z`
echo "start "$LOAD_TEST_START
echo "end "$LOAD_TEST_END

Compare the days of the week in unix scripting

I want to compare the current day of the week and execute a set of statements depending on day.
to_day=$(date +%a)
if[ "$to_day" = "Sun" ]
then
echo "Today is sunday"
echo "First day of the week"
elif[ "$to_day" = "Mon" ]
then
echo "Today is monday"
echo "Second day of the week"
and so on...
I have tried the below if formats
if[$to_day = "Tue"]
if["$to_day" -eq "Tue"]
if["$to_day" == 'Tue']
if["$to_day" = 'Tue']
But the error is still present and it reads "if[ Tue = Tue ]:Command not fount ". I have tried the above with spaces after the braces also.
Make sure in your first line is:
#!/bin/bash
and if it is in your code then make sure if: /bin and /usr/bin or /usr/local/bin directories are in you path, because all commands are here. You can check with:
$ echo $PATH
and you will get a similar answer:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11R6/bin:/usr/local/bin:/home/vivekgite/bin
but if you don't have those paths, you can add theirs with:
$ export PATH=$PATH:/bin:/usr/local/bin

Show current time of most commons timezone (UTC, EDT, CEST...)

I can have UTC current time with "date -u".
But i would like to print the current time of some common timezones at the same time. (UTC, EDT, CEST...)
I can create a script and add severals :
date -u -d 'x hour ago'
But sometimes, hours change. (Like in France)
Is there another solution to have the "real" current time, based on the country / cities maybe ?
EDIT :
Here what i have done with the answer :
function dateall(){
echo -n "US Pacific : " && TZ=US/Pacific date
echo -n "US Eastern : " && TZ=US/Eastern date
echo -n "UTC ~ GMT : " && date -u
echo -n "Europe Paris : " && TZ=Europe/Paris date
echo -n "Asia Bangkok : " && TZ=Asia/Bangkok date
}
You can point TZ to the timezone you want:
$ TZ=Europe/Paris date
Fri Jun 12 07:41:28 CEST 2015
In my dateutils there's datezone to do what you want in one simple command:
$ datezone now Europe/Paris US/Pacific US/Eastern UTC
2015-06-12T08:23:32+02:00 Europe/Paris
2015-06-11T23:23:32-07:00 US/Pacific
2015-06-12T02:23:32-04:00 US/Eastern
2015-06-12T06:23:32+00:00 UTC
2015-06-12T13:23:32+07:00 Asia/Bangkok
$
with the additional advantage that you also get the current UTC offsets and the times are guaranteed to coincide, i.e. the current time is determined only once and then used for all timezone conversions.

Generate time serie in iso-8601 format using date command, how to deal with server system date origin offset?

I have the following bash function that generate an epoch list in iso-8601 format on a machine that runs ubuntu and it works fine. (where isdate and isint bash functions to test the input)
gen_epoch()
{
## USAGE: gen_epoch [start_date_iso] [end_date_iso] [increment_in_seconds]
##
## TASK : generate an epoch list (epoch list in isodate format).
## result on STDOUT: [epoch_list]
## error_code : 2 0
## test argument
if [ "$#" -ne 3 ]; then echo "$FUNCNAME: input error [nb_of_input]"; return 2
elif [ $( isdate $1 &> /dev/null; echo $? ) -eq 2 ]; then echo "$FUNCNAME: argument error [$1]"; return 2
elif [ $( isdate $2 &> /dev/null; echo $? ) -eq 2 ]; then echo "$FUNCNAME: argument error [$2]"; return 2
elif [ $( isint $3 &> /dev/null; echo $? ) -eq 2 ]; then echo "$FUNCNAME: argument error [$3]"; return 2
else local beg=$( TZ=UTC date --date="$1" +%s ); local end=$( TZ=UTC date --date="$2" +%s ); local inc=$3; fi
## generate epoch
while [ $beg -le $end ]
do
local date_out=$( TZ=UTC date --date="UTC 1970-01-01 $beg secs" --iso-8601=seconds ); beg=$(( $beg + $inc ))
echo ${date_out%+*}
done
}
It generates the expected values for this command line example:
gen_epoch 2014-04-01T00:00:00 2014-04-01T07:00:00 3600
expected values:
2014-04-01T00:00:00
2014-04-01T01:00:00
2014-04-01T02:00:00
2014-04-01T03:00:00
2014-04-01T04:00:00
2014-04-01T05:00:00
2014-04-01T06:00:00
2014-04-01T07:00:00
However i have tried this function on a server where i have no root privileges and i have found the following results:
2014-03-31T17:00:00
2014-03-31T18:00:00
2014-03-31T19:00:00
2014-03-31T20:00:00
2014-03-31T21:00:00
2014-03-31T22:00:00
2014-03-31T23:00:00
2014-04-01T00:00:00
and i have seen that the server time origin is not at 1970-01-01T00:00:00.
typing TZ=UTC date --date="1970-01-01T00:00:00" +%s command gives the value of -25200 which corresponds to a 7 hours lag while it should give 0.
My question is how this problem could be corrected on the server?
Could You help me to find an equivalent solution for this function assuming that i don't know on which machine i am running it, so i have know apriori knowledge if the system time is correct or not?
Not a complete answer but too long for a comment.
I guess that this particular server is incorrectly configured upon setup. The problem is that BIOS clocks are set to localtime time, while the systems thinks it's in UTC (or vise versa) (use hwclock to query hardware clocks settings).
If the system is configured incorrectly and you can't fix it for any reason (don't have superuser account or whatever), I'd suggest to provide a "fixing timezone description file with your software and specify it in TZ variable like this: TZ=:/path/to/fixing/timezone date --date="1970-01-01T00:00:00" +%s. Obviously you have to pre-calculate which TZ description file fixes the problem and use a proper one. Usually available timezones are stored in /usr/share/zoneinfo

linux script ends for 6-Nov-2011, but not for 7-Nov-2011 (looping through dates)

I need to run a specific command for a bunch of dates. To that end, I wrote a simple script that will loop through dates, formatted as the command will expect them using the date command:
startdate=`/bin/date --date="January 22 2011" +%e-%b-%Y`
enddate=`/bin/date --date="7-Nov-2011" +%e-%b-%Y`
echo "Start Date: $startdate"
echo "End Date: $enddate"
sleep 5
incdate="$startdate"
until [ "$incdate" == "$enddate" ]
do
echo "$incdate"
incdate=`/bin/date --date="$incdate 1 day" +%e-%b-%Y`
done
exit
If I set enddate to "6-Nov-2011" the script will stop as expected after printing 5-Nov-2011. However If i set enddate to "7-Nov-2011" as above, the script will print out "6-Nov-2011" forever. I can't seem to figure out why...any ideas?
Thank You.
You could use at or crontab
I think I figured out the problem - due to daylight savings time, incrementing 6-Nov-2011 by one day results in 6-Nov-2011 23:00:00 instead of 7-Nov-2011! Suppose I can put in an "if" for this special case.

Resources