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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:25:07+00:00 2026-05-24T20:25:07+00:00

I want to write a simple check upon running mkdir to create a dir.

  • 0

I want to write a simple check upon running mkdir to create a dir. First it will check whether the dir already exists, if it does, it will just skip. If the dir doesn’t exist, it will run mkdir, if mkdir fails (meaning the script could not create the dir because it does not have sufficient privileges), it will terminate.

This is what I wrote:

if [ ! -d "$FINALPATH" ]; then
    if [[ `mkdir -p "$FINALPATH"` -ne 0 ]]; then
        echo "\nCannot create folder at $FOLDERPATH. Dying ..."
        exit 1
    fi
fi

However, the 2nd if doesn’t seem to be working right (I am catching 0 as return value for a successful mkdir). So how to correctly write the 2nd if? and what does mkdir returns upon success as well as failure?

  • 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-24T20:25:08+00:00Added an answer on May 24, 2026 at 8:25 pm

    The result of running

    `mkdir -p "$FINALPATH"`
    

    isn’t the return code, but the output from the program. $? the return code. So you could do

    if mkdir -p "$FINALPATH" ; then
        # success
    else
        echo Failure
    fi
    

    or

    mkdir -p "$FINALPATH"
    if [ $? -ne 0 ] ; then
        echo Failure
    fi
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a simple task which will update and commit source code
I want to write a simple chat-client in Ruby for the terminal. The Problem
I want to write a simple, dumb, X terminal emulator in C on a
I want to write a simple web framework myself using WSGI, Python. I am
I want to write a simple DSL in C#. Nothing too complicated. I'm looking
I want to write a simple web proxy, for exercise. Here's the code I
I want to write a simple class (PHP5) that can 'run' an unknown amount
I want to write a simple SMPP Server that basically forwards traffic to another
I want to write a simple program which shows my internet activity over a
Any links/Turorials avalable? I want to write a simple ISAPI plugin for IIS6.0. Prefered

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.