Converting list of dates/times from GMT to PST

If I run the command by itself it seems to be working fine. As example using:

date --date='TZ="PST" Sun Jan 01 05:00:10 2017' 

Sat Dec 31 21:00:10 PST 2016

However, I'm trying to use an input file with a list of dates/times and having issues with all the quotes and can't seem to get it working. My input file (example name dates.input looks like:

$ cat dates.input Sun Jan 01 06:49:33 2017 Sun Jan 01 05:44:17 2017 Sun Jan 01 05:43:23 2017 Sun Jan 01 05:39:13 2017 Sun Jan 01 05:00:10 2017 

The command I'm having issues with is:

while read i; do "date --date='TZ="PST" ${i}'"; done < dates.input 

which gives

bash: date --date='TZ=PST Sun Jan 01 06:49:33 2017': command not found... bash: date --date='TZ=PST Sun Jan 01 05:44:17 2017': command not found... bash: date --date='TZ=PST Sun Jan 01 05:43:23 2017': command not found... bash: date --date='TZ=PST Sun Jan 01 05:39:13 2017': command not found... bash: date --date='TZ=PST Sun Jan 01 05:00:10 2017': command not found... 

Anyone have any suggestions on how to get this working?

Thanks

1 Answer

As an example, let's pick this value for i:

$ i='Sun Jan 01 06:49:33 2017' 

Now, let's run the command in your loop:

$ "date --date='TZ="PST" ${i}'" bash: date --date='TZ=PST Sun Jan 01 06:49:33 2017': command not found 

As one can see, putting quotes around a command and its arguments confuses the shell. Try instead:

$ date --date="TZ=\"PST\" ${i}" Sat Dec 31 22:49:33 PST 2016 

The above succeeds.

After placing this command within the loop, the loop now works:

$ while read i; do date --date="TZ=\"PST\" ${i}"; done < dates.input Sat Dec 31 22:49:33 PST 2016 Sat Dec 31 21:44:17 PST 2016 Sat Dec 31 21:43:23 PST 2016 Sat Dec 31 21:39:13 PST 2016 Sat Dec 31 21:00:10 PST 2016 
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like