Use output text from a command as filename

0

curl http://v6.ipv6-test.com/api/myip.php returns me my current IPv6 addr.

I'd like to use this string as part of a filename to be created, followed by timestamp of when the script ran.

Any idea how that could be done?

Hikari

Posted 2019-08-17T02:47:36.087

Reputation: 268

Answers

1

To use the result of a string as a variable, use the $(command) construct (aka command substitution):

whatevercommand >$(curl http://v6.ipv6-test.com/api/myip.php)-$(date '+%F@%T')

However, in this specific case, this is a bit dangerous since in case of network problems you can get something unexpected, so it is best to get the address separately:

myip=$(curl http://v6.ipv6-test.com/api/myip.php)
[[ $? -ne 0 ]] && exit 1
whatevercommand >$myip-$(date '+%F@%T')

In practice you can get your ipV6 address using ip address show.

xenoid

Posted 2019-08-17T02:47:36.087

Reputation: 7 552

Thanks a lot! I'm gonna try it, I'm very noob on bash scripting. My intent is to append on a flat file the datetime and current IP, so that I can log if my IP is changing. Later I'll try to store in another file the last IP found, then compare with current one, and make some notification in case it changed. – Hikari – 2019-08-21T20:04:42.860