• 自动化运维——HelloWorld(一)


    1.HelloWorld

    vi first_shell.sh
    #!/bin/bash
    #Filename: first_shell.sh
    #auto echo hello world!
    #by authors steve yu 2019
    
    echo "Hello World!"
    chmod o+x first_shell.sh
    ./first_shell.sh
    或者
    /bin/bash first_shell.sh

    前三行的shell注释规范

    第一行,表名是shell脚本文件

    第二行注明文件名

    第三行注明脚本作用

    第四行注明作者

    2.变量

    shell编程语言是典型的解释型语言,不像c++/Java语言编程时需要先声明变量,shell给一个变量赋值,实际上就是定义了变量,在linux支持的所有shell中,都可使用赋值符号“=”进行遍历赋值

    shell遍历可以分为两类,局部变量和环境变量,局部变量只在创建他们的shell脚本中使用,而环境变量则可以创建它们的shell及派生出来的子进程中使用,有些变量是用户创建的,其他是专用的shell变量

    加入脚本定义A=123,定义这种变量,前面是变量名,后面试变量值

    引用变量可以使用$A,把变量放在脚本中会出现什么效果呢?


    #!/bin/bash
    #define path variables
    #by authors steve yu

    A=123
    name="steve yu"
    echo "This is my First Shell var $A"
    echo "My name is $name"

    #输出系统变量

    echo $UID
    echo $PWD
    echo $0 #当前脚本
    echo $1 #第一个参数
    echo $2 #第二个参数

    echo $? #判断上一个命令是否正确,正确?是0,其他都是执行错误
    echo $* #所有参数
    echo $# #参数个数

    #显示颜色
    echo -e '33[32m-------------------------------33[0m'

    书写菜单

    #!/bin/bash
    #by authors steve yu 2019
    
    echo -e "33[32mPlease select menu follow33[0m"
    
    echo "1)安装apache服务器"
    echo "2)安装mysql服务器"
    echo "3)安装php服务器"
    echo "4)配置LAMP WEB架构"

    3.if语句

    #!/bin/bash
    #auto if test
    #by authors steve 2019
    
    NUM1=100
    NUM2=200
    
    if (($NUM1>$NUM2));then
            echo "This $NUM1 greate $NUM2 !" 
    else
            echo "This $NUM2 greate $NUM1 !" 
    fi

    逻辑运算解析

    -f 判断文件是否存在 if [ -f filename]

    -d 判断目录是否存在 if [ -d dir]

    -eq 等于

    -ne 不等于

    -lt  小于

    -gt  大于

    -le  小于等于

    -ge  大于等于

    -a   双方都成立(and)逻辑表达式

    -o   单方成立(or)逻辑表达式


    案例,判定目录是否存在

    #!/bin/bash
    #auto make dir 
    #by authors steve yu 2019
    
    DIR=/tmp/2019/8/17
    
    if [ ! -d $DIR ];then
            mkdir -p $DIR
            echo -e "33[32mThis $DIR create success!33[0m"
    else
            echo -e "33[32mThis $DIR is exist,Please exit.33[0m"
    fi

    截图:

    运行结果:

     判断文件是否存在

    代码:

    #!/bin/bash
    #auto test files
    #by authors steve yu
    
    FILES="/root/a.txt"
    
    if [ ! -f $FILES ];then
            echo "ok" >> $FILES
    else
            cat $FILES
    fi

    截图:

    运行结果:

     比较两数大小

     代码:

    #!/bin/bash
    #by steve yu
    
    echo -e "33[32m-------------------33[1m"
    
    if [[ $1 -gt $2 ]]; then
            echo  "第一个变量大于第二个变量";
    elif [[ $1 -eq $2 ]]; then
            echo  "第一个变量等于第二个变量";
    else
            echo  "第一个变量小于第二个变量";
    fi
    
    echo -e "33[0m"

     截图:

     测试:

    视频来源:av8104450

    今日刷课【3/20】

  • 相关阅读:
    update 更改字段
    查看 links
    oracle 统计字段空值数
    查看oracle 数据库编码个格式
    oracle 数据库DBA权限
    一文弄懂 Golang 排序
    Golang Linux、Windows、Mac 下交叉编译
    go 简单封装数学运算包
    [Vue warn]: Unknown custom element: did you register the component correctly?
    深度图怎么看行情走势
  • 原文地址:https://www.cnblogs.com/littlepage/p/11367191.html
Copyright © 2020-2023  润新知