什么是shell?
Shell是系统的用户界面,提供了用户与内核进行交互操作的一种接口。它接收用户输入的命令并把它送入内核去执行。
实际上Shell是一个命令解释器,它解释由用户输入的命令并且把它们送到内核。不仅如此,Shell有自己的编程语言用于对命令的编辑,它允许用户编写由shell命令组成的程序。Shell编程语言具有普通编程语言的很多特点,比如它也有循环结构和分支控制结构等,用这种编程语言编写的Shell程序与其他应用程序具有同样的效果。
shell和shell脚本有什么区别?
确切一点说,Shell就是一个命令行解释器,它的作用就是遵循一定的语法将输入的命令加以解释并传给系统。它为用户提供了一个向Linux发送请求以便运行程序的接口系统级程序,用户可以用Shell来启动、挂起、停止甚至是编写一些程序。 Shell本身是一个用C语言编写的程序,它是用户使用Linux的桥梁。Shell既是一种命令语言,又是一种程序设计语言(就是你所说的shell脚本)。作为命令语言,它互动式地解释和执行用户输入的命令;作为程序设计语言,它定义了各种变量和参数,并提供了许多在高阶语言中才具有的控制结构,包括循环和分支。它虽然不是 Linux系统内核的一部分,但它调用了系统内核的大部分功能来执行程序、创建文档并以并行的方式协调各个程序的运行。
一、在新脚本里自动添加信息
[root@localhost ~]# cd /mnt
[root@localhost mnt]# vim /etc/vimrc
[root@localhost mnt]# vim westos.sh ##打开一个新的脚本,里面自动添加进去了信息
二、用脚本截取IP
[root@localhost mnt]# vim ip_show.sh ##编写脚本
[root@localhost mnt]# chmod +x ip_show.sh ##给脚本一个可执行权限
[root@localhost mnt]# /mnt/ip_show.sh ##绝对路径调用脚本
[root@localhost mnt]# sh ip_show.sh ##直接调用脚本
三、diff命令
是用来比较两个文件或目录不同
a –add表示添加
c –change表示更改
d –delete表示删除
<表示第一个文件中的内容
—-分割线
>表示第二个文件中的内容
[root@localhost mnt]# vim westos
[root@localhost mnt]# vim westos1
[root@localhost mnt]# cat westos
hello 2018
[root@localhost mnt]# cat westos1
hello 2018
westos and linux
[root@localhost mnt]# diff westos westos1
[root@localhost mnt]# vim westos
hello 2018
linux
[root@localhost mnt]# diff westos westos1
[root@localhost mnt]# diff -u westos westos1 ##以合并的方式显示文件内容的不同
[root@localhost mnt]# diff -u westos westos1 > westos.path ##将比较结果保存到westos.path文件
[root@localhost mnt]# cat westos.path
patch 打补丁
[root@localhost mnt]# yum install patch -y ##安装补丁
[root@localhost mnt]# cat westos
[root@localhost mnt]# cat westos1
[root@localhost mnt]# patch westos westos.path ##给westos打补丁
[root@localhost mnt]# cat westos
[root@localhost mnt]# vim westos ##需要还原文件才能看到效果
[root@localhost mnt]# cat westos
[root@localhost mnt]# patch -b westos westos.path
[root@localhost mnt]# ls
注意:
1.patch命令失败或拒绝接受补丁时,会产生一个和原文件同名,以“.rej“为后缀的差异文件。
2.如果有多个补丁要打,则应该注意打补丁的顺序。
四、cut命令
多用与字符截取。
cut -d 指定分隔符
cut -f 1,7|1-7 指定截取的列
cut -c 1,4|1-4 指定截取的字符位置
[root@localhost mnt]# cp /etc/passwd passwd ##复制/etc/passwd
[root@localhost mnt]# vim passwd ##删除一部分文件的内容,有利于查看
[root@localhost mnt]# cut -d : -f 1 passwd ##以:为分隔符截取第一列
[root@localhost mnt]# cut -d : -f 1,7 passwd ##以:为分隔符截取第1列和第7列
[root@localhost mnt]# cut -c 1,3 passwd ##截取第1和第3个字符位置
五、&&和||运算符
&&(命令执行控制)
1.命令之间使用 && 连接,实现逻辑与的功能。
2.只有在&&左边的命令返回真,&& 右边的命令才会被执行。
3.只要有一个命令返回假(命令返回值$?== 1),后面的命令就不会被执行。
||(命令执行控制)
1.命令之间使用 || 连接,实现逻辑或的功能。
2.只有在 || 左边的命令返回假,|| 右边的命令才会被执行。这和 c 语言中的逻辑或语法功能相同,即实现短路逻辑或操作。
3.只要有一个命令返回真,后面的命令就不会被执行。
写脚本判断IP是否存在:
[root@localhost mnt]# vim ip_check.sh
[root@localhost mnt]# sh ip_check.sh 172.25.254.28
[root@localhost mnt]# sh ip_check.sh 172.25.254.68
六、sort命令
多用于字符排序
sort -n 纯数字排序
sort -r 倒序
sort -u 去掉重复的数字
sort -o 输出到指定文件中
sort -t 指定分隔符
sort -k 指定要排序的列
[root@localhost mnt]# vim linux
[root@localhost mnt]# cat linux
[root@localhost mnt]# sort -n linux ##正序排列
[root@localhost mnt]# sort -n linux -o file ##正序排列后输出到file
[root@localhost mnt]# cat file
[root@localhost mnt]# vim linux
[root@localhost mnt]# cat linux
[root@localhost mnt]# sort -t : -k 2 -n linux ##以:为分隔符排第二列正序排列
七、uniq命令
对重复字符做相应的处理
uniq -u 显示唯一(不重复)的行
uniq -d 显示重复的
uniq -c 每行显示一词并统计重复次数
[root@localhost mnt]# vim file0
[root@localhost mnt]# cat file0
[root@localhost mnt]# sort -n file0 | uniq -c ##将file0文件正序排列,显示有数字重复的次数
[root@localhost mnt]# sort -n file0 | uniq -d ##将file0文件正序排列,显示有重复的数字
[root@localhost mnt]# sort -n file0 | uniq -u##将file0文件正序排列,显示唯一(不重复)的数字
对/mnt里所有的文件大小排序
[root@localhost mnt]# ls -l /mnt
[root@localhost mnt]# ls -l /mnt |grep total -v |awk -F " " '//{print $5,$9}' | sort -nr
八、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”] 文件是否不为空
[“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 -li * ##查看节点
[root@localhost mnt]# [ "/mnt/file" -ef "/mnt/file1" ]&& echo yes ||echo no ##两个节点是否相同
[root@localhost mnt]# rm -fr file1
[root@localhost mnt]# touch file1
[root@localhost mnt]# [ "/mnt/file" -ot "/mnt/file1" ]&& echo yes ||echo no ##比较两个文件哪个文件旧
[root@localhost mnt]# [ "/mnt/file" -nt "/mnt/file1" ]&& echo yes ||echo no ##比较两个文件哪个文件新
实验二:
[root@localhost mnt]# vim file.sh
[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
file file.sh
[root@localhost mnt]# rm -fr file
[root@localhost mnt]# ln -s /mnt/file.sh /mnt/file
[root@localhost mnt]# ll
total 4
lrwxrwxrwx 1 root root 12 6月 11 06:12 file -> /mnt/file.sh
-rw-r--r-- 1 root root 331 6月 11 06:07 file.sh
[root@localhost mnt]# sh file.sh -L ##文件是否为符号链接
[root@localhost mnt]# sh file.sh -S ##文件是否为套接字
[root@localhost mnt]# yum install mariadb-server -y
[root@localhost mnt]# systemctl start mariadb
[root@localhost mnt]# ls /var/lib/mysql/
[root@localhost mnt]# ll /var/lib/mysql/
[root@localhost mnt]# rsync -D /var/lib/mysql/mysql.sock /mnt/file ##远程传输
[root@localhost mnt]# ll
[root@localhost mnt]# sh file.sh -S
[root@localhost mnt]# sh file.sh -s ##是否有容量
[root@localhost mnt]# sh file.sh -b ##是否是块设备
[root@localhost mnt]# rm -fr file
[root@localhost mnt]# rsync -D /dev/vdb /mnt/file ##远程传输一个块设备
[root@localhost mnt]# sh file.sh -b ##是否为块设备
[root@localhost mnt]# rm -fr /mnt/file
[root@localhost mnt]# mkdir /mnt/file
[root@localhost mnt]# sh file.sh -d ##是否为目录
[root@localhost mnt]# rm -fr /mnt/file
[root@localhost mnt]# rsync -D /dev/pts/1 /mnt/file ##远程传输字符设备
[root@localhost mnt]# sh file.sh -c ##是否为字符设备
实验三:
用脚本判断是否为10以内的数字
[root@localhost mnt]# vim num_check.sh
[root@localhost mnt]# sh num_check.sh 20
[root@localhost mnt]# sh num_check.sh 5
实验四:
判断文件是否存在,如果存在属于什么类型
[root@localhost mnt]# vim file_check.sh
九、tr命令
[root@localhost mnt]# vim text.sh
[root@localhost mnt]# sh text.sh hello
[root@localhost mnt]# sh text.sh HELLO
解决方法:
[root@localhost mnt]# vim text.sh
[root@localhost mnt]# sh text.sh HELLO
用脚本检测用户是否存在
[root@localhost mnt]# vim user_create.sh