Linux之shell学习(一)——shell基础、脚本注释栏的自动生成

一、概述

什么是 shell

shell 也是操作系统中的一个软件,它包在 linux 内核的外面,为用户和内核之间的交互提供了一个接口系统中的命令
用shell 去解释,shell 接收系统回应的输出并显示其到屏幕中
bash = GNU Bourne-Again Shell

什么是 shell 脚本

脚本是一种解释型语言
优点:
用 shell 脚本保存执行动作
用脚本判定命令的执行条件
用脚本来实现动作的批量执行

二、编写简单的shell脚本

1、编写简单程序

vim hello.sh          ##注意,脚本一般用.sh结尾,这样写脚本时会高亮显示,而不加.sh也能编写脚本,但是不能高亮显示
cat hello.sh
#!/bin/bash           ##脚本使用的解释器,通常用幻数 "#!" 指定,这里的#不是注释
echo hello world!!!

在这里插入图片描述

2、执行shell脚本

sh hello.sh				##sh 后面直接跟脚本名称,source script.sh也可以执行


chmod +x hello.sh		##或者给脚本可执行权限,然后绝对路径执行
./hello.sh

3、查看脚本的运行过程

sh -x hello.sh        ##-x是查看过程

4、关于shell的类型

vim hello.sh 
cat hello.sh          ##SHELL是有很多种的,bash可以,这里修改为sh也可以,但是不能随便修改,否则会报错的
#!/bin/sh
echo hello world!!!
./hello.sh 

在这里插入图片描述

三、SHELL编程注释

一般shell脚本要注上以下信息,作者、版本、邮箱、日期、描述

    # Author:               Humble1015
    # Version:              1.0
    # Mail:                 [email protected]
    # Date:                 2019-3-15
    # Description:          

但是每个脚本如果都这样写太麻烦了,所以就需要自动生成注释信息

1、快捷键产生

vim /etc/vimrc 
    写在最后面
    map <F5>  ms:call TAYLOR()<cr>'s                   ##按下F5,调用TAYLOR()函数,这里函数名要大写
     
    function TAYLOR ()                                      ##这是我们自己写的一个函数
            call append(0,"#################################")         ##调用append函数,作用是打印,0表示第一行,打印引号里面的内容
            call append(1,"# Author:           jiekouma        #") 1表示第二行
            call append(2,"#Version: 1.0                  #")       ##多个信息用.分开,后面信息用(“ ”)包起来,这个其实可以不写,和上面一行格式一以也可以
            call append(3,"# Mail:           			 #")
            call append(4,"#Create_Date:  ".strftime("%Y-%m-%d")."     #")    ##这里调用时间函数,以年月日的形式显示
            call append(5,"# Description                   #")
            call append(6,"#################################")
            call append(7,"")
            call append(8,"#!/bin/bash")
    endfunction                                     ##函数结尾标志

测试

进入vim编辑器中,按下F5(Fn+F5)就可以产生
在这里插入图片描述

2、自动产生

修改/etc/vimrc的内容

    "map <F5>  ms:call TAYLOR()<cr>'s                   ##单引号注释掉
    autocmd BufNewFile *.sh,*.script exec ":call TAYLOR()"       ##如果是新的文件,并且是以.sh为后缀的脚本,那么执行调用TAYLOR()函数的动作
    
    function TAYLOR ()
		call append(0,"#################################")
		call append(1,"#Author:         jiekouma      #")
		call append(2,"#Version: 1.0                  #")
		call append(3,"#Mail:                         #")
		call append(4,"#Create_Date:  ".strftime("%Y-%m-%d")."     #")
		call append(5,"#Description:                  #")
		call append(6,"###############################")
		call append(7," ")
		call append(8,"#!/bin/bash")
endfunction

当我们编辑一个新的脚本时,就会自动生成注释信息
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43570369/article/details/88578367
今日推荐