Skip to content
Advertisement

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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement