【问题标题】:How can i hardcode array in shell script? [closed]如何在 shell 脚本中对数组进行硬编码? [关闭]
【发布时间】:2014-07-16 19:41:19
【问题描述】:

我想在一组主机上运行命令ping host 并将所有输出保存到一个文件中。 我尝试编写一个 shell 脚本,但没有成功。谁能帮我写一个单行来做到这一点?

【问题讨论】:

  • ping example.com > ping_results.txt
  • 请出示你的非工作脚本,我们会看看它有什么问题。
  • 我在 /tmp/hosts.txt pastebin.com/42w79ipD中有值

标签: linux bash shell unix sh


【解决方案1】:

你可以使用简单的while read循环:

while read -r line; do
   ping "$line"
done < /etc/hosts > ping.out

【讨论】:

    【解决方案2】:

    试试这个

    (echo google.com; echo oracle.com; cat hosts.txt) | xargs -n1 ping -c1 | tee output.txt
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多