• shell编程基础之根据输入进行相应的操作


    这个题目是

    输入A:创建自己名字的文件夹,如果有这个文件夹的话,就读取file1和file2的文件内容

    输入B:获取当前用户对file1和file2文件权限

    输入C:内容转为大写

    输入D:计算连接和文件数目

    输入E:计算文件空行数目

    以下是我的代码:

    #! bin/bash
    read opts;
    case $opts in
    'A') 
        if [ -d ~/li ];then
            cp ./file1 ~/li;
            cp ./file2 ~/li;
            cat ~/li/file1;
            cat ~/li/file2;    
        else
            mkdir ~/li;
            chmod -R 755 ~/li;
        fi
        ;;    
    'B')    if [ -d ~/liwei ];then
            file1=$(ls -l ~/li | grep 'file1'| awk -F " " '{print $1}' | cut -d '.' -f 1);
            file2=$(ls -l ~/li | grep 'file2'| awk -F " " '{print $1}' | cut -d '.' -f 1);
            echo your power of file1 is $file1;
            echo your power of file2 is $file2;
        else
            echo please excute 'A' first;
        fi
        ;;    
    'C')    if [ -d ~/li];then
            file1=$(cat ~/li/file1);
            file2=$(cat ~/li/file2);
        
            upper=$(echo $file1 | tr '[a-z]' '[A-Z]');
            lower=$(echo $file2 | tr '[a-z]' '[A-Z]');
            
            echo 'uppercase show of  file1 and file2:';
            echo $upper;
            echo $lower;
        else
            echo please excute 'A' first;
        fi    
        ;;
    'D')    file_str=$(ls -l  /dev | awk -F " " '{print $1}' | cut -c 1 | tr 
     , );
        file_arr=($file_str);
        l_num=0;
        d_num=0;
        for x in ${file_arr[@]}
        do 
            if [ "$x" = "d"  ];then
                d_num=$(($d_num + 1));
            fi
            if [ "$x" = "l" ];then
                l_num=$(($l_num + 1));
            fi
        done
        echo the number of directory in dev is $d_num;
        echo the number of link in dev is $l_num;
        ;;
    'E')    line_num=0;
        test_num=0;
        if [ -d ~/li ];then
            
            while read line
            do
                if [[ ! -n $line  ]];then
                    line_num=$(($line_num + 1));
                fi
            done < ~/li/file1
            echo "the number of blank line is $line_num"; 
        else
            echo please excute 'A' first.
        fi
        ;;
    esac
    exit
  • 相关阅读:
    第二次结对编程作业
    第一次结对编程作业
    第一次个人编程作业
    第一次博客作业
    第09组 Alpha冲刺(4/6)
    第09组 Alpha冲刺(3/6)
    第09组 Alpha冲刺(2/6)
    第09组 Alpha冲刺(1/6)
    第09组 团队Git现场编程实战
    第09组 团队项目-需求分析报告
  • 原文地址:https://www.cnblogs.com/rimochiko/p/8168620.html
Copyright © 2020-2023  润新知