编写一个shell脚本实现: 判断用户家目录下(~)下面有没有一个叫test的文件夹

编写一个shell脚本实现:
判断用户家目录下(~)下面有没有一个叫test的文件夹
如果没有,提示按y创建并进入此文件夹,按n退出
如果有,直接进入,提示请输入一个字符串,并按此字符串创建一个文件,如果此文件已存在,提示重新输入,重复三次自动退出,不存在创建完毕,退出

#!/bin/bash

cd ~
dir="test"
if [ -d "$dir" ];then
    num=0
    while (($num < 3));do
        read -p "Input file name:" file
        if [ -f "$dir/$file" ];then
            echo "file is exists,please input other file name"
            num=`expr $num + 1`
            continue
        else
            touch $dir/$file
            break
        fi
    done
else
    read -p "test dir is not exists, are you want to create it?[y/n]" ch
    if [ "$ch" = "y" ];then
        mkdir -p $dir
    else
        exit 1
    fi
fi

温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网