Skip to content

Instantly share code, notes, and snippets.

@lazerl0rd
Last active October 13, 2023 00:53
Show Gist options
  • Save lazerl0rd/040743fe718324796820dc071c529bdc to your computer and use it in GitHub Desktop.
Save lazerl0rd/040743fe718324796820dc071c529bdc to your computer and use it in GitHub Desktop.
#! /usr/bin/env bash
args="$@"
argsUnsupported="false"
asUser="root"
if [[ $@ == "" ]]; then
echo "usage: xsudo [command [arg ...]]"
echo "usage: xsudo [-u user] [command [arg ...]]"
exit 1
fi
if [[ $1 == -u ]]; then
args="${@:3}"
asUser="$2"
fi
for arg in "$args"; do
if [[ $arg == -* ]]; then
argsUnsupported="true"
break;
fi
done
if [[ $argsUnsupported == true ]]; then
echo "Unsupported arguments were passed, please use \`sudo\`."
exit 1
else
sudo -u "$asUser" sh -c "xauth add $DISPLAY . $(xauth list | grep $(uname -n) | grep :$(echo -n $DISPLAY | awk -F ":" '{print $2}' | awk -F "." '{print $1}') | awk '{print $3}'); $args"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment