• shell脚本学习积累


    创建shell脚本并运行

    方式1:
    vim myshell.sh
    #文件开头是#!/bin/bash 或 #!/bin/sh
    sh myshell.sh
    
    方式2:
    vim myshell
    #需要设置文件为可执行的状态
    chmod +x myshell
    #作为可执行文件直接执行,有的后面可带输入参数
    ./myshell
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    条件测试

    博主-条件测试传送门

    条件测试中-z到-d参数的意思

    &&的妙用,并用echo来反馈前一个命令任务是否成功:

    零碎知识

    变量与正则化

    # 使用命令结果赋给变量,注意命令外面一定使用、、(这个不是常规的单引号)
    PWD = ·pwd·
    
    # find命令中模糊寻找(其中:一定要用双引号而不是单引号, -printf '%f\n'打印时可以单行打印)
    find /root/rpmbuild/SRPMS -name "${NAME}-[0-9]*" -printf '%f\n'
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    基础shell脚本

    打包过去24小时内修改过的文件

    #!/bin/bash
    tar -zcvf lastmodify.tar.gz `find . -mtime 1 -type f -print`
    
    • 1
    • 2

    注意`` 可以将【一个命令的输出】作为作为【另一个命令的输入参数】,其中mtime是过去24小时修改的意思

    自动解压 bzip2, gzip 和 zip不同类型的压缩包

    创建一个smartzip的脚本实现,能实现自动的解压缩bzip2, gzip 和 zip不同类型的压缩包。
    思路:利用file命令首先判断文件的类型,按照字符串的匹配来使用不同的解压缩命令进行解压缩

    #!/bin/sh
    # USAGE:smartzip file.zip
    # EXAMPLE:smartzip articles.zip
    
    #ftype变量是查询文件类型后的字符串信息,利用case来进行字符串匹配
    #$1 就是脚本命令中输入参数的第一个
    ftype=`file "$1"`
    case "$ftype" in
    #file查出的信息开头就是那个文件的名字,所以字符串拼接头就是$1,冒号后开始具体的文件类型
    "$1: Zip archive"*)
    unzip "$1" ;;
    "$1: gzip compressed"*)
    gunzip "$1" ;;
    "$1: bzip2 compressed"*)
    bunzip2 "$1" ;;
    *) error "File $1 can not be uncompressed with smartzip";;
    esac
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    case的脚本语法形式为:

    打印一个rpm包的统计信息【含有输入参数】

    vim一个showrpm的脚本文件,同时chmod +x showrpm命令

    #!/bin/sh
    # list a content summary of a number of RPM packages
    # USAGE: showrpm rpmfile1 rpmfile2 ...
    # EXAMPLE: showrpm /cdrom/RedHat/RPMS/*.rpm  <-- 实现查看某个文件下的rpm包的统计信息
    # 正常测试:需要使用./showrpm rpmfile1 rpmfile2 来完成
    
    for rpmpackage in $*; do
    # -r判断这个变量属性是否可以read
    if [ -r "$rpmpackage" ];then
    echo "=============== $rpmpackage =============="
    rpm -qi -p $rpmpackage
    else
    echo "ERROR: cannot read file $rpmpackage"
    fi
    done
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    注意这里的$*表示输入行命令参数的所有值,如果是 1 表示输入命令行参数中的第一个参数, 1表示输入命令行参数中的第一个参数, 1表示输入命令行参数中的第一个参数,$#表示输入参数的个数。

    for-loop的脚本语法形式为

    重命名多个文件名【含有输入参数】

    vim一个rename_file脚本,用于重名多个文件,将文件名的old字段替换为new字段

    #!/bin/sh
    # USAGE:./rename_file old new file1 file2 file3 【显然:输入参数至少要3个,前两个替换,后面至少一个文件】
    # we have less than 3 arguments. Print the help text:
    if [ $# -lt 3 ] ; then
    cat <<HELP
    ren -- renames a number of files using sed regular expressions
    USAGE: ren 'regexp' 'replacement' files...
    EXAMPLE: rename all *.HTM files in *.html:
    ren 'HTM$' 'html' *.HTM
    HELP
    exit 0
    fi
    
    # old被替换的字段。new待取代的字段
    OLD="$1"
    NEW="$2"
    # The shift command removes one argument from the list of 【移除输入参数的最前面两个字段】
    # command line arguments.
    shift
    shift
    # $* contains now all the files:【遍历所有的文件名】
    for file in $*; do
    if [ -f "$file" ] ; then
    # newfile变量存储为替换后的文件名
    newfile=`echo "$file" | sed "s/${OLD}/${NEW}/g"`
    # 文件已存在,error ; 文件不存在,save
    if [ -f "$newfile" ]; then
    echo "ERROR: $newfile exists already"
    else
    echo "renaming $file to $newfile ..."
    mv "$file" "$newfile"
    fi
    fi
    done
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    在这里插入图片描述
    在这里插入图片描述

    二进制转十进制【含有输入参数】

    #!/bin/sh
    # vim: set sw=4 ts=4 et:
    # USAGE: b2d 110100
    help()
    {
    cat <<HELP
    b2h -- convert binary to decimal
    USAGE: b2h [-h] binarynum
    OPTIONS: -h help text
    EXAMPLE: b2h 111010
    will return 58
    HELP
    exit 0
    }
    
    # 输入参数错误函数
    error()
    {
    # print an error and exit
    echo "$1"
    exit 1
    }
    
    # 获取字符串的最后一个字符
    lastchar()
    {
    # return the last character of a string in $rval
    # 判断字符串长度是否为0
    if [ -z "$1" ]; then
    # empty string
    rval=""
    return
    fi
    # wc puts some space behind the output this is why we need sed:
    # 去除wc统计字符数量中的空格
    numofchar=`echo -n "$1" | wc -c | sed 's/ //g' `
    # now cut out the last char
    # cut -b 截取第$numofchar个字符
    rval=`echo -n "$1" | cut -b $numofchar`
    }
    
    # 删除字符串的最后一个字符
    chop()
    {
    # remove the last character in string and return it in $rval
    if [ -z "$1" ]; then
    # empty string
    rval=""
    return
    fi
    # wc puts some space behind the output this is why we need sed:
    numofchar=`echo -n "$1" | wc -c | sed 's/ //g' `
    if [ "$numofchar" = "1" ]; then
    # only one char in string
    rval=""
    return
    fi
    # 获取倒数第二个字节的位置
    numofcharminus1=`expr $numofchar "-" 1`
    # now cut all but the last char:
    # 截取第1~numofcharminus1位置的字符,即删除最后一个字符
    rval=`echo -n "$1" | cut -b 0-${numofcharminus1}`
    }
    
    
    # 程序开始入口:
    # 判断二进制字符串是否为空
    while [ -n "$1" ]; do
    case $1 in
    -h) help;shift 1;; # function help is called
    --) shift;break;; # end of options
    -*) error "error: no such option $1. -h for help";;
    *) break;;
    esac
    done
    
    # The main program:右移位权重累计和
    # 初始化累计和为0,初始化权重值为0
    sum=0
    weight=1
    # one arg must be given:
    [ -z "$1" ] && help
    binnum="$1"
    binnumorig="$1"
    
    while [ -n "$binnum" ]; do
    # 获取最后一个字符
    lastchar "$binnum
    # 累计权重和
    if [ "$rval" = "1" ]; then
    sum=`expr "$weight" "+" "$sum"`
    fi
    
    # remove the last position in $binnum【移除最后一个字符】
    chop "$binnum"
    # 更新二进制字符串
    binnum="$rval"
    # 更新权重
    weight=`expr "$weight" "*" 2`
    done
    echo "binary $binnumorig is decimal $sum"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
  • 相关阅读:
    打造类ChatGPT服务,本地部署大语言模型(LLM),如何远程访问?
    逻辑器件与热插拔
    洛谷 : P1020 [NOIP1999 普及组] 导弹拦截
    前端JS学习(一)
    Vue环境搭建
    6-5 头插法创建单链表(C) 分数 10
    localStorage容量太小? 试试它们
    nodejs项目实例医生预约平台宠物医院预约挂号网
    目标检测论文解读复现之三:基于改进YOLOv7的X光图像旋转目标检测
    python+vue实验室课程预约管理系统
  • 原文地址:https://blog.csdn.net/qq_39227541/article/details/132716050