• 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. How to Create a MySQL Docker Container for Testing
  2. How to use command redirection under Linux
  3. Install AzCopy on Linux – Fastest way to copy in Azure
  4. Unable to run NGINX Docker due to “13: Permission denied”
  5. How to use command line shell functions in Linux
  6. Docker Basics – Expose ports, port binding and docker link
  7. “while” Loop Examples in Shell Scripts
  8. How to Stop and Start Ec2 instance using Jenkins
  9. How to Download Ext JS GPL
  10. How to schedule master node running pod/service as worker node

You May Also Like

Primary Sidebar

Recent Posts

  • ncat Command Examples in Linux
  • ncat: command not found
  • nautilus Command Examples in Linux
  • namei: command not found

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright