Linux: Bash: what does mkdir return

20,712

Solution 1

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

Solution 2

Just for completeness, you can exit by issuing:

mkdir -p "$FINALPATH" || { echo "Failure, aborting..." ; exit 1 ; }

Braces are necessary, or else exit 1 would execute in both cases.

Or you can create an abort function like:

errormsg()
{
    echo "$1"
    echo Aborting...
    { exit 1 ; }
}

And then just call it by issuing:

mkdir -p "$FINALPATH" || errormsg "Failure creating $FINALPATH"

Edited:

  • Braces, not parenthesis, as parenthesis only exit the subshell. ( Thanks @Charles Duffy )
  • A function to write a message and exit

Solution 3

The shorter way would be

 mkdir -p "$FINALPATH" || echo failure

also idiomatic:

 if mkdir -p "$FINALPATH"
 then
      # .....
 fi

Likewise you can while .....; do ....; done or until ......; do ......; done

Share:
20,712
Tu Hoang
Author by

Tu Hoang

I am a novice programmer ...

Updated on February 07, 2020

Comments

  • Tu Hoang
    Tu Hoang over 4 years

    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?

  • Tu Hoang
    Tu Hoang almost 13 years
    Cool! It's working. One side question: what is the difference between if [ condition ]; then and if [ condition ] \n then? I don't quite get that part :(
  • Owen
    Owen almost 13 years
    @Tanner I think it's just stylistic.
  • Keith Thompson
    Keith Thompson almost 13 years
    @Tanner: It's just like any other context where you can have either a semicolon or a newline. command1 ; command2 is like command1 \n command2 (I wish I could do real newlines in a comment!). The word then has to be at the beginning of a line or following a ; to be recognized; otherwise it's just a word (as in echo then).
  • Charles Duffy
    Charles Duffy over 8 years
    If the goal is to exit the interpreter for the parent script, you want braces, not parens: mkdir -p "$FINALPATH" || { echo "Failure, aborting..."; exit 1; } -- mind the trailing ;, it's mandatory if you don't have a newline there; without it, } is parsed as an argument to exit.
  • Charles Duffy
    Charles Duffy over 8 years
    I'm not sure an empty if statement body is actually legal in all shells (actually, I'm quite certain that it's not). You might need to make it something like : success or true # success... or just invert the test and not have an else clause.
  • Charles Duffy
    Charles Duffy over 8 years
    Needs more quotes -- the OP's code is safe for directory names with whitespace or wildcard characters; should be so here as well.