Date formatting error in shell script - shell

I am trying to run the below command in Unix. It doesn't work for the below date:-
$ date "+%m%d%Y" -d "09-Mar-2014 02:06:28"
date: invalid date `09-Mar-2014 02:06:28'
When i change the command slightly, it works.
$ date "+%Y%m%d" -d "09-Mar-2014 03:06:28"
20140309
Can some one explain as to why this is so and is there any solution for this?

The above command works fine without any issues in my HP UX B.11.11 version.
I was able to retrieve in the problematic date format too.

Related

How to get Japan time in shell script file in windows?

I want to get Japan time in my shell script. I am using this command
DATE=`TZ=Japan date +"%Y%d%m%H%M"`
echo "Japan Date - "$DATE
This is working on online shell script execution. But if I try this in my .sh file, its giving wrong datetime.
Kindly suggest on the same.
The code which I mentioned above works fine in UNIX environment. I was executing it in windows environment which is not correct. The identifier "TZ=Japan" only identified by UNIX environment. So once again, below code works fine
DATE=TZ=Japan date +"%Y%d%m%H%M"
echo "Japan Date - "$DATE
Use this command to get the GMT time:
$date=[System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId($(Get-Date), [System.TimeZoneInfo]::Local.Id, 'GMT Standard Time')
And this one to get Japanese time and add 9 hours to the time:
$TimeRaised = ([datetime]$date).AddHours(9).ToString('dd/MM/yyyy HH:mm')

Way to set date automatically from command line

I make a script in a command line, but I have a problem. I need change date automatically from a command line. Is it possible?
I know, I can use static date for example:
date 0101122315
but I need an automatic date from the internet in macOs.
You've probably got some research to do, here is one possible outline:
Use curl to have the date from the internet and store in a file or send to standard output - in the latter case you may pipe into more commands or store in a shell variable.
If needed: Use sed, awk, grep, some JSON editor, etc. to extract the date from the result of (1). Not needed if your URL just returns the date and nothing else.
Use date and shell variable or command output substitution to pass your date as an argument.
You can find documentation on all the commands, and the shell, using the man command, e.g. man curl.
You might end up with a short one line script (something like:
date `curl URL | sed command`
) or something longer. Nobody can really tell you as it is unstated in the question what the URL will return.
If you get stuck once you're further along ask a new question showing what you've developed and explains the error and someone will undoubtedly help you progress.
Have fun!
Try this command
sudo sntp -sS time.apple.com

Setting the current date into a variable in a Script in bash

So for the life of me I cannot figure out why my script will not take my date command as a variable. I have a script that is run every time a message is received and procmail filters specific messages by their subject line. The script looks like this:
d=$(date +%Y%m%d)
:0 wc
* ^(From|subject).*xxx
| cat&>/xx/xx/xx/xx/MSG:$d && \
chmod xxx /xx/xx/xx/xx/MSG:$d && \
/xx/xx/xx/otherscript.sh /xx/xx/xx/xx/MSG:$d
I have run the date command plenty of times in other scripts and to stdout without any issue, so I am wondering if this is a procmail issue? I have looked at a few different sites about this but still have not found a solution. My end goal is to create unique file names as well as for organization purposes each time a new email comes in.
The other reason for me believing it has something to do with procmail is that it was working fine just 3 months ago (didn't change any files or permissions). I have even tried several variations (only showing a few examples):
$'date +"%Y%m%d"'
$(date)
echo $(date)
I get a variety of files created ranging with it printing MSG:(date), MSG:(date ,etc. MSG:(date appears to like it tries to read the variable but is getting cut off or the space between date and + is causing an issue.
And at the end of my script I send it to another script which also creates a new file with the date appended and it works just fine:
fileOut="/xxx/xxx/xxx/xxx.$v.$(date +"%Y%m%d-%H%M%S").xxx"
prints: xxx.YH8AcFV9.20160628-090506.txt
Thanks for your time :-)
Procmail does not support the modern POSIX shell command substitution syntax; you need to use backticks.
d=`date +%Y%m%d` # or just date +%F
If you want to avoid invoking an external process, the From_ pseudo-header contains a fresh date stamp on many architectures.

Getting date month in english format inside bash script

In a bash script I have the following:
MES=$(date +"%b")
How can I get the month in english format?
Now if I echo $MES variable, I get abr. But I would like to get apr.
I'm trying to solve this without using if statement or switch. May be is an option...
I have tried date -u but is not working for me.
EDIT:
Finally I have put this line in the first line of script script:
#!/bin/bash
LANG=en_us_8859_1
# Here rest of the script
Now is working, but I can't accept my own answer as valid... I think because I haven't enough reputation in stackoverflow
Use Command Substitution
MES=$(LANG=en_us_88591; date +"%b")
to change the language just for this single call.

I want my bash program sees a date different from the actual

I need a bash script that sets a date for my program. The program must work with a date different from the current one. Is it possible? With:
#!/bin/sh
date 122511462014.30 && myprogram
I get the following message error
date: cannot set date: Operation not permitted
because my script runs with no root privileges.
You can't do it like that. date changes the date for the entire system.
You need something like libfaketime

Resources