shell (计算机壳层)
Linux系统的shell作为操作系统的外壳,为用户提供使用操作系统的接口。它是命令语言、命令解释程序及程序设计语言的统称。
shell是用户和Linux内核之间的接口程序,如果把Linux内核想象成一个球体的中心,shell就是围绕内核的外层。当从shell或其他程序向Linux传递命令时,内核会做出相应的反应。
shell是一个命令语言解释器,它拥有自己内建的shell命令集,shell也能被系统中其他应用程序所调用。用户在提示符下输入的命令都由shell先解释然后传给Linux核心。
脚本示例
截取主机ip:
[root@localhost ~]# cd /mnt/
[root@localhost mnt]# vim ip_show.sh ##编辑脚本文件
[root@localhost mnt]# cat ip_show.sh
#!/bin/bash ##幻数
ifconfig eth0 | awk -F " " '/inet\>/{print $2}' ##显示主机ip
[root@localhost mnt]# chmod +x ip_show.sh ##赋予权限
[root@localhost mnt]# /mnt/ip_show.sh ##绝对路径调用脚本
172.25.254.119
[root@localhost mnt]# sh ip_show.sh ##直接调用脚本
172.25.254.119
编辑脚本文件内容:
一·在新脚本中自动添加信息
[root@localhost mnt]# vim /etc/vimrc
65 map <F4> ms:call WESTOS()<cr>'s ##使用F4快捷键调用命令
66 func WESTOS ()
67 call append(0,"############################")
68 call append(1,"# Author: linux".(" #"))
69 call append(2,"# Version: ".(" #"))
70 call append(3,"# Mail: ".(" #"))
71 call append(4,"# Date: ".strftime("%Y-%m-%d").(" #"))
72 call append(5,"# Description: ".(" #"))
73 call append(6,"# ".(" #"))
74 call append(7,"############################")
75 call append(8," ")
76 call append(9,"#!/bin/bash")
77 endfunc
[root@localhost mnt]# vim test.sh ##使用快捷键F4调用命令
[root@localhost mnt]# vim /etc/vimrc
注释65行
66 autocmd BufNewFile *.sh exec ":call WESTOS()" ##打开新文件直接添加(只针对与新文件,调用过的文件,不会第二次再调用)
[root@localhost mnt]# vim test.sh ##打开文件直接添加
编辑配置文件内容:
打开脚本文件
二·Shell中常用命令
1、diff命令是用来比较两个文件或目录不同
a –add 表示添加
c –change 表示更改
d –delete 表示删除
<表示第一个文件中的内容
—-分割线
表示第二个文件中的内容
[root@localhost mnt]# vim westos
[root@localhost mnt]# cat westos
hello 123
linux
[root@localhost mnt]# vim westos1
[root@localhost mnt]# cat westos1
hello 123
[root@localhost mnt]# diff westos westos1 ##比较文件过程中结果读取
2d1
< linux
d:删除(删除第一个文件中的第二行)
[root@localhost mnt]# vim westos1
[root@localhost mnt]# cat westos1
hello 123
westos
[root@localhost mnt]# diff westos westos1
2c2
< linux
---
> westos
c:改变(将第一个文件中第二行改变成第二个文件中第二行)
[root@localhost mnt]# vim westos
[root@localhost mnt]# cat westos
hello 123
[root@localhost mnt]# diff westos westos1
1a2
> westos
a:添加(在第一个文件中添加第二个文件第二行)
2、补丁patch
[root@localhost mnt]# ls
ip_show.sh westos westos1
[root@localhost mnt]# diff -u westos westos1 ##-u:创建补丁文件
[root@localhost mnt]# diff -u westos westos1 > westos.path ##将两个文件都导入新的文件中
[root@localhost mnt]# cat westos.path
[root@localhost mnt]# yum install patch -y
[root@localhost mnt]# ls
ip_show.sh westos westos1 westos.path
[root@localhost mnt]# patch -b westos westos.path ##-b:保留原文件
[root@localhost mnt]# ls
ip_show.sh westos westos1 westos.orig (生成原文件) westos.path
[root@localhost mnt]# cat westos.orig
hello 123
[root@localhost mnt]# cat westos
hello 123
westos
[root@localhost mnt]# cat westos1
hello 123
westos
在做补丁实验时,两个文件中的内容必须是原文件,否则会生成另一个文件
3、&&和||
&&:条件为真
||:条件为假
[root@localhost mnt]# vim ip_check.sh
[root@localhost mnt]# sh ip_check.sh 172.25.254.71
172.25.254.71 is up
[root@localhost mnt]# sh ip_check.sh 172.25.254.222
172.25.254.222 is down
编辑脚本文件:
4、sort:多用于字符排序
sort -n 纯数字排序
sort -r 倒序
sort -u 去掉重复的数字
sort -o 输出到指定文件中
sort -t 指定分隔符
sort -k 指定要排序的列
[root@localhost mnt]# vim westos
[root@localhost mnt]# cat westos
[root@localhost mnt]# sort -n westos ##纯数字排序
[root@localhost mnt]# sort westos ##排序
[root@localhost mnt]# sort -u westos ##去掉重复数字
[root@localhost mnt]# sort -rn westos ##纯数字倒序
[root@localhost mnt]# sort -n westos -o file ##输出到指定文件中
[root@localhost mnt]# cat file
[root@localhost mnt]# vim westos
[root@localhost mnt]# cat westos
[root@localhost mnt]# sort -t : -k 2 -n westos ##以:为分隔符,第二列排序
[root@localhost mnt]# sort -t : -k 1 -n westos
5、uniq:对重复字符做相应的处理
uniq -u 显示唯一(不重复)的行
uniq -d 显示重复的
uniq -c 每行显示一词并统计重复次数
[root@localhost mnt]# vim westos
[root@localhost mnt]# cat westos
[root@localhost mnt]# sort -n westos | uniq -c ##每行显示一词并统计重复次数
[root@localhost mnt]# sort -n westos | uniq -d ##显示重复的
[root@localhost mnt]# sort -n westos | uniq -u ##显示唯一(不重复)的
6、test命令
test命令和[ ]等同
test “ A"==" A "==" B” 等同[“ A"==" A "==" B”]
[“ A"=" A "=" B”] 等于
[“ A"!=" A " ! =" B”] 不等于
[“ A"−eq" A " − e q " B”] 等于
[“ A"−ne" A " − n e " B”] 不等于
[“ A"−le" A " − l e " B”] 小于等于
[“ A"−lt" A " − l t " B”] 小于
[“ A"−ge" A " − g e " B”] 大于等于
[“ A"−gt" A " − g t " B”] 大于
[“ A"−ne" A " − n e " B”-a” A"−gt" A " − g t " B”] -a必须两个都满足
[“ A"−ne" A " − n e " B”-o” A"−gt" A " − g t " B”] -o满足一个条件
[-z” A”]文件是否为空[−n” A ” ] 文 件 是 否 为 空 [ − n ” A”] 文件是否不为空
[“file1”-nt”file2”] 判断file1是否比file2新
[“file1”-ot”file2”] 判断file1是否比file2旧
[“file1”-ef”file2”] 两个文件节点是否相同
[-e “file”] 文件是否存在
[-f “file”] 文件是否为普通文件
[-L “file”] 文件是否为符号链接
[-S “file”] 文件是否为套接字
[-b “file”] 文件是否为块设备
[-d “file”] 文件是否为目录
[-c “file”] 文件是否为特殊文件
实验一:
[root@localhost mnt]# touch file
[root@localhost mnt]# ln /mnt/file /mnt/file1
[root@localhost mnt]# ls -l
[root@localhost mnt]# ls -li * ##查看节点
[root@localhost mnt]# [ "/mnt/file" -ef "/mnt/file1" ]&& echo yes || echo no ##两个文件节点是否相同
[root@localhost mnt]# rm -fr file1 ##删除
[root@localhost mnt]# ll
[root@localhost mnt]# touch file1 ##重新建立文件
[root@localhost mnt]# ll
[root@localhost mnt]# [ "file" -ot "file1" ]&& echo yes || echo no ##比较两个文件哪个老
[root@localhost mnt]# [ "file" -nt "file1" ]&& echo yes || echo no ##比较两个文件哪个新
实验二:
[root@localhost mnt]# vim num_check.sh ##判断是否为10以内的数字
[root@localhost mnt]# sh num_check.sh 20 ##调用脚本
[root@localhost mnt]# sh num_check.sh 3
编辑脚本内容:
实验三:
(1)文件是否存在
[root@localhost mnt]# vim file.sh ##编写脚本文件
[root@localhost mnt]# sh file.sh -e ##文件是否存在
[root@localhost mnt]# touch file ##建立文件
[root@localhost mnt]# sh file.sh -e ##再次查看
[root@localhost mnt]# sh file.sh -f ##是否为普通文件
[root@localhost mnt]# sh file.sh -L ##文件是否为软链接
[root@localhost mnt]# ls
编辑脚本文件:
(2)文件是否为软链接
[root@localhost mnt]# rm -fr file
[root@localhost mnt]# ln -s /mnt/file.sh /mnt/file ##制作软链接
[root@localhost mnt]# ll
lrwxrwxrwx 1 root root 12 Jun 13 05:33 file -> /mnt/file.sh ##软链接
[root@localhost mnt]# sh file.sh -L ##文件是否为软链接
[root@localhost mnt]# sh file.sh -S ##文件是否为套接子
(3)文件是否为套接子
[root@localhost mnt]# yum install mariadb-server -y ##安装数据库
[root@localhost mnt]# systemctl start mariadb ##开启服务
[root@localhost mnt]# ls /var/lib/mysql/
mysql.sock ##套接子
[root@localhost mnt]# ll
lrwxrwxrwx 1 root root 12 Jun 13 05:33 file -> /mnt/file.sh ##软链接
[root@localhost mnt]# ll /var/lib/mysql/
srwxrwxrwx 1 mysql mysql 0 Jun 13 05:42 mysql.sock
[root@localhost mnt]# rsync -D /var/lib/mysql/mysql.sock /mnt/file ##远程传输套接子
[root@localhost mnt]# ll
srwxrwxrwx 1 root root 0 Jun 13 05:43 file ##套接子
[root@localhost mnt]# sh file.sh -S ##文件是否为套接子
[root@localhost mnt]# sh file.sh -s ##文件是否有容量
[root@localhost mnt]# sh file.sh -b ##文件是否块设备
(4)文件是否为块设备
[root@localhost mnt]# ll
srwxr-xr-x 1 root root 0 Jun 13 05:51 file ##套接子
[root@localhost mnt]# rm -fr file
[root@localhost mnt]# rsync -D /dev/vdb /mnt/file ##远程传输块设备
[root@localhost mnt]# ll
brw-r----- 1 root root 253, 16 Jun 13 05:52 file ##块设备
[root@localhost mnt]# sh file.sh -b ##文件是否为块设备
[root@localhost mnt]# sh file.sh -d ##文件是否为目录
(5)文件是否为目录
[root@localhost mnt]# ll
brw-r----- 1 root root 253, 16 Jun 13 05:52 file ##块设备
[root@localhost mnt]# rm -fr file
[root@localhost mnt]# mkdir /mnt/file ##建立目录
[root@localhost mnt]# ll
drwxr-xr-x 2 root root 6 Jun 13 05:57 file ##目录
[root@localhost mnt]# sh file.sh -d ##文件是否为目录
[root@localhost mnt]# sh file.sh -c ##文件是否为字符设备
7、tr命令
[root@localhost mnt]# vim test.sh
[root@localhost mnt]# tr 'a-z' 'A-Z' < test.sh ##小写转换为大写
[root@localhost mnt]# tr 'A-Z' 'a-z' < test.sh ##大写转换为小写
脚本编辑文件:
实验一:
[root@localhost mnt]# vim test.sh ##编辑脚本(不区分大小写)
[root@localhost mnt]# sh test.sh HELLO ##调用脚本
[root@localhost mnt]# sh test.sh hello
[root@localhost mnt]# sh test.sh hell
[root@localhost mnt]# sh test.sh WE
编辑脚本内容:
实验二:
[root@localhost mnt]# rm -fr *
[root@localhost mnt]# vim test.sh
$# ##编辑内容($#的用法是后面有几个参数返回就是几)
[root@localhost mnt]# sh test.sh 777
[root@localhost mnt]# sh test.sh 77
[root@localhost mnt]# sh test.sh 7 5
[root@localhost mnt]# sh test.sh 7 9 0 6 5 6 6
[root@localhost mnt]# sh test.sh hello
[root@localhost mnt]# sh test.sh hel lo
脚本编辑内容: