diff options
author | Samruddhi Khandale <skhandale@microsoft.com> | 2022-06-22 20:47:27 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-22 20:47:27 +0300 |
commit | f657b3e16438557d0cfb54a8c112d4943f7d7b13 (patch) | |
tree | ad8cb79c4665b6cae45125f1843ef1ef348c425e | |
parent | 3bd74f6b70bdb99f90972e43c62c4ace1ecfed21 (diff) |
adding sudo_if
-rw-r--r-- | src/ruby/install.sh | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/src/ruby/install.sh b/src/ruby/install.sh index c877840..f93253c 100644 --- a/src/ruby/install.sh +++ b/src/ruby/install.sh @@ -168,6 +168,16 @@ check_packages() { fi } +# Use sudo to run as non-root user is not already running +sudo_if() { + COMMAND="$*" + if [ "$(id -u)" -eq 0 ] && [ "$USERNAME" != "root" ]; then + su - "$USERNAME" -c "$COMMAND" + else + "$COMMAND" + fi +} + # Ensure apt is in non-interactive to avoid prompts export DEBIAN_FRONTEND=noninteractive @@ -241,7 +251,7 @@ if [ "${INSTALL_RUBY_TOOLS}" = "true" ]; then # Non-root user may not have "gem" in path when script is run and no ruby version # is installed by rvm, so handle this by using root's default gem in this case ROOT_GEM="$(which gem || echo "")" - su - ${USERNAME} -c ${ROOT_GEM} install ${DEFAULT_GEMS} + sudo_if ${ROOT_GEM} install ${DEFAULT_GEMS} fi # VS Code server usually first in the path, so silence annoying rvm warning (that does not apply) and then source it |