0

我想在脚本中运行某些命令。就像是:

if(today is monday){
  do something;
}

有没有办法在 shell 脚本中做到这一点?

4

3 回答 3

2

如果您使用的是 POSIX 兼容的 shell:

if [ "$(date +%A)" = "Monday" ]; then
    echo "something"
fi

+%A将返回当天的全名。看

date --help

更多格式选项。

于 2013-03-25T18:23:46.410 回答
1

假设一个 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 以确保完整性

于 2013-03-25T18:22:25.037 回答
-1

如果使用 Windows,您可以使用它来安排特定日期/时间的进程:

schtasks

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/schtasks.mspx?mfr=true

如果您只想查看日期,前 3 个字符%DATE%是星期几

于 2013-03-25T18:12:48.160 回答