Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何使用bash在一系列日期中查找一天中的特定天数?_Linux_Bash_Shell_Ubuntu_Redhat - Fatal编程技术网

Linux 如何使用bash在一系列日期中查找一天中的特定天数?

Linux 如何使用bash在一系列日期中查找一天中的特定天数?,linux,bash,shell,ubuntu,redhat,Linux,Bash,Shell,Ubuntu,Redhat,我想用bash搜索一系列日期中的第三个星期四 这是我的代码: #!/bin/bash start=2018-03-23 end=2018-04-22 while ! [[ $start > $end ]]; do echo $start start=$(date -d "$start+third Thursday" +%F) done 看起来最后一部分“$start+第三个星期四”运行不正常 但单独使用date和“第三个星期四”可以: date -d "third Thu

我想用bash搜索一系列日期中的第三个星期四

这是我的代码:

#!/bin/bash
start=2018-03-23
end=2018-04-22
while ! [[ $start > $end ]]; do
    echo $start
    start=$(date -d "$start+third Thursday" +%F)
done
看起来最后一部分“$start+第三个星期四”运行不正常

但单独使用date和“第三个星期四”可以:

date -d "third Thursday" +%F

此代码查找给定日期后的第三个星期四

#!/bin/bash
start=2018-04-24
thursdayOfCurrentWeek=$((4-$( date +%u --date $start )))
if [[ $thursdayOfCurrentWeek -ge 0 ]]
then
    date -d "$start + $thursdayOfCurrentWeek days + 2 weeks" +%F
else
    date -d "$start + $thursdayOfCurrentWeek days + 3 weeks" +%F
fi
让我们把它分开:

date +%u --date $start
这将把
$start
的工作日作为一个数字返回(1=星期一,…,4=星期四)

这将计算要添加到
$start
日期的天数,以获得当前星期四

if [[ $thursdayOfCurrentWeek -ge 0 ]]
如果星期四的日期是今天或本周晚些时候,那么只需在下一个星期四的基础上再加两周,因为它已经是下三个星期四之一了

date -d "$start + $thursdayOfCurrentWeek days + 2 weeks" +%F
# or
date -d "$start + $thursdayOfCurrentWeek days + 3 weeks" +%F
添加或减去
$start
日期以获得当前星期的星期四,并根据本周的星期四的时间添加2周或3周

如果
$start
日期是星期四,并且不应算作三个日期之一,只需将If语句替换为:

if [[ $thursdayOfCurrentWeek -gt 0 ]]
这是一个更一般的答案(因为开始日期不必是星期五),然后是您要寻找的,但它也适用于您的用例

希望这有帮助

PS您不需要
$end
日期

编辑:

如果您确实想要最低版本,如果
$start
始终为星期五,则可以执行以下操作:

#!/bin/bash
start=2018-03-23
date -d "$start - 1 day + 3 weeks" +%F

请阅读如何使用。另外,提出一个问题或描述任何你自己无法解决的问题。我改进了我的初始问题。它有效,因为$date是“”,因此你有
date-d“+第三个星期四”+%F
。对不起,你是对的。有人能帮我吗?谢谢,有什么更简单的:
date%F-d吗“$startdate+4周下周四”
@user2637202这根本不起作用。你不能将
下周四
与给定日期结合起来。@user2637202我为你的特定情况添加了一个解决方案,其中开始日期总是周五。
#!/bin/bash
start=2018-03-23
date -d "$start - 1 day + 3 weeks" +%F