Quantcast
Channel: CodeSection,代码区,Linux操作系统:Ubuntu_Centos_Debian - CodeSec
Viewing all articles
Browse latest Browse all 11063

How do you refer to an error in Bash / Shell Script?

$
0
0

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?

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.


Viewing all articles
Browse latest Browse all 11063

Trending Articles