Skip to content

Instantly share code, notes, and snippets.

@QNimbus
Last active February 17, 2020 16:27
Show Gist options
  • Save QNimbus/81ee0fba982e68eb5d9df6277df6951e to your computer and use it in GitHub Desktop.
Save QNimbus/81ee0fba982e68eb5d9df6277df6951e to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
##
## Title: setfacl.sh
## Description:
## Author: B. van wetten
## Created date: 17-02-2020
## Updated date: 17-02-2020
## Version: 0.1.0
## GitHub Gist: https://gist.github.com/QNimbus/81ee0fba982e68eb5d9df6277df6951e
##
## Usage: setfacl.sh
# Shell utilities
FIND=$(which find); [[ $? != 0 ]] && echo "Command 'find' not found" >&2 && exit 1
GETFACL=$(which getfacl); [[ $? != 0 ]] && echo "Command 'getfacl' not found" >&2 && exit 1
SETFACL=$(which setfacl); [[ $? != 0 ]] && echo "Command 'setfacl' not found" >&2 && exit 1
# Initialize variables
_PWD=$(pwd)
_ME=$(basename "${0}")
# Option strings
SHORT=hm:i:
LONG=help,minutes:,ignore:
# Get command line arguments
if [[ "${#}" -gt 0 ]]
then
getopt -T > /dev/null
if [ $? -eq 4 ]; then
# GNU enhanced getopt is available
_ARGS=$(${GETOPT} --name "$_ME" --long ${LONG} --options ${SHORT} -- "$@")
else
# Original getopt is available (no long option names, no whitespace, no sorting)
_ARGS=$(${GETOPT} ${SHORT} "$@")
fi
if [ $? -ne 0 ]; then
echo "$_ME: usage error (use -h for help)" >&2
exit 2
fi
fi
###############################################################################
# Functions #
###############################################################################
# _print_usage()
#
# Usage:
# _print_usage
#
# Print the program usage information.
_print_usage() {
cat <<HEREDOC
____ ____ ___ ____ ____ ____ _
[__ |___ | |___ |__| | |
___] |___ | | | | |___ |___
Usage:
${_ME} -h
Options:
-h Show this screen
HEREDOC
exit ${1:-0}
}
# _parse_commandline_arguments()
#
# Usage:
# _parse_commandline_arguments
#
# Parses and validates commandline arguments and populates appropriate variables.
_parse_commandline_arguments()
{
while true;
do
case "${1:-}" in
-i|--ignore)
while
if ! [[ -z "${2}" || "${2}" =~ ^-{1,2} ]]
then
_IGNORE_DEVS+=("${2}")
shift 1
fi
! [[ -z "${2}" || "${2}" =~ ^-{1,2} ]]
do
:
done
shift 1
;;
-m|--minutes)
[[ -z "${2}" || "${2}" == *[[:space:]]* || "${2}" == -* ]] && { echo "$_ME: $1 needs a value" >&2; _print_usage 1; }
_set_variable _INTERVAL $(("${2}"*60))
shift 2
;;
\?) echo "$_ME: Unknown option -$1" >&2;
exit 1
;;
:) echo "$_ME: -$1 needs a value" >&2;
exit 1
;;
*) shift
break
;;
esac
done
}
# _validate_parameters()
#
# Usage:
# _validate_parameters
#
# Performs several checks on the supplied command line arguments
_validate_parameters() {
:
}
# _set_variable()
#
# Usage:
# _set_variable variable value
#
# Sets the variable to a value if not already set. Otherwise exits with an error message
_set_variable()
{
local varname="$1"
shift
if [ -z "${!varname:-}" ]; then
eval "$varname=\"$@\""
else
echo "Error: $varname already set"
usage
fi
}
###############################################################################
# Main #
###############################################################################
# _main()
#
# Usage:
# _main [<options>] [<arguments>]
#
# Description:
# Entry point for the program, handling basic option parsing and dispatching.
_main() {
# Avoid complex option parsing when only one program option is expected.
if [[ "${@:-}" =~ -h|--help ]]
then
_print_usage
else
_parse_commandline_arguments "$@"
_validate_parameters
_FILES=$(${FIND} "${_PWD}" -type f)
_FOLDERS=$(${FIND} "${_PWD}" -type d)
while read FILE ; do
printf "%s\n\n" "$(${GETFACL} "${FILE}")"
done <<< "${_FILES}"
fi
}
# Call `_main` after everything has been defined.
_main "$@"
exit ${exitcode:-0}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment