Getting date month in english format inside bash script - bash

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.

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

BASH variable in string

This script1 is not working as intended. I will explain below:
#!/bin/bash
### SETUP ###
USER="MYUSER"
DIRS="MYDIR"
BUCKET="mybucket"
DOACCESS="ACCESSKEY"
DOSECRET="SECRETKEY"
NAME="FILENAME"
EXPIRE="7 days"
NOW=$(date +"%d-%m-%Y")
DAY=$(date +"%a")
# ...lots of code that is working great...
### CLEAN OLD FILE FROM BUCKET ###
### This is the line that I am having issues with.
sh ./s3-del-old.sh ''"${BUCKET}"'/backup' '"${EXPIRE}"'
END
The script2 got copied from here.
What I had prior to following some instructions on Bash: Variable in single quote linked below was:
sh ./s3-del-old.sh "$BUCKET/backup" "$EXPIRE"
This did not work and was ignored when running the bash script.
I attempted to leave out the stuff that doesn't matter to the question below, although I believe I may have confused things. For this I apologize. Very simply, I have a line in script1 that calls another script2. I use variables to meet the needs of the script. To which it is not working and I cannot find a easy to understand solution online, thus the need to post the question.
----END OF UPDATE----
I have looked at some of the answered questions, but I am not finding a solution that fits my needs or one that I can understand fully to use for my needs.
I have tried following this, although I need a little more help.
This is what I am trying to do:
I have a backup script that uses DreamHost's DreamObjects to store my backups. The annoying part with DreamObjects is that it doesn't have any built in features for removing files created x days ago. Hence my problem. I would like to add a call to a bash file from my bash file. If that makes sense. :) If not, the code in question is below, you should be able to understand then.
I would really like to be able to add the code to my current script instead of using a separate file. I just don't know how to rewrite it properly without spending more time than I have on it. I found the code at.
My variables that matter for this problem:
BUCKET="mybucket"<br>
EXPIRE="7 days"
This is the line that calls the file:
sh ./s3-del-old.sh ''"${BUCKET}"'/backup' '"${EXPIRE}"'
This provides me with an error of date:
invalid date `-"${EXPIRE}"'
The file uses the following syntax to work:
s3-del-old "bucket" "30 days"
It does work perfectly when I use it in the command line on it's own, I just would like to add the call to one file so that I can use one cronjob instead of two. Plus, this way I can use the script with any of my domains/buckets by changing the variables. :)
The "other script" that you need to call is a bash script.
A bash script (usually) will not work if called as sh, as you are doing:
sh ./s3-del-old.sh ''"${BUCKET}"'/backup' '"${EXPIRE}"'
Please call the script with bash:
bash ./s3-del-old.sh "${BUCKET}"/backup "${EXPIRE}"
Or even better, let the script choose the shell that should run it:
./s3-del-old.sh "${BUCKET}"/backup "${EXPIRE}"
With the shebang of the file s3-del-old.sh:
#!/bin/bash
Sometimes I amaze myself at how difficult I try to make things...s3cmd has an expire function for files by create date...that will be a lot easier...
Really a big thank you to all that helped!
My bad this was suppose to be a comment not an answer. :)

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.

Date formatting error in shell script

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.

Resources