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?
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.
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...
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With