ambari-sudo.sh 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/bin/bash
  2. # Licensed to the Apache Software Foundation (ASF) under one or more
  3. # contributor license agreements. See the NOTICE file distributed with
  4. # this work for additional information regarding copyright ownership.
  5. # The ASF licenses this file to You under the Apache License, Version 2.0
  6. # (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. SUDO_BINARY="/usr/bin/sudo"
  16. if [[ $# -eq 0 ]] ; then
  17. echo 'usage: ambari-sudo.sh [sudo_arg1, sudo_arg2 ...] command [arg1, arg2 ...]'
  18. exit 1
  19. fi
  20. # if user is non-root
  21. if [ "$EUID" -ne 0 ] ; then
  22. $SUDO_BINARY "$@"
  23. else
  24. ENV=()
  25. SUDO_ARGS=()
  26. for i ; do
  27. if [[ "$i" == *"="* ]] ; then
  28. ENV+=("$i")
  29. shift
  30. elif [[ "$i" == "-"* ]] ; then
  31. SUDO_ARGS+=("$i")
  32. shift
  33. else
  34. break
  35. fi
  36. done
  37. #echo "sudo arguments: ${SUDO_ARGS[@]}"
  38. #echo "env: ${ENV[@]}"
  39. #echo "args: $@"
  40. if [ "$ENV" ] ; then
  41. export "${ENV[@]}"
  42. fi
  43. "$@"
  44. fi