-
-
Save gnanet/e26356c616854c6e7f29c0ca14838a27 to your computer and use it in GitHub Desktop.
bash function to compare if a file is older than 28 hours or not
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#######BEGIN SCRIPT############ | |
#!/bin/bash | |
# This checks that the specified file is less than 28 hours old. | |
# returns 0 if younger than 28 hours. | |
# returns 1 if older than 28 hours. | |
#funtion arguments -> filename to comapre against curr time | |
function comparedate() { | |
if [ ! -f $1 ]; then | |
echo "file $1 does not exist" | |
exit 1 | |
fi | |
MAXAGE=$(bc <<< '28*60*60') # seconds in 28 hours | |
# file age in seconds = current_time - file_modification_time. | |
FILEAGE=$(($(date +%s) - $(stat -c '%Y' "$1"))) | |
test $FILEAGE -lt $MAXAGE && { | |
echo "$1 is less than 28 hours old." | |
return 0 | |
} | |
echo "$1 is older than 28 hours seconds." | |
return 1 | |
} | |
#sample usage of function test if file /tmp/test.sh is older than 28 hours or not | |
comparedate /tmp/test.sh | |
#######END_SCRIPT######### |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment