Skip to content

Instantly share code, notes, and snippets.

@sebastianw
Last active August 6, 2025 09:19
Show Gist options
  • Select an option

  • Save sebastianw/0f59221a3ed6bc706b36bbef9a739701 to your computer and use it in GitHub Desktop.

Select an option

Save sebastianw/0f59221a3ed6bc706b36bbef9a739701 to your computer and use it in GitHub Desktop.
Bash: Wait for JAMF Admin Privileges before running command
###########################
## Color /Output Helpers ##
###########################
# These might not all be used
# shellcheck disable=2034
{
## Color Helpers
TCOLOR_NONE="\033[0m" # Reset Colors
TCOLOR_BOLD="\033[1m"
TCOLOR_DIM="\033[2m"
TCOLOR_UNDER="\033[4m"
TCOLOR_BLINK="\033[5m" # Won't work in most terminals
TCOLOR_REV="\033[7m" # Reverse
TCOLOR_HID="\033[8m" # Hidden
}
# 256-Color code output $(TCOLOR_EXT <color> <set if background>)
function TCOLOR_EXT () {
local fgbg
if [ -z "$2" ]; then
# Foreground
fgbg=38
else
# Background
fgbg=48
fi
echo -ne "\033[${fgbg};5;${1}m"
}
# Wrap string in color. $(TC_WRAP <color> <string>)
function TC_WRAP () {
echo -ne "$(TCOLOR_EXT "$1")${2}${TCOLOR_NONE}"
}
function colortest() {
local fgbg color
for fgbg in 38 48 ; do # Foreground / Background
for color in {0..255} ; do # Colors
# Display the color
printf "\e[${fgbg};5;%sm %3s \e[0m" "$color" "$color"
# Display 6 colors per lines
if [ $(((color + 1) % 6)) == 4 ] ; then
echo # New line
fi
done
echo # New line
done
}
function perror() {
printf "$(TCOLOR_EXT 1)${TCOLOR_BOLD}ERROR: %s${TCOLOR_NONE}\n" "$*" >&2
}
function pwarn() {
printf "$(TCOLOR_EXT 3)${TCOLOR_BOLD}WARNING: %s${TCOLOR_NONE}\n" "$*" >&2
}
function pinfo() {
printf "$(TCOLOR_EXT 2)${TCOLOR_BOLD}INFO: %s${TCOLOR_NONE}\n" "$*" >&2
}
##############################################
## Actual function to wait for Admin Rights ##
####################ää########################
function _wait_admin () {
if [ $# -eq 0 ]; then
perror "Usage: _wait_admin <command>"
return 1
fi
local ids
ids=$(id -G -n)
if ! [[ $ids =~ (^|[[:space:]])admin([[:space:]]|$) ]]; then
pinfo "Please request Admin Privileges..."
fi
until [[ $ids =~ (^|[[:space:]])admin([[:space:]]|$) ]]; do
sleep 1
ids=$(id -G -n)
done
"$@"
}
alias sudo="_wait_admin sudo"
alias mtr='sudo mtr --mpls --order "LSRDNABWVMX"'
@sebastianw
Copy link
Author

Put this into .bashrc/.bash_aliases or wherever your bash init stuff is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment