我想在脚本中运行某些命令。就像是:
if(today is monday){
do something;
}
有没有办法在 shell 脚本中做到这一点?
如果您使用的是 POSIX 兼容的 shell:
if [ "$(date +%A)" = "Monday" ]; then
echo "something"
fi
+%A将返回当天的全名。看
date --help
更多格式选项。
假设一个 bash 脚本:
#!/bin/bash
# Use the date command to get the numerical representation of the week
# (or whatever format you are looking for)
check=$(date +%u)
if [ "$check" == 1 ]; then
# Monday
# Do something
elif [ "$check" == 2 ]; then
# Monday
# Do something
...
elif [ "$check" == 7 ]; then
# Sunday
# Do something
fi
编辑 - 添加 bin/bash 以确保完整性
如果使用 Windows,您可以使用它来安排特定日期/时间的进程:
schtasks
如果您只想查看日期,前 3 个字符%DATE%
是星期几