简体   繁体   中英

Combining two bash commands

If found this code

host raspberrypi | grep 'address' | cut -d' ' -f4

which gives pi Ip address

and this

wget --post-data="PiIP=1.2.3.4" http://dweet.io/dweet/for/cycy42

which sends 1.2.3.4 off to dweet.io stream

How can I get the output from 1st to replace the 1.2.3.4 in second please?

Save the output of the first command in a variable:

ip=$(host raspberrypi | grep 'address' | cut -d' ' -f4)
wget --post-data="PiIP=$ip" http://dweet.io/dweet/for/cycy42

Btw, if your raspberrypi is running raspbian, then a much cleaner way to get the IP address:

hostname -I

Simplifying the commands to:

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

Making that a one-liner:

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

UPDATE

So it seems hostname -I gives a bit different output for you. You can use this then:

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

To make it a one-liner, you can insert this into the second line just like I did in the earlier example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM