• Shell基础知识和编程规范


    一,Shell环境查看

    1.1 查看系统Shell支持情况
    [root@linux-node1 ~]# cat /etc/shells 
    /bin/sh
    /bin/bash
    /sbin/nologin
    /usr/bin/sh
    /usr/bin/bash
    /usr/sbin/nologin
    1.2 查看当前系统默认的Shell

    方法一

    [root@linux-node1 ~]# echo $SHELL
    /bin/bash

    方法二

    [root@linux-node1 ~]# grep root /etc/passwd
    root:x:0:0:root:/root:/bin/bash

    提示:后面/bin/bash就是用户登陆后的Shell解释器

    二,Shell脚本的建立和执行

    2.1 脚本开通(第一行)

    一个规范的Shell脚本在第一行会指出由哪个程序(解释器)来执行脚本中的内容,这一行内容在Linux bash的编程一般为

    #!/bin/bash
    或
    #!/bin/sh #<---255个字符以内

    其中,开头的“#!"字符又称为幻数,在执行bash脚本的时候,内核会根据“#!”后面的解释器来确认哪个程序解释这个脚本中的内容(注意 这一行必须在每个脚本顶端第一行)

    2.2 bash与sh的区别

    sh为bash的软连接

    [root@linux-node1 ~]# ll /bin/sh
    lrwxrwxrwx. 1 root root 4 Aug  6  2016 /bin/sh -> bash
    2.3 查看系统版本
    [root@linux-node1 ~]# cat /etc/redhat-release 
    CentOS Linux release 7.2.1511 (Core) 

    查看bash版本

    [root@linux-node1 ~]# bash --version
    GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)
    Copyright (C) 2011 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    
    This is free software; you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.

    测试bash是否有破壳漏洞,如果返回be careful,则表示需要尽快升级bash了

    [root@xiewenming]# bash --version
    GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
    Copyright (C) 2009 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    
    This is free software; you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.
    [root@xiewenming]# 
    [root@xiewenming]# env x='() { :;}; echo be careful' bash -c "echo this is a test"
    be careful
    this is a test
    2.4 升级bash方法
    [root@xiewenming]# yum -y update bash
    [root@xiewenming]# rpm -qa bash
    bash-4.1.2-48.el6.x86_64
    [root@xiewenming]# env x='() { :;}; echo be careful' bash -c "echo this is a test"
    this is a test  #再次执行已没有be careful提示

    提示:如果没有输出be careful,则不需要升级

    2.5 脚本注释

    在Shell脚本中,跟再#后面的内容表示注释,开发脚本时,如果没有注释,那么团队里的其他人就会很难理解加班对应内容的用途,而且时间长了自己也会忘记。为了方便别人和方便自己,避免影响团队的协助效率,

    要养成一个写关机注释的习惯。脚本注释尽量不要用中文,避免乱码问题

    2.6 Shell脚本的执行

    当Shell脚本运行时,它会先查找系统环境变量ENV,改变量指定了环境文件,加载顺利通车是/etc/profile ---> ~/.bash_profile ---> ~/.bashrc --->/etc/bashrc等,在加载了上述的环境变量文件后,Shell就开始执行脚本中的内容

    通常情况下,在执行Shell脚本时,会向系统请求启动一个新的进程,以便在该进程中执行脚本的命令及子Shell脚本。

    提示:设置crond任务时,最好能再定时任务脚本中重新定义系统环境变量,否则,一些系统环境变量将不会被加载,这个问题需要注意!

    Shell脚本的执行可以采用以下几种方式:

    1).bash 脚本名 或sh 脚本名 (推荐使用)

    [root@linux-node1 ~]# bash test01.sh 
    Welcome warren
    [root@linux-node1 ~]# sh test01.sh 
    Welcome warren

    2). 使用脚本的绝对路径执行 或相对路径 ./脚本名 这个需要有文件的执行权限

    [root@linux-node1 ~]# chmod +x test01.sh 
    [root@linux-node1 ~]# /root/test01.sh
    Welcome warren
    [root@linux-node1 ~]# ./test01.sh
    Welcome warren

    3).source 脚本名 或  . 脚本名

    [root@linux-node1 ~]# source test01.sh 
    Welcome warren
    [root@linux-node1 ~]# . test01.sh 
    Welcome warren

    4) sh < 脚本名 或 cat 脚本名|sh

    [root@linux-node1 ~]# cat test01.sh 
    echo "Welcome warren"
    [root@linux-node1 ~]# sh < test01.sh 
    Welcome warren
    [root@linux-node1 ~]# cat test01.sh |bash
    Welcome warren

    使用cat 创建脚本文件

    [root@linux-node1 ~]# cat > test02.sh
    echo "this is test"

    Ctrl+d结束编辑,这里作为cat用法的扩展知识

    父Shell不会继承子Shell的环境变量,测试如下

    [root@linux-node1 ~]# cat test.sh 
    user001=`whoami`
    [root@linux-node1 ~]# echo $user001
    #空
    [root@linux-node1 ~]# 

    使用source导入子脚本的环境变量到当前环境就可以获取子脚本的变量

    [root@linux-node1 ~]# source  test.sh 
    [root@linux-node1 ~]# echo $user001
    root

    三, Shell脚本开发的基本规范

    Shell 脚本的开发规范及习惯非常重要,有了好的规范可以大大提升开发效率,并能再后期降低对脚本的维护成本。

    1). Shell脚本的第一行是指定脚本介绍器

    #!/bin/bash

     2). Shell脚本的开通会加班表,版权等信息

    #!/bin/bash
    #Date: 2017-12-5
    #Author: Created by warren
    #Blog: http://www.cnblogs.com/xiewenming/
    #Description: This scripts function is ...
    #Version:1.1

    可以修改“~/.vimrc”配置文件配置vim编辑文件时自动加上以上信息

    3).  在Shell脚本中尽量不用中文,防止切换系统环境后中文乱码的困扰。如果非要加中文,请根据系统进行字符集调整.

          如: export LANG="zh_CN.UTF-8",并在脚本中,重新定义字符集设置,和系统保证一致。

    4). Shell脚本的命名应以 .sh 为扩展名

         例如: test.sh

    5). Shell 脚本应存放在固定的目录下

         例如:/opt/scripts

    四, Shell脚本书写的良好习惯

    1).成对的符合应尽量一次性写出来,然后退格在符合里增加内容,以防止遗漏。

    这些成对的符合包括:

    {}  []  ''  ``  ""

    2). 中括号两端至少各一个空格,先退2格 然后进一格,双括号也是如此

    3). 对应流程控制语句,应一次将格式写完,再添加内容。

     比如,一次性完成if语句的格式,应为

    if 条件内容
      then
        内容
    fi

    一次性完成for循环语句格式,应为

    for
    do
      内容
    done

    一次完成white语句格式,应为

    white 条件
    do
      内容
    done

    提示:until和case语句也一样

    4). 通过缩进让代码更易读

    5). 对应常规变量的字符串定义变量值应加双引号,并且等号前后不能有空格

    6). 脚本中的单引号和双引号必须为英文状态下的符合

    7). 变量名称应该具有相关意思,不能太随便。

    说明:好的习惯可以让我们避免很多不必要的麻烦并提示工作效率

  • 相关阅读:
    002Python和JS的不同进制之间的转换实现
    001JS中的非严格相等 ==
    028_同步本地git项目到github和码云
    015你所常见的日常英语
    001CSS三大特性
    014国家地区语言缩写码
    013常用的英语词典Share
    012_犯人的夏日的蚊虫叮咬词汇
    011_中文"上火"的英文怎么说
    我的IT之路这样走过
  • 原文地址:https://www.cnblogs.com/xiewenming/p/7985508.html
Copyright © 2020-2023  润新知