Difference between mkdir and mktemp within a script

642 views Asked by At

When I use mkdir to create a directory named WORKDIR in the cwd, the following way works perfectly fine:

#!/bin/bash

SUBDIR=$(pwd)
export BASISDIR="/home/Basis_Sets"

for i in *.ZMAT; do
    [ -f "$i" ] || break
    BASENAME=$(basename $i .ZMAT)
    ZMAT_file=$BASENAME.ZMAT
    FCMINT_file=$BASENAME.FCMINT
    ERROR_file=$BASENAME.slu
    
    mkdir $SUBDIR/$BASENAME
    export WORKDIR="$SUBDIR/$BASENAME"
    if [[ ! -f $ERROR_file ]] && [[ -d $WORKDIR ]]; then
        cp $BASENAME.* $WORKDIR
        cp $BASISDIR/GENBAS $WORKDIR
        cp _CFOUR_SLURM.SUB $WORKDIR
        cd $WORKDIR
        RUN_COMMAND="sbatch -J $BASENAME _CFOUR_SLURM.SUB"
        eval $RUN_COMMAND
        mv $ZMAT_file ZMAT
        mv $FCMINT_file FCMINT
        file_count=$(ls -x *)
        echo "File Count: $file_count"
        cd $SUBDIR
        sleep 10
    else
        echo "$BASENAME Error File Exists - Not Submitting Job"
    fi
done

However, when I use it as follows it fails to identify WORKDIR, but the directory is still created (i.e. the if statement fails, but the directory is created). I used this identical method in a different script to create a temporary directory [that is: NAME=$(mktemp -d ....)], and it works perfectly fine. Why does the below code work for mktemp but not mkdir?

#!/bin/bash

SUBDIR=$(pwd)
export BASISDIR="/home/Basis_Sets"

for i in *.ZMAT; do
    [ -f "$i" ] || break
    BASENAME=$(basename $i .ZMAT)
    ZMAT_file=$BASENAME.ZMAT
    FCMINT_file=$BASENAME.FCMINT
    ERROR_file=$BASENAME.slu
    
    WORKDIR=$(mkdir $SUBDIR/$BASENAME)
    ....
1

There are 1 answers

0
Nathan Mills On BEST ANSWER

mkdir doesn't print the name of the directory created; mktemp -d does. Since mkdir doesn't print anything, $WORKDIR gets set to an empty string.