Reputation: 39
Is it possible to refer to an error? Here is my code:
read dir
mkdir /Users/Dillon/$dir
And if the directory is already there, it tells me mkdir: /Users/Dillon/(dir): File exists
. Is there a way to state that if it already exists to not not show error?
Upvotes: 1
Views: 63
Reputation: 17936
You can test for directory existence before running the command:
[ -d /Users/Dillon/$dir ] || mkdir /Users/Dillon/$dir
Alternately, you can use the -p
flag:
mkdir -p /Users/Dillon/$dir
This will make the directory if it does not yet exist, as well as any missing directories in the path. It does not complain if the directory already exists. It will complain if any segment of the path exists, but isn't a directory or a symlink to a directory.
Upvotes: 5
Reputation: 12527
To suppress error output for any command, redirect the stderr stream to /dev/null
mkdir /Users/Dillion/$dir 2> /dev/null
Or for this one specific case, you could first check for the existence of the directory and bypass the mkdir call if the directory exists:
if [ ! -d /Users/Dillion/$dir ]; then
mkdir /Users/Dillion/$dir
fi
Upvotes: 1