shell-判断一个目录是否存在,不存在是否创建

#!/bin/bash
is_it_a_directory()#用于判断一个目录是否存在
{
    if [ $# -lt 1 ]
    then
        echo "is_it_a_directory:I need an argument"
        return 1
    fi
    _DIRECTORY_NAME=$1
    if [ ! -d $_DIRECTORY_NAME ]
    then
        return 1
    else
        return 0
    fi

}
#是否创建
echo "enter destination directory:"
read DIREC
if is_it_directory $DIREC >/dev/null 2>&1
then :
else
    echo "$DIREC does not exits, create it now?[y..n]"
    read choice
    if [ "$choice" = "y" ]
    then
        mkdir $DIREC
        echo "create $DIREC ok"
    else
        echo "see you again"
    fi
fi

你可能感兴趣的:(Shell手记)