昨天(最后一个工作日)的 bash shell 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5383927/
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
bash shell script for yesterdays date (last working day)
提问by oompahloompah
I am writing a bash script that needs to print the date of the last working day. So for example if the script is run on a Monday, it will print the date for last Friday.
我正在编写一个 bash 脚本,需要打印最后一个工作日的日期。例如,如果脚本在星期一运行,它将打印上星期五的日期。
I found that this prints yesterdays date:
我发现这会打印昨天的日期:
date -d '1 day ago' +'%Y/%m/%d'
I also know that I can get the day of the week by using this statement
我也知道我可以通过使用这个语句来获取星期几
date +%w
I want to combine these two statements in order to have a little helper script that prints the required date. The logic goes something like this (note: its Pseudo code - I've never written a bash script)
我想结合这两个语句,以便有一个打印所需日期的小帮助脚本。逻辑是这样的(注意:它的伪代码 - 我从来没有写过 bash 脚本)
DAY_OF_WEEK = `date +%w`
if (%DAY_OF_WEEK == 1)
LOOK_BACK = 3
elif
LOOK_BACK = 1
fi
echo `date -d '%LOOK_BACK day ago' +'%Y/%m/%d'`
Can someone help by correcting the pseudo code above?
有人可以通过更正上面的伪代码提供帮助吗?
(I am running on Ubuntu 10.0.4)
(我在 Ubuntu 10.0.4 上运行)
回答by kevin cline
You were so close:
你是如此接近:
day_or_week=`date +%w`
if [ $day_or_week == 1 ] ; then
look_back=3
else
look_back=1
fi
date -d "$look_back day ago" +'%Y/%m/%d'
回答by Don Park
Sunday also needs to be checked.
周日也需要检查。
DAY_OF_WEEK=`date +%w`
if [ $DAY_OF_WEEK = 0 ] ; then
LOOK_BACK=2
elif [ $DAY_OF_WEEK = 1 ] ; then
LOOK_BACK=3
else
LOOK_BACK=1
fi
date -d "$LOOK_BACK day ago" +'%Y/%m/%d'
回答by Carl Norum
I'm using a Mac, so my date
command doesn't have the same -d
flag yours seems to, but the following should work if it behaves as you've indicated:
我使用的是 Mac,所以我的date
命令没有-d
你看起来的相同标志,但如果它的行为如你所指示的那样,以下应该可以工作:
if [[ $(date +%w) == 1 ]]
then
LOOK_BACK=3
else
LOOK_BACK=1
fi
date -d "${LOOK_BACK} day ago" +%Y/%m/%d
回答by Cody A. Ray
A more concise form using a bash inline "ternary" expression:
使用 bash 内联“三元”表达式的更简洁的形式:
[[ $(date +%w) == 1 ]] && days=3 || days=1
date -d "$days day ago" +"%Y-%m-%d"
回答by bryce
Putting the other answers together, I came up with this:
把其他答案放在一起,我想出了这个:
last_workday() {
from_date="${@:-today}"
day_of_week=$(date +%w --date="${from_date}")
if [ ${day_of_week} = "0" ] ; then
look_back=2
elif [ ${day_of_week} = "1" ] ; then
look_back=3
else
look_back=1
fi
date -d "${from_date} - ${look_back} day" +'%Y/%m/%d'
}
next_workday() {
from_date="${@:-today}"
day_of_week=$(date +%w --date="${from_date}")
if [ ${day_of_week} = "5" ] ; then
look_forward=3
elif [ ${day_of_week} = "6" ] ; then
look_forward=2
else
look_back=1
fi
date -d "${from_date} + ${look_forward} day" +'%Y/%m/%d'
}
for i in $(seq 16); do
now=$(date +'%Y/%m/%d' --date="today + ${i} day")
prev=$(last_workday "${now}")
next=$(next_workday "${now}")
echo "${now}: ${prev} ${next}"
done
回答by ahonnecke
For OSX (tested on 10.9.2 and 10.13.4), so probably any environment where you are using BSD date.
对于 OSX(在 10.9.2 和 10.13.4 上测试),所以可能在您使用 BSD 日期的任何环境中。
if [ $(date +%w) == 1 ] ; then
date -v-3d +'%Y/%m/%d'
else
date -v-1d +'%Y/%m/%d'
fi
You can check to see if you are using BSD date by
您可以通过以下方式检查您是否正在使用 BSD 日期
$ man date | grep "BSD General"