Offset of a given timezone from GMT in linux shell script - linux

Is there a way to get the offset of a given timezone (identifier like EDT or America/New_York) from GMT in linux shell script?

Export your TZ environment variable and print date with %z for timezone offset.
#!/bin/sh
export TZ=":Pacific/Auckland"
date +%z

This is a roundabout way to do it but it works (loosely based on this):
#!/bin/bash
ZONE=$1
TIME=$(date +%s --utc -d "12:00:00 $ZONE")
UTC_TIME=$(date +%s --utc -d "12:00:00")
((DIFF=UTC_TIME-TIME))
echo - | awk -v SECS=$DIFF '{printf "%d",SECS/(60*60)}'
Save that as tzoffset, make it executable, and run it like this:
tzoffset PST
This script in its current form only handles abbreviated timezones.

Related

LC_TIME="en_GB.utf8" not more working since March

On German Linux-system I run script to grep information by date from English document.
LC_TIME="en_GB.utf8" grep -a "\[$(date -d "yesterday" +%b\ %d)" file.log
Since March script don't grep information anymore. Before it worked fine over months.
Manual I see the problem, that not "Mar" but German "Mär" is in search string:
LC_TIME="en_GB.utf8" echo $(date -d "yesterday" +%b\ %d)
Mär 27
Following LC_TIME="en_GB.utf8" is not more working correct.
What can I do?
You are changing LC_TIME for grep and for echo not for date.
echo $(LC_TIME="en_GB.utf8" date -d "yesterday" +%b\ %d)
grep -a "[$(LC_TIME="en_GB.utf8" date -d "yesterday" +%b\ %d)"

Print the path of a file a day before and a day after in Shell script

I am having a shell script like below
#!/bin/bash
TIMESTAMP=`date "+%Y-%m-%d"`
path=/home/$USER/logging/${TIMESTAMP}/status/${TIMESTAMP}.fail_log
echo filePath=$path
In this script I want to print the path of the failed logs for that particular timestamp.
Now I am able to get the echo to print the path.
How do I print a day before and day after the timestamp? Is it possible to do that?
How Can I do that in a single line of code? Can we do that?
To get tomorrow's data, you can do:
date -d '+1 day' "+%Y-%m-%d"
To get yesterday's data, you can do:
date -d '-1 day' "+%Y-%m-%d"
To use it in script:
#!/bin/bash
nextDate=$(date -d '+1 day' "+%Y-%m-%d")
prevDate=$(date -d '-1 day' "+%Y-%m-%d")
nextDatePath=/home/$USER/logging/${TIMESTAMP}/status/${nextDate}.fail_log
prevDatePath=/home/$USER/logging/${TIMESTAMP}/status/${prevDate}.fail_log

How to get time in seconds since process started, without using etimes?

Trying to use ps to find the number of seconds since a process started. The version of ps I have doesn't support etimes and not sure how else to get this info?
For process of pid 1234, you could use the mtime field of meta-data of pseudo-file /proc/1234/status. Read proc(5) for more.
See also stat(2) and stat(1) and date(1).
So date +%s is giving the current date since the Unix epoch, e.g. 1479125355 now in November 14th, 2016. stat -c %Y /proc/1234/status is giving the start time of process 1234 since Unix epoch. You want the difference. Perhaps use (barely tested, my interactive shell is zsh) $[$(date +%s) - $(stat -c %Y /proc/1234/status)]; adapt that to your shell.
For example:
bash -c 'sleep 4; echo $(($(date +%s) - $(stat -c %Y /proc/$$/status)))'
is giving me 4 as expected. Of course the $$ is expanded to the pid of the bash -c command

Compare last modification dates of local and remove files

I can get a last modification date of the remote file using
curl --head http://url 2>/dev/null | grep -Po '(?<=^Last-Modified:\s).*$'
This gets me date/time such as
Wed, 04 Sep 2013 19:53:18 GMT
For local file I can use
find /path/file -exec stat \{} --printf="%y\n" \;
and it gets me date/time such as
2012-01-09 09:50:30.000000000 -0500
How can I compare this dat/time with last modification date of the local file? Please note that time zone may be different for remote and local file.
You can actually use date -d to parse the string, as #fedorqui says. Try running the below commands:
$ date -d "$(<your curl command grepped>)" +%s #+%s gets you timestamp.
$ date -d "$(<your find command>)" +%s
To actually compare, you can subtract the timestamps, something like:
$ echo $(( $(date -d "$(<curl cmd>)" +%s) - $(date -d "$(<find cmd>)" +%s) ))

Better quote to execute command on shell script

I'm in doubt of the diference and which one is the better quote to execute a command in shell script.
For example, I have this two examples:
echo "The name of the computer is `uname -n`"
echo "The name of the computer is $(uname -n)"
Which one is better? Or there is no diference?
The $(...) one is generally recommended because it nests easier. Compare:
date -d "1970-01-01 $(echo "$(date +%s)-3600"|bc) sec UTC"
date -d "1970-01-01 `echo \"\`date +%s\`-3600\"|bc` sec UTC "

Resources