shell编程(一) | shell基本知识

一,概述

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

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

二,从C语言开始

1,编写c语言程序

[root@100 mnt]# vim hello.c                     >>>>后缀一般为.c
[root@100 mnt]# cat hello.c
#include <stdio.h>
main()
{
	printf("hello world\n");
}

2,安装c语言编译器

[root@100 mnt]# yum install gcc -y

3,编译源码

[root@100 mnt]# gcc hello.c                     >>>>>编译之后文加默认是是a.out
[root@100 mnt]# ls
a.out  hello.c               
[root@100 mnt]# ./a.out                         >>>>>绝对路径执行
hello world
[root@100 mnt]# gcc hello.c -o  hello           >>>> -o 指定输出文件名为hello
[root@100 mnt]# ls
a.out  hello  hello.c
[root@100 mnt]# ./hello                         >>>> 绝对路径执行
hello world

三,简单的SHELL编程

1,编写简单程序

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

2,执行shell脚本

[root@100 mnt]# sh hello.sh           >>>>>>sh 后面直接跟脚本名称!!!!!!!source script.sh也可以执行
hello world
[root@100 mnt]# chmod +x hello.sh     >>>>>>或者给脚本可执行权限,然后绝对路径执行
[root@100 mnt]# ./hello.sh
hello world

3,查看脚本的运行过程

[root@100 mnt]# sh -x hello.sh        >>>>>-x是查看过程

4,关于shell的类型

扫描二维码关注公众号,回复: 1712076 查看本文章
[root@100 mnt]# vim hello.sh 
[root@100 mnt]# cat hello.sh          >>>>>>SHELL是有很多种的,bash可以,这里修改为sh也可以,但是不能随便修改,否则会报错的
#!/bin/sh
echo hello world
[root@100 mnt]# /mnt/hello.sh 
hello world

四,SHELL编程注释

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

# Author:               Minz
# Version:              1.0
# Mail:                 [email protected]
# Date:                 2018-6-10
# Description:          
#
#

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

1,快捷键产生

[root@100 mnt]# vim /etc/vimrc 
写在最后面
map <F4>  ms:call WESTOS()<cr>'s                    >>>>>按下F4,调用WETSOS()函数,这里函数名要大写

func WESTOS ()                                      >>>>>这是我们自己写的一个函数
        call append(0,"#################################")         >>>>调用append函数,作用是打印,0表示第一行,打印引号里面的内容
        call append(1,"# Author:           Minz        #") 1表示第二行
        call append(2,"# Version:              ".("        #"))       >>>>多个信息用.分开,后面信息用(“ ”)包起来,这个其实可以不写,和上面一行格式一以也可以
        call append(3,"# Mail:           [email protected] #")
        call append(4,"# Date:        ".strftime("%Y-%m-%d").("       #"))    >>>>>这里调用时间函数,以年月日的形式显示
        call append(5,"# Description                   #")
        call append(6,"#                               #")
        call append(7,"#################################")
        call append(8,"")
        call append(9,"#!/bin/bash")
endfunc                                     >>>>>>>> 函数结尾标志

测试

进入vim编辑器中,按下F4(Fn+F4)就可以产生

#################################
# Author:           Minz        #
# Version:                      #
# Mail:           [email protected] #
# Date:        2018-06-10       #
# Description                   #
#                               #
#################################

#!/bin/bash

2,自动产生

修改/etc/vimrc的内容

"map <F4>  ms:call WESTOS()<cr>'s                   >>>>>单引号注释掉
autocmd BufNewFile *.sh exec ":call WESTOS()"       >>>>如果是新的文件,并且是以.sh为后缀的脚本,那么执行调用WESTOS()函数的动作

func WESTOS ()
        call append(0,"#################################")
        call append(1,"# Author:           Minz        #")
        call append(2,"# Version:              ".("        #"))
        call append(3,"# Mail:           [email protected] #")
        call append(4,"# Date:        ".strftime("%Y-%m-%d").("       #"))
        call append(5,"# Description                   #")
        call append(6,"#                               #")
        call append(7,"#################################")
        call append(8,"")
        call append(9,"#!/bin/bash")
endfunc
当我们编辑一个新的脚本时,就会自动生成注释信息

猜你喜欢

转载自blog.csdn.net/ha_weii/article/details/80686138