Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

Saturday, January 8, 2011

Am I root?

Root privileges is important when doing system wide installation and configuration. How do we determine the script is being run by a root or normal user?

# Check that we are run as root.
# Return 0 if yes, or 1 if not or undeterminable.
function is_root() {
  uid=$(id -u 2> /dev/null)

  if [ -n "$uid" ]; then
    if [ "$uid" != "0" ]; then
      echo "FAILED: You need root privileges to run this script.";
      echo
      return 1
    fi
  else
    echo "Could not detect UID";
    return 1
  fi
}

Wednesday, January 5, 2011

Which distro?

When writing shell script, sometime we need to find out which Linux flavor is running on the machine. I like the following bash script from knowledgetree installer to do the job.

function distro_info() {
  distro=$(lsb_release --short --id)

  case "$distro" in
    "Ubuntu"|"Debian")
      # install deb package using apt-get
      ;;
    "CentOS"|"Fedora"|"RedHatEnterpriseServer")
      # install rpm package using yum
      ;;
    * )
      unsupported_distro
      return 1
      ;;
  esac
}

Related Posts Plugin for WordPress, Blogger...