【问题标题】:Execute command from a file each second每秒从文件执行命令
【发布时间】:2019-04-03 12:06:09
【问题描述】:

我有一个包含一堆不同 curl 命令的文件。例如 curls.txt:

curl http:/localhost:7070/a
curl http:/localhost:7070/b
curl http:/localhost:7070/c
curl http:/localhost:7070/d
curl http:/localhost:7070/e
curl http:/localhost:7070/f
curl http:/localhost:7070/g

如何运行此文件中的每个命令但延迟 1 秒?

【问题讨论】:

  • 好的,在每一行后面加上sleep 1
  • 有什么意义?你想做什么?

标签: bash macos curl


【解决方案1】:

是的,你可以在 bash 中这样做:

while read cmd; do echo "$cmd" ; $cmd ; sleep 1 ; done < curls.txt

【讨论】:

  • 它每 1 秒执行一次,但会引发错误:c 我也尝试了您预先编辑的每个答案:D curl http://localhost:7070/a zsh: no such file or directory: curl http://localhost:7070/a curl http://localhost:7070/b zsh: no such file or directory: curl http://localhost:7070/b curl http://localhost:7070/c zsh: no such file or directory: curl http://localhost:7070/c
  • 您是否尝试使用引号来执行它?那行不通……
  • 它似乎只适用于 Bash(不是 Zsh)。它应该没有 $cmd 周围的引号,否则 curl 参数将无法正确理解
【解决方案2】:

是否有理由将它们放在一个文件中?

for x in a b c d e f g
do curl http:/localhost:7070/$x; sleep 1
done

【讨论】:

    【解决方案3】:

    正如马克所说,我在每一行之后添加了 sleep 1 并执行了一个文件:bash curls.txt。奇迹般有效。谢谢:)

    【讨论】:

      猜你喜欢
      • 2016-11-29
      • 1970-01-01
      • 2022-12-17
      • 2016-08-11
      • 2017-04-03
      • 1970-01-01
      • 1970-01-01
      • 2013-11-23
      • 1970-01-01
      相关资源
      最近更新 更多