linux指定时间运行脚本设置,linux – 如何在指定的时间段内在bash脚本中运行命令?...

比方说,只有当前时间是从上午11点10分到下午2点30分才能运行“命令”.如何在bash脚本中完成此操作?

下面用伪语言写的东西:

#!/bin/bash

while(1) {

if ((currentTime > 11:10am) && (currentTime <2:30pm)) {

run command;

sleep 10;

}

}

解决方法:

其他答案忽略了当一个数字从0开始时,Bash将在基数8†中解释它.因此,例如,当它是上午9点时,日期’%H%M’将返回0900,这是Bash中的无效数字. (不再).

一个适当而安全的解决方案,使用现代Bash:

while :; do

current=$(date '+%H%M') || exit 1 # or whatever error handle

(( current=(10#$current) )) # force bash to consider current in radix 10

(( current > 1110 && current < 1430 )) && run command # || error_handle

sleep 10

done

如果您接受第一次运行的潜在10秒延迟,可以缩短一点:

while sleep 10; do

current=$(date '+%H%M') || exit 1 # or whatever error handle

(( current=(10#$current) )) # force bash to consider current in radix 10

(( current > 1110 && current < 1430 )) && run command # || error_handle

done

完成!

†看:

$current=0900

$if [[ $current -gt 1000 ]]; then echo "does it work?"; fi

bash: [[: 0900: value too great for base (error token is "0900")

$# oooops

$(( current=(10#$current) ))

$echo "$current"

900

$# good :)

正如xsc在评论中指出的那样,它适用于古代[内置…但这已成为过去:).

标签:bash,linux

来源: https://codeday.me/bug/20190725/1532572.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值