Linux入门-第三周 1、总结vim命令行模式常见快捷方式,以及vim查找,替换的方法 2、总结脚本中运算符、逻辑运算以及用法 3、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到 /root/etcYYYY-mm-dd中 4、编写脚本/root/bin/nologin.sh和login.sh,实现禁止和充许普通用户登录系统 5、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值

Linux入门-第三周
1、总结vim命令行模式常见快捷方式,以及vim查找,替换的方法
2、总结脚本中运算符、逻辑运算以及用法
3、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到 /root/etcYYYY-mm-dd中
4、编写脚本/root/bin/nologin.sh和login.sh,实现禁止和充许普通用户登录系统
5、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值

  vim [options] [file ..]

    +#  打开文件后,让光标处于第#行的行首,(默认行尾)

      举例vim +10 /etc/passwd      (光标调至第十行)如下图

    Linux入门-第三周
1、总结vim命令行模式常见快捷方式,以及vim查找,替换的方法
2、总结脚本中运算符、逻辑运算以及用法
3、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到 /root/etcYYYY-mm-dd中
4、编写脚本/root/bin/nologin.sh和login.sh,实现禁止和充许普通用户登录系统
5、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值  

  +/PATTERN打开文件后,直接让光标处于第一个被PATTERN匹配到的行的行首

      举例vim +/ga /etc/passwd    (让光标置于ga开头的行)如下图

    Linux入门-第三周
1、总结vim命令行模式常见快捷方式,以及vim查找,替换的方法
2、总结脚本中运算符、逻辑运算以及用法
3、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到 /root/etcYYYY-mm-dd中
4、编写脚本/root/bin/nologin.sh和login.sh,实现禁止和充许普通用户登录系统
5、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值

  -d file1 file2 ...   比较多个文件

    Linux入门-第三周
1、总结vim命令行模式常见快捷方式,以及vim查找,替换的方法
2、总结脚本中运算符、逻辑运算以及用法
3、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到 /root/etcYYYY-mm-dd中
4、编写脚本/root/bin/nologin.sh和login.sh,实现禁止和充许普通用户登录系统
5、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值

  -m file 只读方式打开文件(即使修改后:wq!也无法修改文件)

    Linux入门-第三周
1、总结vim命令行模式常见快捷方式,以及vim查找,替换的方法
2、总结脚本中运算符、逻辑运算以及用法
3、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到 /root/etcYYYY-mm-dd中
4、编写脚本/root/bin/nologin.sh和login.sh,实现禁止和充许普通用户登录系统
5、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值

  vim 常用快捷键

按键 功能 命令模式跳转按键 功能
i insert 在光标所在处插入 h
l 在当前光标所在行的行首插入 i
a append,在光标所在处后面输入 j
A 在当前光标所在行的行尾输入 k
o 在当前光标所在行的下方打开一个新行 #command 跳转由#指定的个数的字符
O 在当前光标所在行的上方打开一个新行 w 下一个单词的词首
Esc 从插入模式转换到命令模式 e 当前或下一个单词的词尾
拓展命令模式 b 当前或前一个单词的词首
Esc,enter 拓展命令模式转到命令模式 #command 由#指定一次跳转的单词数
:q 退出(拓展命令模式) H 页首
:q! 强制退出,不保存修改(拓展命令模式) M 页中间行
:wq 保存并退出(拓展命令模式) L 页低
:x 保存并退出(拓展命令模式) zt 将光标所在当前行移动到屏幕顶端
ZZ 保存并退出(命令模式) zz 将光标所在当前行移到屏幕中间
ZQ 不保存退出(命令模式) zb 将光标所在当前行移到屏幕底端

 下图是一张VIM的键盘布局图以供参考

 Linux入门-第三周
1、总结vim命令行模式常见快捷方式,以及vim查找,替换的方法
2、总结脚本中运算符、逻辑运算以及用法
3、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到 /root/etcYYYY-mm-dd中
4、编写脚本/root/bin/nologin.sh和login.sh,实现禁止和充许普通用户登录系统
5、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值

   命令模式:查找

    /PATTERN:从当前光标所在处向文件尾部查找

    ?PATTERN:从当前光标所在处向文件首部查找
    n:与命令同方向
    N:与命令反方向

    Linux入门-第三周
1、总结vim命令行模式常见快捷方式,以及vim查找,替换的方法
2、总结脚本中运算符、逻辑运算以及用法
3、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到 /root/etcYYYY-mm-dd中
4、编写脚本/root/bin/nologin.sh和login.sh,实现禁止和充许普通用户登录系统
5、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值

  拓展命令模式:查找并替换

    s: 在扩展模式下完成查找替换操作

    格式:s/要查找的内容/替换为的内容/修饰符

    要查找的内容:可使用模式

    替换为的内容:不能使用模式,但可以使用1, 2, ...等后向引用符号;还可以使用“&”引用前面查找时查找到的整个内容

    修饰符:

    i: 忽略大小写
    g: 全局替换;默认情况下,每一行只替换第一次出现
    gc:全局替换,每次替换前询问

   举例:全局查找var替换为user

     Linux入门-第三周
1、总结vim命令行模式常见快捷方式,以及vim查找,替换的方法
2、总结脚本中运算符、逻辑运算以及用法
3、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到 /root/etcYYYY-mm-dd中
4、编写脚本/root/bin/nologin.sh和login.sh,实现禁止和充许普通用户登录系统
5、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值

2、总结脚本中运算符、逻辑运算以及用法

  Bash 支持很多运算符,包括算数运算符、关系运算符、布尔运算符、字符串运算符和文件测试运算符。

  原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如awk和expr,expr最常用

  awk:Aho, Weinberger, Kernighan,报告生成器,格式化文本输出;awk 是一种很棒的语言,它适合文本处理和报表生成,其语法较为常见,借鉴了某些语言的一些精华,如 语言等。在 linux 系统日常处理工作中,发挥很重要的作用,掌握了 awk将会使你的工作变的高大上。 awk 是三剑客的老大,利剑出鞘,必会不同凡响。

  expr命令是一个手工命令行计数器,用于在UNIX/LINUX下求表达式变量的值,一般用于整数值,也可用于字符串。

    例如两个数相加:

1 [root@localhost data]# val=`expr 1 + 1`
2 [root@localhost data]# echo "Total value : $val"
3 Total value : 2
4 [root@localhost data]# 

  算术运算符:  

    举例:


 1 [root@VM_0_3_centos ~]# x=5
 2 [root@VM_0_3_centos ~]# y=10
 3 [root@VM_0_3_centos ~]# s=`expr $x + $y`
 4 [root@VM_0_3_centos ~]# echo "x + y = $s"
 5 x + y = 15
 6 [root@VM_0_3_centos ~]# 
 7 [root@VM_0_3_centos ~]# s=`expr $y - $x`
 8 [root@VM_0_3_centos ~]# echo "y - x = $s"
 9 y - x = 5
10 [root@VM_0_3_centos ~]# 
11 [root@VM_0_3_centos ~]# s=`expr $y * $x`
12 [root@VM_0_3_centos ~]# echo "y * x = $s"
13 y * x = 50
14 [root@VM_0_3_centos ~]# 
15 [root@VM_0_3_centos ~]# s=`expr $y / $x`
16 [root@VM_0_3_centos ~]# echo "y / x = $s"
17 y / x = 2
18 [root@VM_0_3_centos ~]# 
19 [root@VM_0_3_centos ~]# s=`expr $y % $x`
20 [root@VM_0_3_centos ~]# echo "y % x = $s"
21 y % x = 0
22 [root@VM_0_3_centos ~]# 
23 [root@VM_0_3_centos ~]# if [ $x == $y ]
24 > then
25 > echo "x is equal to y"
26 > fi
27 [root@VM_0_3_centos ~]# if [ $x != $y ]
28 > then
29 > echo "x is not equal to y"
30 > fi
31 x is not equal to y
32 [root@VM_0_3_centos ~]# 
View Code
运算符 说明 举例
+ 加法 `expr $x + $y`结果为15
- 减法 `expr $x - $ y`结果为-5
* 乘法 `expr $x * $y`结果为50
/ 除法 `expr $x / $y`结果为2
% 取于 `expr $x % $y`结果为0
= 赋值 x=$y 把变量y的值赋给x
== 相等,用于比较两个数字,相同则输出true [ $x == $y ] 返回 false
!= 不相等。用于比较两个数字,不相同则输出ture [ $x == $y ] 返回 true

注意:条件表达式要放在方括号之间,并且要有空格,例如 [$x==$y] 是错误的,必须写成 [ $x == $y ]。

  关系运算符:  -eq  -ne  -gt  -lt  -ge  -le

    举例

 1 [root@VM_0_3_centos ~]# #!/bin/sh
 2 [root@VM_0_3_centos ~]# x=5
 3 [root@VM_0_3_centos ~]# y=20
 4 [root@VM_0_3_centos ~]# if [ $x -eq $y ]
 5 > then
 6 > echo "$x -eq $y : x is equal to y"
 7 > else
 8 > echo "$x -eq $y: x is not equal to y"
 9 > fi
10 5 -eq 20: x is not equal to y
11 [root@VM_0_3_centos ~]# if [ $x -ne $y ]
12 > then
13 > echo "$x -ne $y: x is not equal to y"
14 > else
15 > echo "$x -ne $y : x is equal to y"
16 > fi
17 5 -ne 20: x is not equal to y
18 [root@VM_0_3_centos ~]# if [ $x -gt $y ]
19 > then
20 > echo "$x -gt $y: x is greater than y"
21 > else
22 > echo "$x -gt $y: x is not greater than y"
23 > fi
24 5 -gt 20: x is not greater than y
25 [root@VM_0_3_centos ~]# if [ $x -lt $y ]
26 > then
27 > echo "$x -lt $y: x is less than y"
28 > else
29 > echo "$x -lt $y: x is not less than y"
30 > fi
31 5 -lt 20: x is less than y
32 [root@VM_0_3_centos ~]# if [ $x -ge $y ]
33 > then
34 > echo "$x -ge $y: x is greater or equal to y"
35 > else
36 > echo "$x -ge $y: x is not greater or equal to y"
37 > fi
38 5 -ge 20: x is not greater or equal to y
39 [root@VM_0_3_centos ~]# if [ $x -le $y ]
40 > then
41 > echo "$x -le $y: x is less or equal to y"
42 > else
43 > echo "$x -le $y: x is not less or equal to y"
44 > fi
45 5 -le 20: x is less or equal to y
46 [root@VM_0_3_centos ~]# 
View Code
运算符 说明 举例
-eq 检查两个数是否相等,相等返回true

[ $x -eq $y ] 返回true

-ne 检查两个数是否相等,不相等返回true

[ $x -ne $y ] 返回true

-gt 检查左边的数是否大于右边的,如果大于,返回true

[ $x -gt $y ] 返回true

-lt 检查左边的数是否小于右边的,如果小于,返回true

[ $x -lt $y ] 返回true

-ge 检查左边的数是否大于等于右边的,如果大于等于,则返回true

[ $x -gr $y ] 返回true

-le 检查左边的数是否小于等于右边的,如果小于等于,则返回true

[ $x -le $y ] 返回true

  布尔运算符:  !  -0  -a

    举例:

 1 [root@VM_0_3_centos ~]# #!/bin/sh
 2 [root@VM_0_3_centos ~]# x=5
 3 [root@VM_0_3_centos ~]# y=10
 4 [root@VM_0_3_centos ~]# if [ $x != $y ]
 5 > then
 6 > echo "$x != $y : x is not equal to y"
 7 > else
 8 > echo "$x != $y: x is equal to y"
 9 > fi
10 5 != 10 : x is not equal to y
11 [root@VM_0_3_centos ~]# if [ $x -lt 100 -a $y -gt 15 ]
12 > then
13 > echo "$x -lt 100 -a $y -gt 15 : returns true"
14 > else
15 > echo "$x -lt 100 -a $y -gt 15 : returns false"
16 > fi
17 5 -lt 100 -a 10 -gt 15 : returns false
18 [root@VM_0_3_centos ~]# if [ $x -lt 100 -o $y -gt 100 ]
19 > then
20 > echo "$x -lt 100 -o $y -gt 100 : returns true"
21 > else
22 > echo "$x -lt 100 -o $y -gt 100 : returns false"
23 > fi
24 5 -lt 100 -o 10 -gt 100 : returns true
25 [root@VM_0_3_centos ~]# if [ $x -lt 5 -o $y -gt 100 ]
26 > then
27 > echo "$x -lt 100 -o $y -gt 100 : returns true"
28 > else
29 > echo "$x -lt 100 -o $y -gt 100 : returns false"
30 > fi
31 5 -lt 100 -o 10 -gt 100 : returns false
32 [root@VM_0_3_centos ~]# 
View Code
布尔运算符
运算符 说明 举例
! 非运算,表达式为true则返回false,否则返回true [ !false ]返回true
-o 或运算,有一个表达式为true,就返回true [ $x -lt 100 -o $y -gt 100 ] 返回true
-a 与运算,两个表达式都为true,才返回true [ $x -lt 5 -o $y -gt 100 ] 返回false

  字符串运算符:  =  !=  -z  -n  str

    举例:

 1 [root@VM_0_3_centos ~]# #!/bin/sh
 2 [root@VM_0_3_centos ~]# x="qwer"
 3 [root@VM_0_3_centos ~]# y="asdf"
 4 [root@VM_0_3_centos ~]# if [ $x = $y ]
 5 > then
 6 > echo "$x = $y : x is equal to y "
 7 > else
 8 > echo "$x = $y: x is not equal to y "
 9 > fi
