• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to Check if the script is run by root at the start of the script

by admin

Check root at start of script

Some scripts need to be run as root and you may want to check at the start of that script that it is running as root. This can be done by checking the environment variable $EUID. This variable will hold the value 0 if it’s being run as root.

#!/bin/bash
if [ "$EUID" -ne 0 ];then
    echo "Please run this script as root"
    exit 1
fi

Filed Under: DevOps, Shell Scripting

Some more articles you might also be interested in …

  1. What are Shell Scripts? How to Create Shell Scripts?
  2. How to find docker storage device and its size (device mapper storage driver)
  3. “Error: Could Not Find A Ready Tiller Pod” – helm error
  4. How to check the status and space used by images and containers
  5. Shell Script to print pyramid of Stars
  6. Understanding Positional Parameters (Passing Parameters/Arguments to Shell script)
  7. How to Map Static IP to your Domain (with GoDaddy example)
  8. How to change the default IP address of docker bridge
  9. How to Pause and Resume Docker Containers
  10. How to Configure Nagios NRPE Client for System Monitoring (CentOS/RHEL)

You May Also Like

Primary Sidebar

Recent Posts

  • qemu-system-x86_64: command not found
  • timedatectl: command not found
  • mpirun.openmpi: command not found
  • startkde: command not found

© 2022 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright