1

I am trying to create a test whereas the logic is

If mkdir command is successfully executed perform the next actions

What I have are:

FILEPATH=/home/Documents/projectDirectory
CREATE_DIRECTORY=mkdir $FILEPATH

and the test command:

if [ -e $FILEPATH ]; then
    echo "${FILEPATH} exists.."
    else 
        #Create the folder: Test it if is created , else exit gracefully.
        if [ -e "${CREATE_COMMAND}" ]; then
        echo "File created ${FILEPATH}"
        else
            echo "Error creating the filePath..exiting.."
            error_exit "Error creating the filePath..exiting.."
        fi
fi
Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
Jepoy
  • 11
  • 3

3 Answers3

0

I'm unsure why you would want to this, but here is a working solution:

FILEPATH=/home/Documents/projectDirectory
CREATE_DIRECTORY="mkdir $FILEPATH 2>/dev/null"
eval $CREATE_DIRECTORY
if [[ $? -eq 0 ]]; then
 echo "Directory created"
else
 echo "Directory already exists"
fi

This will try to create the folder and pipe any error message to /dev/null (not displaying it). Then it checks the status of the previous command in the if statement and if that was successful (0) then tell you.

Mikael Kjær
  • 1,001
  • 1
    I would not recommend using eval -- depending on how tightly controlled the contents of the command are, it's either unnecessary or unsafe or both. There are almost always better ways to do things (although what those better ways are depends on exactly what the actual problem is). – Gordon Davisson Feb 28 '18 at 06:40
  • I totally agree, but when we don't know the purpose of this script which doesn't make sense in it's current form it's very difficult to give a good answer. – Mikael Kjær Feb 28 '18 at 07:20
0

Your current code has the following flaws:

  • CREATE_DIRECTORY=mkdir $FILEPATH:
    1. Since there are no quotes around mkdir $FILEPATH, CREATE_DIRECTORY only holds mkdir as its value.
    2. $FILEPATH is subject of word splitting.
    3. The CREATE_DIRECTORY variable may be exported to the environment of the resulting expansion of $FILEPATH if the first token of such expansion is an external command.
  • [ -e "${CREATE_COMMAND}" ]:
    1. The CREATE_COMMAND variable holds no value, as far as we know. I assume you meant to write CREATE_DIRECTORY.
    2. It doesn't test if mkdir was successful or not.

Also, the if compound command could be used to test if a command succeeded or not:

if command; then
  # Code to execute if command succeeded
else
  # Code to execute if command failed
fi

So your code could be rewrite as:

FILEPATH=/home/Documents/projectDirectory

if [ -e "${FILEPATH}" ]; then
    echo "${FILEPATH} exists..."
else
    if mkdir "${FILEPATH}"; then
        echo "File created ${FILEPATH}"
    else
        echo "Error creating the filePath..exiting.."
        error_exit "Error creating the filePath..exiting.."
    fi
fi

Note that:

  1. The CREATE_DIRECTORY variable is not needed.
  2. [ -e "${CREATE_COMMAND}" ] was replaced with mkdir "${FILEPATH}".
nxnev
  • 3,654
0

I don't really see why you can't just do

dir='/home/Documents/projectDirectory'

if ! mkdir "$dir"; then
    printf 'Something went wrong when trying to create "%s"\n' "$dir" >&2
    exit 1
fi

printf '"%s" created, now doing other things...\n' "$dir"

The call to mkdir will fail if $dir already exists.

If you need to allow for the directory to exist and only bail out if mkdir fails to create it, then add -p to the mkdir call above, or do an explicit test for the existence of the directory:

dir='/home/Documents/projectDirectory'

if [ ! -d "$dir" ]; then
    if ! mkdir "$dir"; then
        printf 'Something went wrong when trying to create "%s"\n' "$dir" >&2
        exit 1
    fi

    printf '"%s" created\n' "$dir"
fi

echo 'now doing other things...'

I'm using the -d test here to test whether $dir may be a directory. I do this because I assume that the rest of the script will require that the name doesn't just exists but that it refers to an actual directory.


Don't try to store commands in variables, it's rarely if ever what you need to do. It's not a good way to save on typing things out. If you need a shorter way of performing an action in a shell script, create a shell function for it.

Also, get into the habit of always double-quoting your variable expansions. That way your shell scripts will always correctly handle filenames with spaces in them and will never accidentally invoke filename globbing.

Related:


Other things:

CREATE_DIRECTORY=mkdir $FILEPATH

This will run $FILEPATH as a command with CREATE_DIRECTORY=mkdir in its environment. It will fail because $FILEPATH is a directory (if it exists) or at least not a command.

To create a variable with spaces in its value, quote the value.

The line

if [ -e "${CREATE_COMMAND}" ]; then

will not create a directory. It will test whether the value in $CREATE_COMMAND corresponds to a name in the file hierarchy.

I don't know what error_exit is, but it's probably a shorthand for a simple echo and exit 1 or similar, right?

Kusalananda
  • 333,661