Here is the exact script that's used for Ubuntu external dependencies:
It covers not using gksudo when the DISPLAY is not set (as no X Server is likely running) and the apt commands are in quotes. Found in pts/distro-scripts/install-ubuntu-packages.sh on all releases prior to 2.4 or with 2.4+ is to be found in pts-core/static/distro-scripts/install-ubuntu-packages.sh.
if [ `whoami` != "root" ]; then
if [ -x /usr/bin/gksudo ] && [ ! -z "$DISPLAY" ]; then
elif [ -x /usr/bin/kdesu ] && [ ! -z "$DISPLAY" ]; then
elif [ -x /usr/bin/sudo ]; then
if [ -x /usr/bin/aptitude ]; then
# aptitude is nice since it doesn't fail if a non-existant package is hit
# See: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=503215
$ROOT "aptitude -y install $*"
$ROOT "apt-get -y --ignore-missing install $*"