zoukankan      html  css  js  c++  java
  • shell脚本(2)-shell脚本语法

    一、如何抒写shell脚本

    1、shell脚本的命名

    名字要有意义,不要以a、b、c、1、2、3这种方式命令,建议以sh结尾,在30个字节内,例如:check_memory.sh

     2、shell脚本的格式

    shell脚本开头必须指定运行环境以#!这个特殊组合来组成,如:#!/bin/bash指该脚本运行解析由/bin/bash来完成

    shell的注释使用#号(执行脚本的注释最好使用英文)(#!是特例)

    #Author:Mr White
    #Created Time:2021/07/17 00:50
    #Srcipt Description:nginx install script

     

    二、shell脚本运行方法

    1、脚本运行需要执行权限,赋予执行权限后,该脚本可以运行

    chmod u+x filename

    2、不希望赋予脚本执行权限,那么可以使用base命令来运行未给予执行权限的脚本

    base filename

     

    三、shell中的特殊符号

    不要和正则表达式中的符号含义搞混淆了。

    ~:家目录 #cd ~代表进入家目录

    ! :执行历史记录   !!执行上一条命令

    $:变量中取内容符

    +  -  * / %  :加减乘除余

    &:后台执行

    *:通配符,匹配所有

    ?:通配符,匹配一个字符

    ;:一行执行多个命令,用分号分隔

    |:管道符,上一个命令的输出作为下一个命令的输入

    :转义字符

    ``:反引号,命令中执行命令

    ' ':单引号,脚本中字符串用单引号引起来,不同与双引号是的,单引号不解释变量(两个单引号可以)

    " ":双引号,脚本中出现的字符串可以用双引号引起来

     

    四、管道

    | :管理符在shell中使用最多,很多组合命令都需要通过管道来完成输出,管理符其实就是下一个命令对上一个命令的输出做处理

    [root@localhost ~]# cat /etc/passwd | grep "root"
    root:x:0:0:root:/root:/bin/bash
    operator:x:11:0:operator:/root:/sbin/nologin

     

    五、重定向

    1、重定向输出:覆盖原数据

    [root@localhost test20210718]# echo haha > ./test.txt
    [root@localhost test20210718]# cat test.txt
    haha
    [root@localhost test20210718]# echo hehe > ./test.txt
    [root@localhost test20210718]# cat test.txt
    hehe

    2、重定向追加输出:在原数据的末尾添加

    [root@localhost test20210718]# echo zhuijia1 >> test.txt
    [root@localhost test20210718]# echo zhuijia2 >> test.txt
    [root@localhost test20210718]# echo zhuijia3 >> test.txt
    [root@localhost test20210718]# cat test.txt 
    hehe
    zhuijia1
    zhuijia2
    zhuijia3

    3、重定向输入:

    [root@localhost test20210718]# wc < test.txt #返回 行 单词 字节
     4  4 32

    4、重定向追加输入:

    [root@localhost test20210718]# wc << EOF
    > hello world!!
    > EOF
     1  2 14

    六、shell脚本中的数学运算

    1、expr命令:只能做整数运算,格式比较古板,注意空格

    [root@localhost test20210718]# expr 2 + 3
    5
    [root@localhost test20210718]# expr 2 - 3
    -1
    [root@localhost test20210718]# expr 2 * 3
    6
    [root@localhost test20210718]# expr 2 / 3
    0
    [root@localhost test20210718]# expr 2 % 3
    2

    2、使用bc计算器处理浮点运算,scale=2代表小数点保留两位

    [root@localhost test20210718]# free -mh
                  total        used        free      shared  buff/cache   available
    Mem:           972M        176M        435M        7.7M        360M        647M
    Swap:          2.0G          0B        2.0G
    [root@localhost test20210718]# expr 435 / 972
    0
    [root@localhost test20210718]# echo "`echo  "scale=2;435*100/972" | bc`%"
    44.75%

    3、使用let对整数做运算,需要赋值变量

    [root@localhost test20210718]# let a=5+1
    [root@localhost test20210718]# echo $a
    6

    4、(())运算符,处理整形

    [root@localhost test20210718]# echo $((1+2))
    3

    七、脚本退出

    exit NUM退出脚本,释放系统资源,NUM代表一个整数,代表返回值

  • 相关阅读:
    模块 hashlib模块
    设计模式
    类中双下方法
    工作小结 常见定制类
    python collections模块
    启动脚本
    anaconda镜像
    理解python的可变参数
    使用spark
    python 异常处理
  • 原文地址:https://www.cnblogs.com/mrwhite2020/p/15017967.html
Copyright © 2011-2022 走看看