If I run this script, how do I pass super user permissions to it? I wrote this just to setup new machines with the basics. I don't want to run every command with elevated permissions, but the commands that do have sudo I want to run with them.How do I have some commands run with sudo and others run as the regular user?#!/bin/sh# If Linux, install nodeJSif $(uname) = 'Linux';then    export IS_LINUX=1    # Does it have aptitude?    if  -x which apt-get;    then        export HAS_APT=1        # Install NodeJS        sudo apt-get install --yes nodejs    fi    # Does it have yum?    if  -x which yum ;    then        export HAS_YUM=1        # Install NodeJS        sudo yum install nodejs npm    fi    # Does it have pacman?    if  -x which pacman ;    then        export HAS_PACMAN=1        # Install NodeJS        pacman -S nodejs npm    fifi# If OSx, install Homebrew and NodeJSif  $(uname) = 'Darwin' ;then    export IS_MAC=1    if test ! $(which brew)    then      echo ================================      echo   Installing Homebrew for you.      echo ================================      ruby -e $(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)      export HAS_BREW=1    elif  -x 'which brew' ;    then      export HAS_BREW=1      brew update    fi    # Install NodeJS    brew install --quiet nodefi# Does it have python?if  -x which python ;then    export HAS_PYTHON=1    if  -x which pip ;    then      pip list --outdated | cut -d ' ' -f1 | xargs -n1 pip install -U      export HAS_PIP=1    fifi# Does it have node package manager?if  -x which npm ;then    export HAS_NPM=1else    echo NPM install failed, please do manuallyfi# Does it have ruby gems?if  -x which gem ;then    export HAS_GEM=1fiThe rest of the bash script (that I didn't include for length) installs packages from an array using npm, apt, yum, brew, or pacman, depending on the machine. It only installs simple things like git, wget, etc.
How do I maintain sudo in a bash script?
bash;shell script;permissions;sudo
First time sudo is invoked password is prompted for. Then, depending on configuration, if invoked within N minutes (default 5 minutes IIRC), one do not need to enter password again.You could do something like:sudo echo >/dev/null || exit 1or perhaps something like:sudo -p Become Super:  printf  || exit 1at start of script.If you want to prevent anyone from doing sudo ./your_script you should check EUID as well (bash):if [[ $EUID -eq 0 ]]then    printf Please run as normal user.\n >&2    exit 1fior something like:if [ $(id -u) = 0 ]   ...In any case also check out which shell you targetr. I.e.https://wiki.debian.org/DashAsBinShhttps://wiki.ubuntu.com/DashAsBinShhttps://lwn.net/Articles/343924/etc.To keep it alive one could do something like:while true; do  sleep 300  sudo -n true  kill -0 $$ 2>/dev/null || exitdone &