美文网首页
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入门笔记

    Shell脚本:Linux Shell脚本学习指南菜鸟教程 - Shell教程Linux入门 - Shell脚本是...

  • 2018-09-26

    shell脚本 1.1、什么是shell脚本(shell script , ...

  • Shell script + crontab实现Mysql定时备

    一、Shell 脚本 Shell 脚本(shell script),是一种为 shell 编写的脚本程序。 业界所...

  • 嵌入式day12

    shell脚本的本质 shell脚本语言是解释型语言 shell脚本的本质:shell命令的有序集合 shell编...

  • shell脚本

    什么是shell脚本 Shell 脚本(shell script),是一种为 shell 编写的脚本程序。业界所说...

  • Shell脚本语法

    1. Shell脚本简介Shell 脚本(shell script),是一种为 shell 编写的脚本程序。业界所...

  • shell脚本

    什么是Shell脚本 Shell脚本(英语:Shell script),又称Shell命令稿、程序化脚本,是一种电...

  • 【生物信息笔记】shell 脚本 (dry-2)

    shell 和 shell script(脚本)区别: shell 和 shell 脚本是两个不同概念,shell...

  • chapter 11. 构建基本脚本

    创建shell脚本 shell脚本第一行为指定具体shell来运行该脚本,可以指定shell(待验证) echo ...

  • PySparkSQL脚本模板

    PySpark模板分为shell脚本和python脚本两部分,通过shell脚本提交spark任务。 shell脚...

网友评论

      本文标题:Shell脚本

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