From 69ddc4592be8122630130eda748ccaf85f4355a4 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Holcroft Date: Tue, 1 May 2018 18:48:06 +0200 Subject: Add helpers (multi-file systemd and exec_as) --- scripts/_common.sh | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'scripts/_common.sh') diff --git a/scripts/_common.sh b/scripts/_common.sh index dce035f..7d4f823 100644 --- a/scripts/_common.sh +++ b/scripts/_common.sh @@ -1,5 +1,19 @@ #!/bin/bash + +# Execute a command as another user +# usage: exec_as USER COMMAND [ARG ...] +exec_as() { + local user=$1 + shift 1 + + if [[ $user = $(whoami) ]]; then + eval "$@" + else + sudo --login --user="$user" "$@" + fi +} + #================================================= # # POSTGRES HELPERS -- cgit v1.2.3-70-g09d2