Linux入门-第三周
1、总结vim命令行模式常见快捷方式,以及vim查找,替换的方法
vim [options] [file ..]
+# 打开文件后,让光标处于第#行的行首,(默认行尾)
举例vim +10 /etc/passwd (光标调至第十行)如下图
+/PATTERN打开文件后,直接让光标处于第一个被PATTERN匹配到的行的行首
举例vim +/ga /etc/passwd (让光标置于ga开头的行)如下图
-d file1 file2 ... 比较多个文件
-m file 只读方式打开文件(即使修改后:wq!也无法修改文件)
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的键盘布局图以供参考
命令模式:查找
/PATTERN:从当前光标所在处向文件尾部查找
?PATTERN:从当前光标所在处向文件首部查找
n:与命令同方向
N:与命令反方向
拓展命令模式:查找并替换
s: 在扩展模式下完成查找替换操作
格式:s/要查找的内容/替换为的内容/修饰符
要查找的内容:可使用模式
替换为的内容:不能使用模式,但可以使用\1, \2, ...等后向引用符号;还可以使用“&”引用前面查找时查找到的整个内容
修饰符:
i: 忽略大小写
g: 全局替换;默认情况下,每一行只替换第一次出现
gc:全局替换,每次替换前询问
举例:全局查找var替换为user
2、总结脚本中运算符、逻辑运算以及用法
Bash 支持很多运算符,包括算数运算符、关系运算符、布尔运算符、字符串运算符和文件测试运算符。
原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如awk和expr,expr最常用
awk:Aho, Weinberger, Kernighan,报告生成器,格式化文本输出;awk 是一种很棒的语言,它适合文本处理和报表生成,其语法较为常见,借鉴了某些语言的一些精华,如 C 语言等。在 linux 系统日常处理工作中,发挥很重要的作用,掌握了 awk将会使你的工作变的高大上。 awk 是三剑客的老大,利剑出鞘,必会不同凡响。
expr命令是一个手工命令行计数器,用于在UNIX/LINUX下求表达式变量的值,一般用于整数值,也可用于字符串。
例如两个数相加:
[root@localhost data]# val=`expr + `
[root@localhost data]# echo "Total value : $val"
Total value :
[root@localhost data]#
算术运算符:
举例:
[root@VM_0_3_centos ~]# x=
[root@VM_0_3_centos ~]# y=
[root@VM_0_3_centos ~]# s=`expr $x + $y`
[root@VM_0_3_centos ~]# echo "x + y = $s"
x + y =
[root@VM_0_3_centos ~]#
[root@VM_0_3_centos ~]# s=`expr $y - $x`
[root@VM_0_3_centos ~]# echo "y - x = $s"
y - x =
[root@VM_0_3_centos ~]#
[root@VM_0_3_centos ~]# s=`expr $y \* $x`
[root@VM_0_3_centos ~]# echo "y * x = $s"
y * x =
[root@VM_0_3_centos ~]#
[root@VM_0_3_centos ~]# s=`expr $y / $x`
[root@VM_0_3_centos ~]# echo "y / x = $s"
y / x =
[root@VM_0_3_centos ~]#
[root@VM_0_3_centos ~]# s=`expr $y % $x`
[root@VM_0_3_centos ~]# echo "y % x = $s"
y % x =
[root@VM_0_3_centos ~]#
[root@VM_0_3_centos ~]# if [ $x == $y ]
> then
> echo "x is equal to y"
> fi
[root@VM_0_3_centos ~]# if [ $x != $y ]
> then
> echo "x is not equal to y"
> fi
x is not equal to y
[root@VM_0_3_centos ~]#
运算符 | 说明 | 举例 |
+ | 加法 | `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
举例
[root@VM_0_3_centos ~]# #!/bin/sh
[root@VM_0_3_centos ~]# x=
[root@VM_0_3_centos ~]# y=
[root@VM_0_3_centos ~]# if [ $x -eq $y ]
> then
> echo "$x -eq $y : x is equal to y"
> else
> echo "$x -eq $y: x is not equal to y"
> fi
-eq : x is not equal to y
[root@VM_0_3_centos ~]# if [ $x -ne $y ]
> then
> echo "$x -ne $y: x is not equal to y"
> else
> echo "$x -ne $y : x is equal to y"
> fi
-ne : x is not equal to y
[root@VM_0_3_centos ~]# if [ $x -gt $y ]
> then
> echo "$x -gt $y: x is greater than y"
> else
> echo "$x -gt $y: x is not greater than y"
> fi
-gt : x is not greater than y
[root@VM_0_3_centos ~]# if [ $x -lt $y ]
> then
> echo "$x -lt $y: x is less than y"
> else
> echo "$x -lt $y: x is not less than y"
> fi
-lt : x is less than y
[root@VM_0_3_centos ~]# if [ $x -ge $y ]
> then
> echo "$x -ge $y: x is greater or equal to y"
> else
> echo "$x -ge $y: x is not greater or equal to y"
> fi
-ge : x is not greater or equal to y
[root@VM_0_3_centos ~]# if [ $x -le $y ]
> then
> echo "$x -le $y: x is less or equal to y"
> else
> echo "$x -le $y: x is not less or equal to y"
> fi
-le : x is less or equal to y
[root@VM_0_3_centos ~]#
运算符 | 说明 | 举例 |
-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
举例:
[root@VM_0_3_centos ~]# #!/bin/sh
[root@VM_0_3_centos ~]# x=
[root@VM_0_3_centos ~]# y=
[root@VM_0_3_centos ~]# if [ $x != $y ]
> then
> echo "$x != $y : x is not equal to y"
> else
> echo "$x != $y: x is equal to y"
> fi
!= : x is not equal to y
[root@VM_0_3_centos ~]# if [ $x -lt -a $y -gt ]
> then
> echo "$x -lt 100 -a $y -gt 15 : returns true"
> else
> echo "$x -lt 100 -a $y -gt 15 : returns false"
> fi
-lt -a -gt : returns false
[root@VM_0_3_centos ~]# if [ $x -lt -o $y -gt ]
> then
> echo "$x -lt 100 -o $y -gt 100 : returns true"
> else
> echo "$x -lt 100 -o $y -gt 100 : returns false"
> fi
-lt -o -gt : returns true
[root@VM_0_3_centos ~]# if [ $x -lt -o $y -gt ]
> then
> echo "$x -lt 100 -o $y -gt 100 : returns true"
> else
> echo "$x -lt 100 -o $y -gt 100 : returns false"
> fi
-lt -o -gt : returns false
[root@VM_0_3_centos ~]#
运算符 | 说明 | 举例 |
! | 非运算,表达式为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
举例:
[root@VM_0_3_centos ~]# #!/bin/sh
[root@VM_0_3_centos ~]# x="qwer"
[root@VM_0_3_centos ~]# y="asdf"
[root@VM_0_3_centos ~]# if [ $x = $y ]
> then
> echo "$x = $y : x is equal to y "
> else
> echo "$x = $y: x is not equal to y "
> fi
qwer = asdf: x is not equal to y
[root@VM_0_3_centos ~]# if [ $x != $y ]
> then
> echo "$x != $y : x is not equal to y "
> else
> echo "$x != $y: x is equal to y "
> fi
qwer != asdf : x is not equal to y
[root@VM_0_3_centos ~]# if [ -z $x ]
> then
> echo "-z $x : string length is zero"
> else
> echo "-z $x : string length is not zero"
> fi
-z qwer : string length is not zero
[root@VM_0_3_centos ~]# if [ -n $x ]
> then
> echo "-n $x : string length is not zero"
> else
> echo "-n $x : string length is zero"
> fi
-n qwer : string length is not zero
[root@VM_0_3_centos ~]# if [ $x ]
> then
> echo "$x : string is not empty"
> else
> echo "$x : string is empty"
> fi
qwer : string is not empty
[root@VM_0_3_centos ~]#
运算符 | 说明 | 举例 |
= | 检测两个字符串是否相等,相等返回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
举例
[root@VM_0_3_centos ~]# #!/bin/sh
[root@VM_0_3_centos ~]# file="/data/bash.shcho"
[root@VM_0_3_centos ~]# if [ -r $file ]
> then
> echo "File has read access"
> else
> echo "File does not have read access"
> fi
File does not have read access
[root@VM_0_3_centos ~]# if [ -w $file ]
> then
> echo "File has write permission"
> else
> echo "File does not have write permission"
> fi
File does not have write permission
[root@VM_0_3_centos ~]# if [ -x $file ]
> then
> echo "File has execute permission"
> else
> echo "File does not have execute permission"
> fi
File does not have execute permission
[root@VM_0_3_centos ~]# if [ -f $file ]
> then
> echo "File is an ordinary file"
> else
> echo "This is sepcial file"
> fi
This is sepcial file
[root@VM_0_3_centos ~]# if [ -d $file ]
> then
> echo "File is a directory"
> else
> echo "This is not a directory"
> fi
This is not a directory
[root@VM_0_3_centos ~]# if [ -s $file ]
> then
> echo "File size is zero"
> else
> echo "File size is not zero"
> fi
File size is not zero
[root@VM_0_3_centos ~]# if [ -e $file ]
> then
> echo "File exists"
> else
> echo "File does not exist"
> fi
File does not exist
[root@VM_0_3_centos ~]# if [ -b $file ]
> then
> echo "File is Block device file"
> else
> echo "File is not Block device file"
> fi
File is not Block device file
[root@VM_0_3_centos ~]# if [ -c $file ]
> then
> echo "File Character device file"
> else
> echo "File not Character device file"
> fi
File not Character device file
[root@VM_0_3_centos ~]# if [ -g $file ]
> then
> echo "File set SGID"
> else
> echo "File not set SGID"
> fi
File not set SGID
[root@VM_0_3_centos ~]# if [ -k $file ]
> then
> echo "File set sticky bit"
> else
> echo "File not set sticky bit"
> fi
File not set sticky bit
[root@VM_0_3_centos ~]# if [ -p $file ]
> then
> echo "File is A named pipe"
> else
> echo "File not is named pipe"
> fi
File not is named pipe
[root@VM_0_3_centos ~]# if [ -u $file ]
> then
> echo "File set SUID"
> else
> echo "File not set SUID"
> fi
File not set SUID
[root@VM_0_3_centos ~]#
操作符 | 说明 | 举例 |
-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中
[root@VM_0_3_centos data]# cd /root/
[root@VM_0_3_centos ~]# ll
total
drwxr-xr-x. root root Apr : etc2019--
[root@VM_0_3_centos ~]# cat /data/backup.sh
#!/bin/bash today=`date +%F`
echo "starting backup"
cp -av /etc/ /root/etc$today
echo "backup iss finished"
unset today
[root@VM_0_3_centos ~]#
4、编写脚本/root/bin/nologin.sh和login.sh,实现禁止和充许普通用户登录系统
[root@VM_0_3_centos bin]# bash /root/bin/nologin.sh
already can not access
[root@VM_0_3_centos bin]# bash /root/bin/login.sh
already can access
[root@VM_0_3_centos bin]# cat login.sh
#!/bin/bash
[ -f /date/nologin ] && (rm -f /data/nologin;echo " delete /data/nologin success") || echo "already can access"
[root@VM_0_3_centos bin]# cat nologin.sh
#!/bin/bash
[ -f /data/nologin ] && echo "already can not access"||(touch /data/nologin $$echo "create /data/nologin success")
[root@VM_0_3_centos bin]#
5、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值
[root@VM_0_3_centos bin]# vim disk.sh
[root@VM_0_3_centos bin]# cat disk.sh
#!/bin/bash
disk_usage=`df|grep "/dev/vd"|egrep -o "\<[[:digit:]]+%" |tr -d %|sort -n |tail -n1`
echo "The max disk used is $disk_usage"
unset disk_usage
[root@VM_0_3_centos bin]# bash /root/bin/disk.sh
The max disk used is
[root@VM_0_3_centos bin]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 % /
[root@VM_0_3_centos bin]#
最新文章
- 4 HTML&;JS等前端知识系列之Dom的基础
- C# fun
- cmd导入导出
- selenium-pageobject设计模式
- 封装WebAPI客户端,附赠Nuget打包上传VS拓展工具
- 服务器&#215;&#215;&#215;上的MSDTC不可用解决办法
- 【总结】编写自己的JDBC框架
- js里面的等于号--
- HDU-4665 Unshuffle 搜索 | 2-SAT
- 14.5.5.3 How to Minimize and Handle Deadlocks 如何减少和处理死锁
- kafka第四篇--快速入门(如何使用kafka)
- 用keras做SQL注入攻击的判断
- [BZOJ1046] [HAOI2007] 上升序列 (dp)
- 拥抱开源,Office 365开发迎来新时代
- php中pcntl_fork详解
- C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
- MT【279】分母为根式的两个函数
- loading加载的代码
- 浅谈XListView的使用
- http抓包以及网速限定