gpt4 book ai didi

linux - 在 Linux bash curl 中使用日期计算来获取星期几

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:45:31 24 4
gpt4 key购买 nike

我有一个 cronjob,它使用 curl 将 http post 发送到我的 home-assistant.io 服务器,而 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>

我认为 "'"$(date +%A)"'"应该在这种情况下工作是错误的吗?另外我想添加距离周末还有多少天,可能是这样的:

6 - $(date +%u)

我很感激我可以通过在 curl 之前进行一些计算并引用这些计算来非常轻松地完成此操作,但如果可能的话我想在一行中完成。该行目前是从 .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

谢谢。

最佳答案

这条线完美运行:

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

关于linux - 在 Linux bash curl 中使用日期计算来获取星期几,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47165637/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com