Skip to content

Instantly share code, notes, and snippets.

@codarrenvelvindron
Last active February 19, 2021 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codarrenvelvindron/869f841a5326f92dbbe32987bbff4c2d to your computer and use it in GitHub Desktop.
Save codarrenvelvindron/869f841a5326f92dbbe32987bbff4c2d to your computer and use it in GitHub Desktop.
#!/bin/bash
#Check modified date of file and return the time elapsed between modification and current time
#Limiting API curl requests with bash
#By Codarren Velvindron
#Credits: https://stackoverflow.com/questions/1819187/test-a-file-date-with-bash
CWD="$( cd "$(dirname "$0")" ; pwd -P )"
file="$CWD/myfile.txt"
API_Request='curl -G -H "your_api_key:xxxxxxxxxxxx" https://your.api.url/your/endpoint -o $file'
threshold='100'
NOW=$(date +%s)
MOD=$(date --reference="$file" +%s)
echo "#Checking file at location $file"
delta=$(($NOW-$MOD))
echo "--> $file was modified $delta secs ago"
if [[ $delta -lt $threshold ]]; then
echo "--> File didnt exceed threshold-$threshold, exiting"
elif [[ $delta -ge $threshold ]]; then
echo "--> File exceeded threshold-$threshold, Launch the request!"
$API_Request
else
echo "!! EXCEPTION caught!"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment