Bash Script Tests for OS and Run as Root

In my last post, I discussed using sed, the stream editor to make configuration file changes via a bash script.  There are two important factors to keep in mind:

  • Many configuration file can only be modified as root, and
  • The location of configuration files will vary depending on the Linux distribution.

Therefore it is important to test for both of these conditions at the beginning of your Bash script.

Test to ensure expected OS Distribution and Version

The example above example is for Red Hat, so the string to grep will vary depending on the Linux distribution.

Test to Ensure Script is Run as Root

You may prefer a tight one-liner to test for root.  In that case you can use something like:

 

Tagged , , , , . Bookmark the permalink.
  • The postings and views on this site are my own and do not necessarily reflect the positions, strategies, or opinions of any current or previous employer.