我有一个cronjob使用curl将http帖子发送到我的home-assistant.io服务器,然后使用google_say让我的Google Home告诉人们在早上开始准备......有点乐趣。 :)
它的效果很好但是当试图添加一些动态内容时,例如说出星期几,我正在努力使用curl中的日期构造。我还想确定周末之前的天数。我尝试过以下方法:
"message": "Its "'"$(date +%A)"'" morning and x days until the weekend. Time to get ready."
但是收到错误说:
<html><head><title>500 Internal Server Error</title></head><body><h1>500 Internal Server Error</h1>Server got itself in trouble</body></html>
我认为“'”$(日期+%A)“'”在这种情况下应该有效吗?此外,我想补充一下周末的天数,可能是这样的:
6 - $(date +%u)
我很欣赏我可以通过在卷曲和引用之前进行一些计算来很容易地做到这一点,但如果可能的话,我想在一行中完成。该行目前从.sh文件引用,而不是cron中的单行。
这是所要求的完整一行:
curl -X POST -H "x-ha-access: apiPass" -H "Content-Type: application/json" -d '{"entity_id": "media_player.Living_room_Home", "message": "Its "'"$(date +%A)"'" morning and 2 days until the weekend. Time to get ready."}' http://ipAddr:8123/api/services/tts/google_say?api_password=apiPass
感谢。
答案 0 :(得分:2)
这条线完全正常:
curl --trace-ascii 1 -X POST -H "x-ha-access: apiPass" -H "Content-Type: application/json" -d '{"entity_id": "media_player.Living_room_Home", "message": "Its '$(date +%A)' morning and 2 days until the weekend. Time to get ready."}'
结果:
== Info: Trying ::1...
== Info: TCP_NODELAY set
== Info: Connected to localhost (::1) port 80 (#0)
=> Send header, 197 bytes (0xc5)
0000: POST /api/services/tts/google_say?api_password=apiPass HTTP/1.1
0041: Host: localhost
0052: User-Agent: curl/7.50.3
006b: Accept: */*
0078: x-ha-access: apiPass
008e: Content-Type: application/json
00ae: Content-Length: 130
00c3:
=> Send data, 130 bytes (0x82)
0000: {"entity_id": "media_player.Living_room_Home", "message": "Its T
0040: uesday morning and 2 days until the weekend. Time to get ready.
0080: "}
== Info: upload completely sent off: 130 out of 130 bytes
<= Recv header, 24 bytes (0x18)
0000: HTTP/1.1 404 Not Found
<= Recv header, 28 bytes (0x1c)
0000: Server: Microsoft-IIS/10.0
<= Recv header, 37 bytes (0x25)
0000: Date: Tue, 07 Nov 2017 21:12:21 GMT
<= Recv header, 19 bytes (0x13)
0000: Content-Length: 0
<= Recv header, 2 bytes (0x2)
0000:
== Info: Curl_http_done: called premature == 0
== Info: Connection #0 to host localhost left intact
答案 1 :(得分:1)
这会有帮助吗?
echo $(( $(date -d 'next saturday' +%j) - $(date +%j) - 1 )) days until the weekend
GNU日期中的-d选项可让您提供所需日期的灵活描述。