10 qwer = asdf: x is not equal to y 
11 [root@VM_0_3_centos ~]# if [ $x != $y ]
12 > then
13 > echo "$x != $y : x is not equal to y "
14 > else
15 > echo "$x != $y: x is equal to y "
16 > fi
17 qwer != asdf : x is not equal to y 
18 [root@VM_0_3_centos ~]# if [ -z $x ]
19 > then
20 > echo "-z $x : string length is zero"
21 > else
22 > echo "-z $x : string length is not zero"
23 > fi
24 -z qwer : string length is not zero
25 [root@VM_0_3_centos ~]# if [ -n $x ]
26 > then
27 > echo "-n $x : string length is not zero"
28 > else
29 > echo "-n $x : string length is zero"
30 > fi
31 -n qwer : string length is not zero
32 [root@VM_0_3_centos ~]# if [ $x ]
33 > then
34 > echo "$x : string is not empty"
35 > else
36 > echo "$x : string is empty"
37 > fi
38 qwer : string is not empty
39 [root@VM_0_3_centos ~]# 
View Code
字符串运算符
运算符 说明 举例
= 检测两个字符串是否相等,相等返回true [ $x = $y ] 返回false
!= 检测两个字符串是否相等,不相等返回true [ $x != $y ]返回true
-z 检测字符串长度是否为0,为0返回true [ -z $x ]返回false
-n 检测字符串长度是否为0,不为0返回true [ -z $x ]返回ture
str 检测字符串是否为空,不为空返回true [ $x ] 返回true

  文件测试运算符:  -b  -c  -d  -f  -g  -k  -p  -u  -r  -w  -x  -s  -e  

    举例

 1 [root@VM_0_3_centos ~]# #!/bin/sh
 2 [root@VM_0_3_centos ~]# file="/data/bash.shcho"
 3 [root@VM_0_3_centos ~]# if [ -r $file ]
 4 > then
 5 > echo "File has read access"
 6 > else
 7 > echo "File does not have read access"
 8 > fi
 9 File does not have read access
