Home > database >  IF command to check if file exists in SharePoint Online using Bash
IF command to check if file exists in SharePoint Online using Bash

Time:10-05

This question was closed for lack of clarity in a previous post and i was given the option to either edit or re-post the question. I decided to repost the question so others that may had seen it before could see it again but with better clarity. Thanks

i have a folder (e.g. FOLDERX) in a Sharepoint Online Document Library where i use microsoft graph API to GET files by their FILENAME

MS Graph API: https://graph.microsoft.com/v1.0/drives/{drive-id}/root:/filename.txt:/content

Note: these API can only GET files by their filename. The files were the same generic filename each day so i knew what filename to expect hence, i could easily just use the filename in the API call

i run a cron job that GET these file using cURL once a day

so for example, if the file being PUT in the folder each day is "FileA.csv", the API call my in script will look like this which then outputs the file into the remote ubuntu server where the script runs

#!/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

Now, that generic filename isn't generic anymore. the filename is now either "FileA.csv" or "FileB.csv". it's either one each day and i can't keep manually editing the filename on my API call each day.

So i was trying to write an IF statement to first check what filename is available on the Sharepoint Online folder (FOLDERX) each day so when the script is run, the API curl knows which filename to GET using the API curl

Below is where i'm at after trying different IF statement scenarios for over a week now. The API calls are working perfectly. Pls, i just need help writing an IF statement that checks the remote Sharepoint Online folder to see what filename is present and then cURL it using the API call.

#!/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

EDIT : I do not know how to write the IF statement, that's why i brought it on here.

CodePudding user response:

Just try both, using --fail to make curl let the shell know when it got a 404, 503, or similar.

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; }
  • Related