这个问题在前一篇文章中由于不明确而被关闭,我可以选择编辑或者重新发布这个问题。我决定重新发布这个问题,这样其他以前看过它的人就可以再次看到它,但更清晰。谢谢
我在Sharepoint文档库中有一个文件夹(例如FOLDERX),在该文档库中,我使用microsoft通过其文件名获取文件。
MS图API:https://graph.microsoft.com/v1.0/drives/{drive-id}/root:/filename.txt:/content
注意:这些API只能通过文件名来获取文件。这些文件每天都是相同的通用文件名,因此我知道预期的文件名是什么,因此,我可以很容易地在API调用中使用文件名。
我运行一个cron作业,每天使用cURL获取这些文件。
因此,例如,如果每天放在文件夹中的文件是"FileA.csv",API调用my脚本将如下所示,然后将文件输出到运行脚本的远程ubuntu服务器中。
#!/bin/bash
TOKEN=$(<curl command to retrieve the token needed for the api call>)
#api call that from sharepoint online that outputs to a csv file on remote server
curl -L -X GET "https://graph.microsoft.com/v1.0/drives/<driveID>/root:/FOLDERX/FileA.csv:/content" -H "Authorization: Bearer ${TOKEN}" > FileA.csv
现在,这个通用文件名不再是通用文件名了。文件名现在不是"FileA.csv“就是"FileB.csv”。它要么是每天一个,而且我不能每天都手动编辑API调用上的文件名。
因此,我试图编写一个IF语句,首先检查Sharepoint Online文件夹(FOLDERX)上每天可用的文件名,这样当脚本运行时,API curl就知道要使用API curl获取哪个文件名。
下面是我在尝试了一个多星期不同的IF语句场景后所处的位置。API调用运行得很好。请帮助我编写一个IF语句,检查远程Sharepoint文件夹,看看文件名是什么,然后使用API调用cURL它。
#!/bin/bash
TOKEN=$(<curl command to retrieve the token needed for the api call>)
if [ <not sure what to put in here> ];
then
curl -L -X GET "https://graph.microsoft.com/v1.0/drives/<driveID>/root:/FOLDERX/FileA.csv:/content" -H "Authorization: Bearer ${TOKEN}" > FileA.csv
else
curl -L -X GET "https://graph.microsoft.com/v1.0/drives/<driveID>/root:/FOLDERX/FileB.csv:/content" -H "Authorization: Bearer ${TOKEN}" > FileA.csv
fi
编辑:我不知道如何编写IF语句,这就是为什么我把它放在这里。
发布于 2022-10-05 06:56:39
两种方法都可以尝试,使用--fail
让curl
知道当它得到404、503或类似的消息时。
curl --fail -L -X GET "https://graph.microsoft.com/v1.0/drives/<driveID>/root:/FOLDERX/FileA.csv:/content" -H "Authorization: Bearer ${TOKEN}" >File.csv \
|| curl --fail -L -X GET "https://graph.microsoft.com/v1.0/drives/<driveID>/root:/FOLDERX/FileB.csv:/content" -H "Authorization: Bearer ${TOKEN}" >File.csv \
|| { echo "ERROR: Could not retrieve either FileA or FileB" >&2; exit 1; }
https://stackoverflow.com/questions/73962010
复制相似问题