【问题标题】:BASH execute multiple commands which are separated by commasBASH 执行多个以逗号分隔的命令
【发布时间】:2015-03-30 00:35:19
【问题描述】:

我正在尝试逐行执行远程文件:

curl -sL 'http://remote.com/api/'$1'' | while read NAME
    do
        $($NAME) &
done

远程文件是这样的:

sleep 10; wget -O/opt/store/11.xml http://remote.com/api/11
sleep 10; wget -O/opt/store/12.xml http://remote.com/api/12
sleep 10; wget -O/opt/store/13.xml http://remote.com/api/13
sleep 10; wget -O/opt/store/14.xml http://remote.com/api/14

我想执行每一行,但我收到如下错误:

Try `sleep --help' for more information.
sleep: invalid option -- 'O'

所以睡眠得到了参数-O...

有什么想法吗?

谢谢!

【问题讨论】:

  • hm 现在我得到:./x.sh: line 3: sleep 10; wget -O/opt/store/12.xml remote.com/api/12: 没有这样的文件或目录

标签: bash while-loop sleep


【解决方案1】:

如果你可以相信远程文件中不会有恶意命令,你可以使用它:

while read -r line; do
   bash -c "$line"
done < <(curl -sL "http://remote.com/api/$1")

【讨论】:

  • 为什么不只是bash &lt;(curl -sL "http://remote.com/api/$1")curl -sL "http://remote.com/api/$1" | bash -s
  • 说得对,除非 OP 想在每行之后添加一些日志记录等,否则可以这样做并避免此处的循环。
猜你喜欢
  • 2015-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-14
  • 1970-01-01
  • 2016-01-29
  • 1970-01-01
相关资源
最近更新 更多