美文网首页
Shell脚本

Shell脚本

作者: delta1037 | 来源:发表于2019-05-20 20:33 被阅读0次

    Shell脚本

    文件夹排序脚本

    对当前目录

    对当前目录所有的文件夹名称及其文件名称进行排序的操作

    #/bin/bash
    #对文件路径的本目录的所有文件的文件名进行排序
    
    path=$1
    files=$(ls $path)
    
    for filename in $files #循环遍历当前文件夹内的所有文件名及其文件夹名
    do
       echo $filename>>sort.txt #将输出的文件名重定向到test.txt
    done
    
    echo "排序之后:"
    sort test.txt #对文件排序并输出
    rm test.txt #删除文件
    

    对当前目录及其子目录

    对当前目录及其子目录所有的文件夹名称及其文件名称进行排序的操作

    #!/bin/bash
    #对文件路径本目录及其所有子目录文件名排序
    
    function ergodic(){
        for filename in $(ls $1) #循环遍历当前文件夹内的所有文件名及其文件夹名
        do
            if [ -d $1"/"$filename ] #判断是否存在子目录
            then
                ergodic $1"/"$filename #对函数ergodic进行递归调用
            else
                echo $filename>>test.txt #将输出的文件名重定向到test.txt
            fi
        done
    }
    ergodic $1 #函数开始执行的位置,$1为输入的第一个参数
    echo "排序之后:"
    sort test.txt #对文件排序并输出
    rm test.txt #删除文件
    

    非递归阶乘

    #!/bin/bash
    # 计算阶乘
    # 由于$?无法返回大数字,使用全局变量存储返回的值
    
    let result=0
    let localreturn=0
    
    funFactorial(){
        let num1=1
        if [ $1 -gt $num1 ]; then
            let numSub=$1-1
            funFactorial $numSub
            let result=$localreturn*$1
            let localreturn=$result
        else
            let result=1
            let localreturn=1;
        fi
    }
    if [ -n "$1" ]; then
        funFactorial $1
        echo result:$result
    else
        echo "usage: factorial.sh [n]"
        echo "calculates a number's factorial"
    fi
    

    文件解压

    #!/bin/bash
    
    # usage: self_compression.sh [--list] or [source compressd file] [destination path]
    # self compression accroding to file name suffix
    
    # 不存在参数
    if [ "" = "$1" ]; then
        echo "usage: self_compression.sh [--list] or [source compressd file] [destination path]"
        echo "self compression accroding to file name suffix"
    else 
        # 有参数 --list
        if [ "--list" = "$1" ]; then
            echo "Support file types:zip tar tar.gz tar.bz2"
        # 选择文件类型&解压缩文件
        else
            # get type
            file=$1
            type=${file##*.}
            # Choose type & unzip
            if [ "$type" = "zip" ]; then
                unzip $1 -d $2
            elif [ "$type" = "tar" ]; then
                tar -xf $1 -C $2
            elif [ "$type" = "gz" ]; then
                tar -xzvf $1 -C $2
            elif [ "$type" = "bz2" ]; then
                tar -xjvf $1 -C $2
            else
                echo "$type Not Suport!!"
            fi
        fi
    fi
    

    获得某个目录下前N个最大的文件

    #!/bin/bash
    # echo "usage:file_size_get.sh [-n N] [-d DIR]"
    # echo "show top N largest files/directories"
    # 获得指定或默认目录下的前N个文件或者所有文件
    
    # 存在参数
    if [ -n "$1" -o -n "$3" ];then
        # 有两个参数
        if [ -n "$1" -a -n "$3" ];then
            du -ak $4 | sort -nr | head -$2 > test.txt
        # 只有参数-n
        elif [ "-n" = "$1" ];then
            du -ak | sort -nr | head -$2 > test.txt
        # 只有参数-d
        else
            du -ak $2 | sort -nr > test.txt
        fi
    # 不存在参数
    else
        du -ah | sort -nr > test.txt
    fi
    cat -n test.txt
    rm test.txt
    

    源码github链接

    相关文章

      网友评论

          本文标题:Shell脚本

          本文链接:https://www.haomeiwen.com/subject/vmvlzqtx.html