[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Q re: if test of command in bash script



Hey all!

After a couple of hours of searching, I've decided that both my
scripting-foo and my google-foo are weak this morning, so I turn to you
folks for a quick answer.

In a bash script, I'm trying to create a directory and test if it is
successful.

The man page doesn't indicate what mkdir returns as a successful code,
but I have a vague impression (being a non-programmer) that a return
code of zero is generally considered success and non-zero is generally
considered failure. (So man-page writer, hint hint ...)

Here's the relevant snippet of code I have so far:

===

echo "Does target directory '$targetDir' exist?"
if [ -d $targetDir ]
        then
                echo "YES, '$targetDir exists!"
                echo
        else
                echo "No, '$targetDir' does not exist."
                echo
                echo "Attempting to create $targetDir"
                echo
                if  [ mkdir -p $targetDir ]     # If the mkdir fails
                        then
                                echo "Failed to create directory. Aborting."
                                exit 1
                        else
                                echo "Created $targetDir directory
successfully!"
                fi
fi

===

When I run this, I get this output:

===

Does target directory '/TERASTATIONBACKUP/GOSHEN/2008/OCTOBER' exist?
No, '/TERASTATIONBACKUP/GOSHEN/2008/OCTOBER' does not exist.

Attempting to create /TERASTATIONBACKUP/GOSHEN/2008/OCTOBER

/usr/local/bin/BackupGoshenHome: line 57: [: -p: binary operator expected
Created /TERASTATIONBACKUP/GOSHEN/2008/OCTOBER directory successfully!
Tarring up source into target
     /TERASTATIONBACKUP/GOSHEN/2008/OCTOBER/2008-Oct-28.tgz

===


I've tried various permutations of the test case - put it in
double-quotes, in single-quotes, put a bang in front of the text, remove
the brackets, add a semi-colon at the end of the test line before the
comment, etc, but can't figure out what I'm doing wrong.

Thanks for any help!

-- 
Kent West     <*)))><
http://kentwest.blogspot.com


Reply to: