Linux 捕获 bash 命令的输出,解析它并存储到不同的 bash 变量中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18450346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:40:07  来源:igfitidea点击:

Capture output of a bash command, parse it and store into different bash variables

linuxbashshellscripting

提问by Arpith

Explanation:

解释:

I have a small bashscript which simply runs anyLinux command (e.g. say ifconfig)

我有一个小的bash脚本,它可以简单地运行任何Linux 命令(例如说ifconfig

The typical output of ifconfigis something like this:

ifconfig的典型输出是这样的:

eth0      Link encap:Ethernet  HWaddr 30:F7:0D:6D:34:CA
          inet addr:10.106.145.12  Bcast:10.106.145.255  Mask:255.255.255.0
          inet6 addr: fe80::32f7:dff:fe6d:34ca/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1104666 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2171 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:444437904 (423.8 MiB)  TX bytes:238380 (232.7 KiB)

lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.255.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:15900 errors:0 dropped:0 overruns:0 frame:0
          TX packets:15900 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:467306 (456.3 KiB)  TX bytes:467306 (456.3 KiB)

Now what most people usually do is store the entireoutput into a file/variable and parse based on that. I however want to know if there is anyway that I could put specificparts of the output in more than one variable (say a bash variablecalled ${IPETH0}to carry the IP address 10.106.145.12from eth0and ${IPLO}to carry the IP address 127.0.0.1from loin the above example without running ifconfig command twice).

现在大多数人通常做的是将整个输出存储到文件/变量中并基于此进行解析。不过,我想知道是否有反正我可以把特定零件输出的多个变量(说bash的变量称为${IPETH0}承载IP地址10.106.145.12为eth0${IPLO}承载IP地址127.0.0.1LO在上面的例子中不运行ifconfig 命令两次)。

Something like what teecommand does with the input but I want to do this for the outputand store the output into 2 or morevariables in one go. Any ideas?

类似于tee命令对输入执行的操作,但我想对输出执行此操作,并将输出一次性存储到 2 个或更多变量中。有任何想法吗?

采纳答案by Adrian Frühwirth

$ read IPETH0 IPLO <<< $(ifconfig | awk '/inet[[:space:]]/ { print  }')
$ echo "${IPETH0}"
192.168.23.2
$ echo "${IPLO}"
127.0.0.1

This assumes the order of the eth0and lointerfaces, but it shows the basic idea.

这假定了eth0lo接口的顺序,但它显示了基本思想。

回答by anubhava

You can use awk and bash arrays:

您可以使用 awk 和 bash 数组:

arr=( $(awk -F ':' ' == "inet addr"{sub(/ .*/, "", ); print }' < <(ifconfig)) )

Then you can do:

然后你可以这样做:

read IPETH0 IPLO <<< ${arr[@]}

回答by michael501

you can read each line of ifconfig and set variables :

您可以读取 ifconfig 的每一行并设置变量:

while read l1 ;do 
   if [[ $l1 =~ inet ]];then     
      set -- $l1 
      echo  "ip is    " 

   fi
done < <(ifconfig)