Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6476279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:48:21+00:00 2026-05-25T06:48:21+00:00

In an answer to a previous question: How can I use 'do I have

  • 0

In an answer to a previous question:

How can I use 'do I have root access?' as a conditional in bash?

The suggestion to ‘try to do something and detect if it fails’ instead of ‘check permission and then do something’

I have found plenty of rationale for this e.g.:

  • Thorough use of 'if' statements or 'try/catch' blocks?
  • What is the advantage of using try {} catch {} versus if {} else {}

However, I have found very little clear information about how to implementing try/catch in bash. I would guess that it is too easy, except that what I have found seems rather complicated- using functions or other scripts:

  • Error handling in Bash
  • Executing code in if-statement (Bash)

I am relatively new to bash but confused that there is not a simple function similar to the try function in other languages.

specifically, I would like to do the following:

CMD=`./path/to/script.sh`
if [ <echo $CMD | grep error is true> ]; then
        .. do this ..
else 
        .. do that ..
fi   
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:48:22+00:00Added an answer on May 25, 2026 at 6:48 am
    if sudo chmod a-x /etc/shadow 2>/dev/null
    then : Yes - I have root permissions
    else : No - I do not have root permissions or /etc/shadow does not exist or ...
    fi
    

    This chooses an operation that does no damage if it succeeds (the shadow password file is not supposed to be executable; you could do something like chmod o-w / – remove public write permission from the root directory if you prefer), and check that it worked by looking at the exit status of the command. This throws away the error message – you have to decide whether that matters.

    The ‘sudo’ is there to raise the privileges; if you think the user should already be ‘root’, then omit the ‘sudo’.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following on from a previous question in which I asked: How can I use
In my previous question I got an excellent answer that helped me detect where
I have search the previous question but couldn't find the answer. Dynamically generated html:
After getting fine answer to my previous question , I came across another problem.
This came up from this answer to a previous question of mine . Is
I was looking for an answer for a previous question and had an ingenious
In a previous question I asked, the suggested answer was for me to partition
This is a CSS related question, I got one good answer from my previous
An answer posted for one of my previous questions brings up another question; I
(This question is related to my previous question , or rather to my answer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.