Skip to content

Instantly share code, notes, and snippets.

@petergi
Created April 5, 2023 13:00
Show Gist options
  • Select an option

  • Save petergi/30c76e91f74cd685fbfc090b53b194c6 to your computer and use it in GitHub Desktop.

Select an option

Save petergi/30c76e91f74cd685fbfc090b53b194c6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
LOG_FILE="appPrint.log"
. "Check ownership and permissions of a file.sh"
function log()
{
echo "BLAH_BLAH - `date` - $@" >> "$LOG_FILE"
}
# Create logfile with appropriate permissions
function checkLogFile() {
if [ ! -f "$LOG_FILE" ]; then
touch "$LOG_FILE"
chown "$USER" "$LOG_FILE"
chmod 600 "$LOG_FILE"
else
# Check permissions of $LOG_FILE
checkPermissions "$LOG_FILE" "$USER" "-rw-------"
if [ "$?" -ne 0 ]; then
# Try fixing permissions of LOG_FILE
log "ALERT: Incorrect permissions for the $LOG_FILE, trying to fix permissions of $LOG_FILE!"
chown "$USER" "$LOG_FILE"
chmod 600 "$LOG_FILE"
fi
fi
log "$LOG_FILE permissions OK"
return 0
}
checkLogFile
if [ "$?" -ne 0 ]; then
log "ALERT: Incorrect permissions for the $LOG_FILE, aborting!"
exit -1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment