我收到以下命令:
curl -H 'Content-Type: application/json' -X POST -d '{"host": "'$(hostname)'"}' http://sitename.com/update.php
这按预期工作,但如果我尝试发送正常运行时间输出而不是主机名我得到:
curl: (6) Could not resolve host: 19:12; Name or service not known
curl: (6) Could not resolve host: up; Name or service not known
curl: (7) Failed to connect to 0.0.0.4: Invalid argument
curl: (6) Could not resolve host: days,; Name or service not known
curl: (6) Could not resolve host: 5:57,; Name or service not known
curl: (7) Failed to connect to 0.0.0.3: Invalid argument
curl: (6) Could not resolve host: users,; Name or service not known
curl: (6) Could not resolve host: load; Name or service not known
curl: (6) Could not resolve host: average; Name or service not known
curl: (6) Could not resolve host: 0.07,; Name or service not known
curl: (6) Could not resolve host: 0.05,; Name or service not known
curl: (3) [globbing] unmatched close brace/bracket at pos 6
很明显,这是由空间引起的,但我怎样才能摆脱它们呢?
我可以删除空格awk:
curl -H 'Content-Type: application/json' -X POST -d '{"uptime": "'$(uptime | awk '{print $3$4}')'"}' http://sitename.com/update.php
它给了我“4 天”,但必须有更好的解决方法:D
答案1
使用一种类型的引用更简单并且可以解决该问题;
curl -H "Content-Type: application/json" -X POST -d "{\"uptime\": \"$(uptime)\"}" "http://sitename.com/update.php"
或者你可以使用 2 种引用类型,但它不太优雅;
curl -H 'Content-Type: application/json' -X POST -d '{"uptime": "'"$(uptime)"'"}' 'http://sitename.com/update.php'