我有一个每分钟运行的脚本,我想检查当前分钟是否包含在从文本文件中分配的变量中。
clip_time =(< /home/project/data/clip_time.cfg)
clip_time.cfg是一个文本文件,如下所示: 00,05,10,15,20,25,30,35,40,45,50,55
在if语句中: 如果当前分钟来自" date' +%M'"是变量,比如" 45"然后 这样做,如果没有,请这样做
如果分钟为45,则会触发,但不会触发46,47,48,49。但触发50分。
换句话说,每分钟运行一次脚本,如果分钟在变量中,如果不是睡眠30秒并继续,请执行此操作。
变量不一定是每5分钟一次。可能是05,08,11,22,35。
答案 0 :(得分:0)
要将当前分钟存储在变量中,请执行此操作
minute=$(date +"%M");
然后要搜索,您可以使用grep
echo $(grep $minute file.txt);
以下是grep的手册页
答案 1 :(得分:0)
你可以像这样使用grep和date:
grep "\b`date "+%M"`\b" test
带有单词边界,甚至可以添加-q标志来返回0或1,如下所示:
grep -q "\b`date "+%M"`\b" test
答案 2 :(得分:0)
一种方法是检查clip_time字符串是否包含当前分钟作为子字符串:
clip_time="$(cat /home/project/data/clip_time.cfg)"
minute="$(date +%M)"
# The 2 asterisks make this a glob match, which should find that
# minute in the list, if it's in there
if [[ "$clip_time" == *"$minute"* ]]; then
echo "$minute is in the list"
else
echo "$minute is NOT in the list"
fi