-
-
Save taikedz/112198ec7ec2fef8d2ded2799f94cf4b to your computer and use it in GitHub Desktop.
Run an X application as a different user, with some sound working
This file contains hidden or 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
#!/usr/bin/env bash | |
die() { | |
echo "$*" | |
exit 1 | |
} | |
turnoff_xhost() { | |
xhost - >/dev/null 2>&1 | |
} | |
audio_user() { | |
local user="$1"; shift | |
if ! (groups "$user" | grep -e "\baudio\b" -q); then | |
echo "Adding $user to audio group." | |
echo "Password for $(whoami)" | |
sudo gpasswd -a "$user" audio || echo "Audio enablement problem - audio may not work" | |
fi | |
} | |
checkvar() { | |
local var="$1"; shift | |
[[ -n "$var" ]] || die "$* not specified" | |
} | |
main() { | |
if [[ "$WAITING_MODE" = true ]]; then | |
user="$1"; shift || { | |
echo "Specify the user to run as !" | |
} | |
trap turnoff_xhost exit | |
sudo -u "$user" -i "$@" >/dev/null 2>&1 | |
else | |
checkvar "$1" user | |
checkvar "$2" program | |
audio_user "$1" | |
xhost + >/dev/null 2>&1 || die "Could not adjust xhost" | |
WAITING_MODE=true "$0" "$@" & | |
fi | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment