Both [`su`](http://manpages.ubuntu.com/manpages/zesty/en/man1/su.1.html) or [`sudo`](http://manpages.ubuntu.com/manpages/zesty/en/man8/sudo.8.html) can do this. They run a command as another user; by default that "another user" is root, but it can be any user, for example sudo -u www-data ls will run `ls` as the user `www-data`. However... The usual way is to run the script as the invoking user and use `sudo` for those commands which need it. `sudo` caches the credentials, so it should prompt at most once.