Skip to content

Instantly share code, notes, and snippets.

@leoheck
Forked from palmerc/userpic.sh
Created November 2, 2021 15:21
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 leoheck/f565c1c6de005d17049304edeebf1183 to your computer and use it in GitHub Desktop.
Save leoheck/f565c1c6de005d17049304edeebf1183 to your computer and use it in GitHub Desktop.
Updating a user's picture from a script
#!/bin/bash
set -e
declare -x USERNAME="$1"
declare -x USERPIC="$2"
declare -r DSIMPORT_CMD="/usr/bin/dsimport"
declare -r ID_CMD="/usr/bin/id"
declare -r MAPPINGS='0x0A 0x5C 0x3A 0x2C'
declare -r ATTRS='dsRecTypeStandard:Users 2 dsAttrTypeStandard:RecordName externalbinary:dsAttrTypeStandard:JPEGPhoto'
if [ ! -f "${USERPIC}" ]; then
echo "User image required"
fi
# Check that the username exists - exit on error
${ID_CMD} "${USERNAME}" &>/dev/null || ( echo "User does not exist" && exit 1 )
declare -r PICIMPORT="$(mktemp /tmp/${USERNAME}_dsimport.XXXXXX)" || exit 1
printf "%s %s \n%s:%s" "${MAPPINGS}" "${ATTRS}" "${USERNAME}" "${USERPIC}" >"${PICIMPORT}"
${DSIMPORT_CMD} "${PICIMPORT}" /Local/Default M &&
echo "Successfully imported ${USERPIC} for ${USERNAME}."
rm "${PICIMPORT}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment