
curl http://v6.ipv6-test.com/api/myip.php
返回我目前的 IPv6 位址。
我想使用這個字串作為要建立的檔案名稱的一部分,後面跟著腳本運行時的時間戳記。
知道如何做到這一點嗎?
答案1
要將字串的結果用作變量,請使用構造$(command)
(也稱為命令替換):
whatevercommand >$(curl http://v6.ipv6-test.com/api/myip.php)-$(date '+%F@%T')
但是,在這種特定情況下,這有點危險,因為如果出現網路問題,您可能會得到意想不到的結果,因此最好單獨獲取位址:
myip=$(curl http://v6.ipv6-test.com/api/myip.php)
[[ $? -ne 0 ]] && exit 1
whatevercommand >$myip-$(date '+%F@%T')
實際上,您可以使用 來取得您的 ipV6 位址ip address show
。