Linux 组合两个bash命令

Linux 组合两个bash命令,linux,bash,shell,Linux,Bash,Shell,如果找到此代码 host raspberrypi | grep 'address' | cut -d' ' -f4 它给出了pi的Ip地址 还有这个 wget --post-data="PiIP=1.2.3.4" http://dweet.io/dweet/for/cycy42 它将1.2.3.4发送到dweet.io流 如何从1st获取输出以在1秒内替换1.2.3.4,请?将第一个命令的输出保存在变量中: ip=$(host raspberrypi | grep 'address' | c

如果找到此代码

host raspberrypi | grep 'address' | cut -d' ' -f4
它给出了pi的Ip地址

还有这个

wget --post-data="PiIP=1.2.3.4" http://dweet.io/dweet/for/cycy42
它将1.2.3.4发送到dweet.io流


如何从1st获取输出以在1秒内替换1.2.3.4,请?

将第一个命令的输出保存在变量中:

ip=$(host raspberrypi | grep 'address' | cut -d' ' -f4)
wget --post-data="PiIP=$ip" http://dweet.io/dweet/for/cycy42
顺便说一句,如果你的raspberrypi正在运行raspbian, 然后是一种更干净的获取IP地址的方法:

hostname -I
将命令简化为:

ip=$(hostname -I)
wget --post-data="PiIP=$ip" http://dweet.io/dweet/for/cycy42
使其成为一条直线:

wget --post-data="PiIP=$(hostname -I)" http://dweet.io/dweet/for/cycy42
更新

因此,
hostname-I
似乎为您提供了一些不同的输出。 然后,您可以使用此选项:

ip=$(hostname -I | awk '{print $1}')

要使其成为一行,您可以像我在前面的示例中那样将其插入第二行。

BTW hostname-我给我192.168.0.35 fd36:c211:4785:0:3ce3:ed63:ecfb:fe1b fd36:c211:4785:0:213:efff:fe80:2658是-它工作:)wget-post data=“PiIP=$(hostname-I | awk'{print$1}”