Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH check if today is 1st day of month

Tags:

date

bash

I have a bash script and I need it to fulfill some conditions if it is 1st day of month.
I have written this code

ifStart=`date '+%d'`
if [$ifStart == 01]
then
test=`/bin/date --date='1 day ago' +'%Y-%m'`
echo $test
fi

I expect it to show 2013-03 today, but I get an errormessage:
Line 2 command not found.

test=`/bin/date --date='1 day ago' +'%Y-%m'`

this part works well without if.
Any suggestions?

like image 423
erizo Avatar asked Apr 01 '13 13:04

erizo


People also ask

How do I get the day of the month in bash?

Bash Date Format MM-DD-YYYY To format date in MM-DD-YYYY format, use the command date +%m-%d-%Y . Please observe the upper and lower case letters : %m for month, %d for day and %Y for year.

How to get number of days in a month in shell script?

Assigning number of days in the month to a variable In the shell, I can use the command: cal `date +%m` `date +%Y`| grep -v '' | wc -w to give me the number of days in the month, but when I assign it to a variable: VAR=`cal `date +%m` `date +%Y`| grep -v '' | wc...

How do I get the last day of the month in Unix?

You have to actually call date twice to get the last day of last month. Here is how: $ date -d "$(date +%Y/%m/01) - 1 day" "+%Y/%m/%d" 2016/03/31.


1 Answers

The command that's not being found is actually due to your if statement. You need spaces:

if [ $ifStart == 01 ]

Otherwise [$ifStart will be interpreted as a command.

like image 59
FatalError Avatar answered Sep 24 '22 02:09

FatalError