10 [root@VM_0_3_centos ~]# if [ -w $file ]
11 > then
12 > echo "File has write permission"
13 > else
14 > echo "File does not have write permission"
15 > fi
16 File does not have write permission
17 [root@VM_0_3_centos ~]# if [ -x $file ]
18 > then
19 > echo "File has execute permission"
20 > else
21 > echo "File does not have execute permission"
22 > fi
23 File does not have execute permission
24 [root@VM_0_3_centos ~]# if [ -f $file ]
25 > then
26 > echo "File is an ordinary file"
27 > else
28 > echo "This is sepcial file"
29 > fi
30 This is sepcial file
31 [root@VM_0_3_centos ~]# if [ -d $file ]
32 > then
33 > echo "File is a directory"
34 > else
35 > echo "This is not a directory"
36 > fi
37 This is not a directory
38 [root@VM_0_3_centos ~]# if [ -s $file ]
39 > then
40 > echo "File size is zero"
41 > else
42 > echo "File size is not zero"
43 > fi
44 File size is not zero
45 [root@VM_0_3_centos ~]# if [ -e $file ]
46 > then
47 > echo "File exists"
48 > else
49 > echo "File does not exist"
50 > fi
51 File does not exist
52 [root@VM_0_3_centos ~]# if [ -b $file ]
53 > then 
54 > echo "File is Block device file"
55 > else
56 > echo "File is not Block device file"
57 > fi
58 File is not Block device file
59 [root@VM_0_3_centos ~]# if [ -c $file ]
60 > then
61 > echo "File Character device file"
62 > else
63 > echo "File not Character device file"
64 > fi
65 File not Character device file
66 [root@VM_0_3_centos ~]# if [ -g $file ]
67 > then
68 > echo "File set SGID"
69 > else
70 > echo "File not set SGID"
71 > fi
72 File not set SGID
73 [root@VM_0_3_centos ~]# if [ -k $file ]
74 > then
75 > echo "File set sticky bit"
76 > else
77 > echo "File not set sticky bit"
78 > fi
79 File not set sticky bit
80 [root@VM_0_3_centos ~]# if [ -p $file ]
81 > then
82 > echo "File is A named pipe"
83 > else
84 > echo "File not is named pipe"
85 > fi
86 File not is named pipe
87 [root@VM_0_3_centos ~]# if [ -u $file ]
88 > then
89 > echo "File set SUID"
90 > else
91 > echo "File not set SUID"
92 > fi
93 File not set SUID
94 [root@VM_0_3_centos ~]# 
View Code
文件测试运算符
操作符 说明 举例
-b file 检测文件是否是块设备文件,如果是,则返回true [ -b $file ] 返回false
-c file 检测文件是否是字符设备文件,如果是,则返回true [ -c $file ] 返回false 
-d file 检测文件是否是目录,如果是,则返回true [ -d $file ] 返回false
-f flie 检测文件是否是普通文件,如果是,则返回true [ -f $file ] 返回true 
-g file 检测文件是否设置了SGID位,如果是,则返回true [ -g $file ] 返回false
-k file 检测文件是否设置了Sticky Bit,如果是,则返回true [ -k $file ] 返回false
-p file 检测文件是否是具名管道。如果是,则返回true [ -p $file ] 返回false
-u file 检测文件是否设置了SUID位,如果是,则返回true [ -u $file ] 返回false
-r file 检测文件是否可读,如果是,则返回true [ -r $file ] 返回false
-w file 检测文件是否可写,如果是,则返回true [-w $file ] 返回false
-x file 检测文件是否可执行,如果是,则返回true [ -x $file ] 返回false
-s file 检测文件是否位空,不为空返回true [ -s $file ] 返回true 
-e file 检测文件是否存在,如果是,则返回true [ -e $file ] 返回false


3、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到 /root/etcYYYY-mm-dd中

 1 [root@VM_0_3_centos data]# cd /root/
 2 [root@VM_0_3_centos ~]# ll
 3 total 27856
 4 drwxr-xr-x. 85 root   root       4096 Apr  7 13:46 etc2019-04-07
 5 [root@VM_0_3_centos ~]# cat /data/backup.sh 
 6 #!/bin/bash
 7 
 8 today=`date +%F`
 9 echo "starting backup"
10 cp -av /etc/ /root/etc$today
11 echo "backup iss finished"
12 unset today
13 [root@VM_0_3_centos ~]# 
View Code

4、编写脚本/root/bin/nologin.sh和login.sh,实现禁止和充许普通用户登录系统

 1 [root@VM_0_3_centos bin]# bash /root/bin/nologin.sh 
 2 already can not access
 3 [root@VM_0_3_centos bin]# bash /root/bin/login.sh   
 4 already can access
 5 [root@VM_0_3_centos bin]# cat login.sh 
 6 #!/bin/bash
 7 [ -f /date/nologin ] && (rm -f /data/nologin;echo " delete /data/nologin success") || echo "already can access"
 8 [root@VM_0_3_centos bin]# cat nologin.sh 
 9 #!/bin/bash
10 [ -f /data/nologin ] && echo "already can not access"||(touch /data/nologin $$echo "create  /data/nologin success")
11 [root@VM_0_3_centos bin]# 
View Code

5、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值

 1 [root@VM_0_3_centos bin]# vim disk.sh 
 2 [root@VM_0_3_centos bin]# cat disk.sh 
 3 #!/bin/bash
 4 disk_usage=`df|grep "/dev/vd"|egrep -o "<[[:digit:]]+%" |tr -d %|sort -n |tail -n1`
 5 echo "The max disk used is $disk_usage"
 6 unset disk_usage
 7 [root@VM_0_3_centos bin]# bash /root/bin/disk.sh 
 8 The max disk used is 11
 9 [root@VM_0_3_centos bin]# df
10 Filesystem     1K-blocks    Used Available Use% Mounted on
11 /dev/vda1       51605116 4982104  44001644  11% /
12 [root@VM_0_3_centos bin]# 
View Code