zoukankan      html  css  js  c++  java
  • shll脚本常用格式和规则使用

    shll脚本格式和规则


    脚本文件必须已 .sh 结尾(yuan.sh)
    脚本第一行必须是:#!/bin/bash
    激活脚本的二种方式(sh yuan.sh)(给脚本X权限,以绝对路径执行脚本)

    逻辑与&&前面执行成功后执行后面,如果前面执行不成功则取消 逻辑或|| 前面命令执行失败后,继续执行||后面的命令 ``优先执行反引号里的命令动作

    逻辑语判断是文件还是目录

    [root@Server yuan]# ls
    aaa  yunjisuan
    [root@Server yuan]# [ -d yunjisuan ]   #判断目录
    [root@Server yuan]# echo $?
    0
    [root@Server yuan]# [ -d aaa ]   #判断文件
    [root@Server yuan]# echo $?
    1
    [root@Server yuan]# [ -f aaa ]
    [root@Server yuan]# echo $?
    0
    

    逻辑语判断是大于还是小于

    (-gt:大于) (-lt:小于) (-eq:等于)
    (-le:小于等于)(-ne:不等于)(-ge:大于等于)

    read交互模式

    例如:

    #!/bin/bash
    read -p "请输入你的成绩:" b    #设置一变量带入脚本
    [ $b -lt 60 ] && echo "不及格"
    [ $b -eq 60 ] && echo "恰好及格"
    [ $b -ge 60 ] && [ $b -lt 70 ] && echo "良"
    [ $b -ge 70 ] && [ $b -lt 85 ] && echo "良好"
    [ $b -gt 85 ] && [ $b -le 100 ] && echo "优秀"
    [ $b -gt 100 ] && echo "作弊高手"
    
    [root@Server ~]# sh yunjisuan.sh     # 执行脚本
    请输入你的成绩:60
    bu'ji'ge 
    

    脚本传参格式

    [root@Server ~]# vim yunjisuan.sh
    #!/bin/bash
    echo $#
    echo $*
    echo $0
    echo $1
    echo $2
    echo $3
    echo $4
    [root@Server ~]# /root/yunjisuan.sh 1111 2222  #执行脚本
    2
    1111 2222
    /root/yunjisuan.sh
    1111
    2222
    
    • $# 传参数的参数总个数

    • $ 横向罗列穿入的参数*

    • $0 文件的绝对路径

    • $1对应传入的不同的参数

    • $n对应传入的不同的参数

    判断字符串个数和是否为空 echo ${#变量}

    [root@Server ~]# xx="" 
    [root@Server ~]# echo ${#xx}
    0
    [root@Server ~]# xx="22"
    [root@Server ~]# echo ${#xx}
    2
    [root@Server ~]# xx="22222"
    [root@Server ~]# echo ${#xx}
    5
    

    文本交互式插入内容

    [root@Server ~]# cat >yuan <<CFY
    > 写入内容
    > 写入内容
    > 写入内容
    >CFY
    [root@Server ~]#
    
    命令说明:
    >yuan 文件名字
    <<CFY 结束语
    

    脚本if条件判断

    条件单分支判断:如果条件1成功那么执行动作1,否则执行动作2

    #!/bin/bash
    read -p "请输入一个数字" x
    
    if [ $x == 60 ];then
            echo "猜对了"
    else
            echo "猜错了"
    fi
    [root@Server ~]# sh yunjisuan.sh   # 执行脚本
    请输入一个数字40
    猜错了
    [root@Server ~]# 
    

    多分支判断:如果条件1成立;那么执行动作1,否则如果条件2成立;执行动作2,否则条件1和2都不成立一律执行动作3

    多个条件时加 elif

    #!/bin/bash
    read -p "请输入一个数字" x
    
    if [ $x == 60 ];then
            echo "猜对了"
    elif [ $x -lt 60 ];then
            echo "猜小了"
    else
            echo "猜大了"
    fi
    [root@Server ~]# sh yunjisuan.sh    # 执行脚本
    请输入一个数字:60
    猜对了
    [root@Server ~]# 
    

    脚本for循环

    #!/bin/bash
    for a in 1 2 3 4 5
    do
            echo $a全部都是数字
    done
    [root@Server ~]# sh yunjisuan.sh    # 执行脚本
    1全部都是数字
    2全部都是数字
    3全部都是数字
    4全部都是数字
    5全部都是数字
    

    C语言的for循环

    h=0
    for b in {1..6}
    do
            echo $h
            ((h++))     # 或者 let h++ 
    done
    [root@Server ~]# sh yunjisuan.sh
    0
    1
    2
    3
    4
    5
    6
    [root@Server ~]# 
    
    命令说明:
    let h++
    不进行++的话,变量只会执行一次,++以后就会以此往上加
    

    脚本while无限循环

    read -p "随便输入一个数字:" h
    while [ $h -gt 0 ]
    do
            echo $h
            let h++
    done
    
    [root@Server ~]# sh yunjisuan.sh   # 执行脚本
    随便输入一个数字:5
    .....无限循环......
    56443
    53467
    55667
    .....无限循环......
    [root@Server ~]# 
    

    case 语句格式

    一般用于菜单选择,用于没有优先级的场合

    #!/bin/bash
    case $h in
            start)
                    echo "服务启动"
                    ;;
            stop)
                    echo "服务停止"
                    ;;
            restart)
                    echo "服务准备停止"
                    echo "服务开始启动"                
                    ;;
            *)
                    echo "输入错误"
                    ;;
    esac
    
    [root@Server ~]# /root/yunjisuan.sh start   # 绝对路径执行
    服务启动
    [root@Server ~]# /root/yunjisuan.sh stop
    服务停止
    [root@Server ~]# /root/yunjisuan.sh restart
    服务准备停止
    服务开始启动
    

    在脚本中引用一个函数库

    #!/bin/bash
    
    . /etc/init.d/functions
    
    case $h in
            start)
                    action "服务启动" /bin/true
                    ;;
            stop)
                    action "服务停止" /bin/false
                    ;;
            restart)
                    action "服务准备停止" /bin/true
                    action "服务开始启动" /bin/false              
                    ;;
            *)
                    action "输入错误"
                    ;;
    esac
    
    [root@Server ~]# /root/yunjisuan.sh start   # 绝对路径执行
    服务启动                                    [  OK  ]
    [root@Server ~]# /root/yunjisuan.sh stop
    服务停止                                    [FAILED]
    [root@Server ~]# /root/yunjisuan.sh restart
    服务准备停止                                [  OK  ]
    服务开始启动                                [FAILED]
    [root@Server ~]# 
    

    把脚本加入到chkconfig里进行管理

    [root@Server ~]# cp /root/yunjisuan.sh /etc/init.d/
    [root@Server ~]# vim /etc/init.d/yunjisuan.sh
    #!/bin/bash
    #chkconfig: 35 90 10
    ......一下忽略.....
    
    [root@Server ~]# chkconfig --add yunjisuan.sh
    [root@Server ~]# chkconfig --list yunjisuan.sh
    .....省略.....
    

    函数的格式用法

    #!/bin/bash
    
    function CFY(){
    echo "吃饭"
    echo "上班"
    echo "睡觉"
    echo "111111111"
    }
    
    CFY
    CFY
    
    [root@Server ~]# sh yunjisuan.sh   # 执行脚本
    吃饭
    上班
    睡觉
    111111111
    吃放
    上班
    睡觉
    111111111
    

    脚本4种循环控制语句

    • exit 强行终止脚本
    #!/bin/bash
    h=0
    while [ $h -lt 20 ]
    do
        if [ $h -eq 5 ];then
                exit
        fi
            echo $h
            let ++
    done
    
    echo "脚本继续执行"
    
    [root@Server ~]# sh yunjisuan.sh # 执行脚本
    0
    1
    2
    3
    4
    5
    
    • break 退出当前最近的循环
    #!/bin/bash
    h=0
    while [ $h -lt 20 ]
    do
        if [ $h -eq 5 ];then
                break
        fi
            echo $h
            let ++
    done
    
    echo "脚本继续执行"
    
    [root@Server ~]# sh yunjisuan.sh # 执行脚本
    0
    1
    2
    3
    4
    5
    脚本继续执行
    
    • continue 终止当前本次循环进入下次循环
    #!/bin/bash
    h=0
    while [ $h -lt 20 ]
    do
        let $h++
        if [ $h -eq 5 ];then
                continue 
        fi
            echo $h
    done
    
    echo "脚本继续执行"
    
    [root@Server ~]# sh yunjisuan.sh # 执行脚本
    0
    1
    ...忽略...
    19
    20
    脚本继续执行
    
    • return 强行跳出函数体系从哪开始到那继续
  • 相关阅读:
    centos7下安装erlang
    centos7下升级git版本
    pytest失败重跑插件: pytest-rerunfailures使用与坑(全网独家精华)
    pytest-assume插件(全网最详细解释):多重断言执行
    pytest踩坑记:NameError: name 'pytest' is not defined
    pytest-ordering:指定pytest的case运行顺序的插件
    pytest中print的坑
    pytest测试入门篇(ExitCode退出码)
    httprunner3.x遇到的问题(hrun make报错)
    httprunner3.x(入门介绍篇)
  • 原文地址:https://www.cnblogs.com/cfy930721/p/12792973.html
Copyright © 2011-2022 走看看