1.什么是脚本
shell脚本其实就是纯文本文档,我们可以编辑这个文档,然后让这个档案来帮助我们
一次执行多个指令,或者达成某些特定功能。

安装一定逻辑关系记录明令的文件
在此文件有可执行权限的情况下可以用文件名称发起脚本内记录明令的执行
shell脚本是一种解释形语言,文件内记录的动作需要解释器shell

2.如何建立一个shell脚本
1)
vim test.sh
#一般情况下脚本的结尾为".sh"这不是系统规定的,但是是一种业界规范

linux-shell scripts 脚本的初步认识
2)
#!/bin/bash
#脚本头的写法,这是脚本使用的解释器,也就是默认脚本运行时开启的子shell


3)
脚本内容是用明令和明令执行的逻辑关系组成

4)
    脚本执行的方式
方法一:
    sh 脚本名称
方法二:
    chmod +x 脚本
    脚本名称调用


linux-shell scripts 脚本的初步认识


[[email protected] mnt]# vim test.sh
#!/usr/bin/env bash     不同系统位置不同


linux-shell scripts 脚本的初步认识


3.编写脚本的规范
1)脚本中应添加脚本的说明信息

Author        :lee
Email        :[email protected]
Create_Date    :2017-08-21
Vesion        :1.0
Description    :test scripts


2)脚本中尽量不要使用中文,哪怕用拼音代替
3)脚本中出现的()|[]|{}|<>等等成对出现的符号要一次打出,并且
内容中的字符与这些符号要有空格
4)脚本中的语句要一次写完在丰富内容
5)语句中使用的动作要缩进写入,使脚本易读
6)如何让vim自动添加脚本说明

map <F4> ms:call AddTile()<cr>'s    ##设定快捷键
function AddTile()            ##设定函数内容
        call append(0,"# Author         :Lee")
        call append(1,"# Email          :[email protected]")
        call append(2,"# Version        :")
        call append(3,"# Create_Date    :".strftime("%Y-%m-%d")."#")
        call append(4,"# Description    :")
endfunction



4.变量

1)作用:用来调用一个数值,或者字符的值。

2)变量的命名规则:

首字母不能是数字
变量中只能包含 字母 数字和"_"
TEST
TEST_REDHAT
Test_Redhat
tesTRedhat


5.特殊符号转译和注释


\    转译单个字符
' '    强引用
" "    弱引用 其转译功能不能转译"!" "$" "\" "`"


linux-shell scripts 脚本的初步认识



linux-shell scripts 脚本的初步认识

linux-shell scripts 脚本的初步认识

linux-shell scripts 脚本的初步认识


例用脚本实现: 将 /var/.log   /mnt/2017_12_11


[[email protected] mnt]# vim backup.sh  编辑脚本内容


linux-shell scripts 脚本的初步认识



测试:


linux-shell scripts 脚本的初步认识