BASH 检查今天是否是一个月的第一天

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15744781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:03:05  来源:igfitidea点击:

BASH check if today is 1st day of month

bashdate

提问by erizo

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

我有一个 bash 脚本,如果是一个月的第一天,我需要它来满足某些条件。
我已经写了这段代码

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.

我希望它今天显示 2013-03,但我收到一条错误消息:
找不到第 2 行命令。

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

this part works well without if.
Any suggestions?

这部分在没有 if 的情况下运行良好。
有什么建议?

回答by FatalError

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

未找到的命令实际上是由于您的 if 语句造成的。你需要空格:

if [ $ifStart == 01 ]

Otherwise [$ifStartwill be interpreted as a command.

否则[$ifStart将被解释为命令。