操纵一年中的某一天

Manipulating the day of the year

我想得到一年中的第几天,然后减去 1,然后 mod 减去 3。它是 运行 我的脚本在 crontab 中的一个开关。

if (((date(%j) -1) %3))
 echo 'hello'
 exit()
else
 echo hi
fi

这只是为了测试它是否正常工作,但是我收到了

./getdate.sh: line 4: syntax error near unexpected token `fi'
./getdate.sh: line 4: `fi'

这对你们中的一些人来说可能是微不足道的,但我对 bash 还是很陌生。

谢谢

这就是我认为你想要做的事情:

#!/bin/bash

if (( ( $(date +%-j) - 1 ) % 3 ))
then
  echo 'hello'
  exit
fi

echo hi
  • 总是使用 shebang。

  • 执行 date 并使用它打印的内容将其包装在 $().

  • 命令的参数,在本例中为 date,只需遵循命令(和 space);没有括号。

  • date 想要 + 作为格式前缀。

  • %j 将打印前导零,Bash 表示该数字是八进制的。使用 %-j 不会得到这样的零。

  • 我想你想在模数之前减去:使用括号进行排序。

  • if 需要 then.

  • exit 不应该有括号。

  • exit后不需要else