给OIer节约5s的快速创建子目录的程序

每次考试考完的时候教练都会要求建立子目录,然后又会在这个时候浪费我们生命中美好的5s,这个时候,这个轮子孕育而生,有了它,你就比其他人多拥有了5s的美好时光~

用法:在选手文件夹下输入Mkdir回车,即可为当前文件夹的所有cpp文件创建一个文件夹,同时把文件复制进去

方法: 复制以下内容到一个叫做"Mkdir"的文件里
宣传一波个人博客

#!/bin/bash
if [[ "$1" == "-h" ]]
then
    echo "---------------------------------------------------------------------------------"
    echo
    echo "-h  --help         输出此帮助界面"
    echo "-a  --ask          按询问模式执行"
    echo "-q  --quiet        安静的执行程序"
    echo "-c  --copy         仅仅将文件复制进文件夹"
    echo "文件名             shell默认跳出参数 并将后面的变量不创建文件夹(且删除此文件目录)"
    echo
    echo "例: Mkdir a.cpp b.cpp"
    echo
    echo "这将不会为a.cpp b.cpp创建目录,且如果也会删掉和文件 询问模式除外"
    echo -e "\033[;31m默认复制并替换文件 \033[;0m"
    echo
    echo "---------------------------------------------------------------------------------"
    exit 0
fi
ask=0
quiet=0
copy=0
while getopts ayqc opt
do
    case "$opt" in
        a)ask=1 ;;
        q)quiet=1 ;;
        c)copy=1 ;;
        *) echo "未知的参数!"  ;;
    esac
done
if echo "$1" | grep "-" >/dev/null 2>&1
then
    shift
fi
if [ $ask -eq 1 ]
then
    echo "以询问模式执行..."
fi
if [ $copy -eq 1 ]
then
    echo "以copy模式执行..."
fi
for file in `ls *.cpp`
do
    pre=`echo "$file" | cut -d '.' -f1`
    if [ $# -ne 0 ]
    then
        for list in "$@"
        do
            if [[ $list = $file ]]
                #if  echo "$file" | grep "$*" >/dev/null 2>&1 
            then
                if [ $ask -eq 1 ]
                then
                    if [ -d $pre ]
                    then
                        echo -e "\033[;33m 找到 $pre 文件夹,是否删除?\033[0m"
                        read -l opt
                        if [[ $opt = 'y' ]] || [[ $opt = 'Y' ]]
                        then
                            echo -e "\033[;31m 删除 $pre 文件夹.\033[0m"
                            echo -e "\033[;31m 删除 $file 文件.\033[0m"
                            echo
                            rm -rf $pre
                            rm $file
                        else
                            echo -e "\033[;32m 保留原文件夹\033[0m"
                        fi
                    fi
                else
                    echo -e "\033[;31m 删除 $pre 文件夹.\033[0m"
                    echo -e "\033[;31m 删除 $file 文件.\033[0m"
                    echo
                    rm -rf $pre
                    rm $file
                fi
                continue 2
            fi
        done
    fi
    if [ $copy -eq 1 ]
    then
        echo -e "\033[;33m 复制 $file 进入 $pre 文件夹...\033[0m"
        cp $file $pre
        continue
    fi
    if [ $quiet -ne 1 ]
    then
        echo -e "\033[;32m 正在创建 '$pre' 文件夹 ... \033[0m"
    fi
    if [ -d "$pre" ]
    then 
        if [ $quiet -ne 1 ]
        then
            echo -e "\033[;31m $pre 文件夹已经存在 \033[0m"
        fi
    else
        if [ $ask -eq 1 ]
        then
            echo -e "\033[;33m 是否创建 $pre 文件夹?(y/n)\033[0m"
            read  -l opt
            if [[ $opt == y ]]|| [[ $opt = 'Y' ]]
            then
                mkdir "$pre"
            else
                echo -e "\033[;33m 跳过此文件\033[0m"
                continue
            fi
        else
            mkdir "$pre"
        fi
        if [ $quiet -ne 1 ]
        then
            echo -e "\033[;37m $pre 文件夹创建成功 \033[0m"
        fi
    fi
    if [ $quiet -ne 1 ]
    then
        echo -e "\033[;32m 正在复制 $file$pre 文件夹 ... \033[0m"
    fi
    if [ -f "$pre/$file" ]
    then
        if [ $quiet -ne 1 ]
        then
            echo -e "\033[;31m 文件 $file 已经存在! \033[0m"
        fi
        if [ $ask -eq 1 ]
        then
            echo -e "\033[;33m 是否强制替换?(y/n) \033[0m"
            read opt
            if [[ $opt == y ]] || [[ $opt = 'Y' ]]
            then
                cp "$file" "$pre"
            fi
        elif [ $quiet -ne 1 ]
        then
            echo -e "\033[;32m 强制替换文件 \033[0m"
        fi
        cp "$file" "$pre"
    else 
        cp "$file" "$pre"
    fi
    echo
done
echo -e "\033[;31m 程序执行结束\033[0m"

然后

sudo chmod 777 Mkdir
sudo mv Mkdir /bin
Mkdir -h

效果图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值