脚本学习

这篇博客介绍了Shell脚本的基础知识,包括如何创建脚本、规范要求、常用命令如diff和grep,以及逻辑分隔符、变量、四则运算和脚本中的常用语句。通过多个实例,如不同方法的操作演示,详细解释了脚本的编写和应用。
摘要由CSDN通过智能技术生成

一、初识脚本

shell:
    一类介于系统内核与用户之间的解释程序。
脚本:
    一类使用特定语言,按预设顺序执行的文件,批处理、宏、解释型程序

二、编写shell脚本

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

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

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

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

举例:
[root@shell-script test]# vim hello.sh
#!/bin/bash
echo hello
[root@shell-script test]# ls
hello.sh
[root@shell-script test]# chmod +x hello.sh 
[root@shell-script test]# bash hello.sh 
hello
[root@shell-script test]# sh hello.sh 
hello
[root@shell-script test]# /test/hello.sh 
hello
2)规范要求
1)脚本中应添加脚本的说明信息
Author      :hx                           
Email       :hx@westos.com
Create_Date :2017-08-21
Vesion      :1.0
Description :test scripts

2)脚本中尽量不要使用中文,哪怕用拼音代替

3)脚本中出现的()|[]|{}|<>等等成对出现的符号要一次打出,并且
内容中的字符与这些符号要有空格

4)脚本中的语句要边写边丰富内容,一气呵成的脚本绝对是少数

5)语句中使用的动作要缩进写入,使脚本易读

6)如何让vim自动添加脚本说明
方法一:
脚本中加入子脚本(不推荐)
#!/bin/bash
cat > $1 <<EOF
############################################ 
# Author                :hx 
# Email                 :hx.westos.com 
# Version               : 
# Create_Date           : 2017-08-22
# Description           : 
############################################# 
EOF
vim $1

方法二:
编辑配置文件(推荐)
vim /etc/vimrc
map <F4> ms:call AddTile()<cr>'s        ##设定快捷键
function AddTile()                      ##设定函数内容
        call append(0,"# Author         :Lee")
        call append(1,"# Email          :lee@westos.com")
        call append(2,"# Version        :")
        call append(3,"# Create_Date    :".strftime("%Y-%m-%d"))
        call append(4,"# Description    :")
endfunction
3)常用命令

1.diff

比较两个文件的不同
diff
[num1,num2][a|c|d][num3,num4]
a       添加
c       改变
d       删除
num1,num3   第一个文件的内容
num3,num4   第二个文件的内容

这里写图片描述

[root@shell-script test]# vimdiff file1 file2
##这种方法的比较会比较明显,不同的地方高亮显示,而且,背景是粉红色,非常的少女心哦~

这里写图片描述

diff -r dir1 dir2         ##递归比较子目录
##recursively compare any subdirectories found         
举例:
[root@shell-script linux]# cat /test/westos/haha
/test/westos/haha
[root@shell-script linux]# cat /test/linux/heihei
/test/linux/heihei
[root@shell-script linux]# diff -r /test/westos/ /test/linux/
Only in /test/westos/: haha
Only in /test/linux/: heihei
diff -u file1 file2 
##output unified (default 3) lines of unified context

举例:
[root@shell-script linux]# cd /test/
[root@shell-script test]# ls
file1  file2  linux  westos
[root@shell-script test]# diff -u file1 file2
--- file1   2017-08-22 21:24:43.366365584 -0400
+++ file2   2017-08-22 21:23:15.493365584 -0400
@@ -1,2 +1,2 @@
 hello world
-124
+123

[root@shell-script test]# diff -u file1 file2 > file.path
##将此文件内容重定向到一个新的文件中,以便后续作布丁使用
patch  file file.path                     ##需要安装patch软件

这里写图片描述

patch -b file file.path     #-b标示备份原文件为file.orig

这里写图片描述

2.grep

    Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。
    grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户。

参数:

-i :忽略大小写
## Ignore  case  distinctions  in  both  the PATTERN and the input files.  (-i is specified by POSIX.)

举例:
[root@shell-script test]# vim file1
[root@shell-script test]# cat file1
hello world
HELLO WORLD
[root@shell-script test]# grep hello file1           ##默认情况下,按标准输出查找
hello world
[root@shell-script test]# grep HELLO file1
HELLO WORLD
[root
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值