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 8384595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:26:32+00:00 2026-06-09T17:26:32+00:00

I have trawled the internet for this answer, but alas couldn’t find one. In

  • 0

I have trawled the internet for this answer, but alas couldn’t find one.

In PHP I can run a command inside an if block, like so

if(command(blah)) {} else {}

As each function will return a truthy/falsey value. Can I do the same in a shell script? I want to echo output based on whether a command runs correctly. The commend to run is:

find . -type d -exec sudo chmod -R 775 {} \\;

I want to output that it ran successfully, or failed, then move on to another command.

Could I run the command into a variable or something similar?

  • 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-06-09T17:26:34+00:00Added an answer on June 9, 2026 at 5:26 pm

    Shell commands typically return an exit code indicating success or failure, with 0 indicating success and anything else indicating some sort of error (the specifics will depend on what command you’re running).

    You can test for this exit code in an if statement:

    if my_command; then
      echo It worked.
    fi
    

    And you can also chain commands together using the || and && operators.

    my_command && echo It worked.
    my_command || echo It failed.
    

    Your specific example with the find command is a little tricky. The chmod command might fail, but the find command will still complete “successfully”. Something like this might help:

    find . -type d -exec sudo sh -c 'chmod -R 775 {} || echo "FAILED {}"' \;
    

    This will print…

    FAILED ./path/to/directory
    

    …for any place it fails, so you could check the output. But I’m not sure that’s absolutely necessary. There are few situations where your command is going to fail: e.g., on a read-only filesystem, or possibly on an NFS mount.

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

Sidebar

Related Questions

I've trawled the SO website thinking this is pretty obvious but alas I have
Jquery wont load. I'm a noob, but have persisted, trawled this site & tried
Have converted devise new session from erb to Haml but doens't work, this is
I have gotten this to work nicely, but when speed hits a certain speed
I have trawled google trying to find someone who has done what I am
I have trawled through tens of forums now to find MySql syntax to insert
I have trawled the internet - to no avail. Woe is me. I have
so I have trawled true all threads in here and any where else where
I have followed numerous screen casts as well as trawled the backbone docs extensively
have written this little class, which generates a UUID every time an object of

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.