在 LaunchAgents 中包含当前日期作为程序参数

Include current date as program argument in LaunchAgents

假设我想对包含当前日期的文件执行某些操作。在 bash 提示符下,我可以这样做:

$ touch /Foo/$(date +%Y-%m-%d)

我怎么能在没有 $() 可用的 LaunchAgents plist 中做到这一点?

<key>ProgramArguments</key>
<array>
  <string>touch</string>
  <string>/Foo/CURRENT-DATE-HERE</string>
</array>

一种可能是让它启动 shell 进行扩展,然后 运行 真正的命令:

<key>ProgramArguments</key>
<array>
  <string>bash</string>
  <string>-c</string>
  <string>touch /Foo/$(date +%Y-%m-%d)</string>
</array>

请注意,整个命令作为单个参数传递给 bash,然后由于嵌入的 space,它拆分为命令与参数。如果它是一个长 运行ning 命令,您可能希望使用 exec touch /Foo/$(date +%Y-%m-%d) 以便 shell 将自己替换为命令而不是 运行ning 命令作为子进程,然后挂出等待它退出。