一、shell基本信息
1.概述
脚本语言(shell、python):解释性语言,用解释器解释 运行效率低 | c、java:描述性语言,运行效率高 #!/bin/bash 幻数 ,在脚本运行的时候先进行该程序
以.sh结尾会有高亮显示
执行: sh hello.sh 或者 chmod +x hello.sh 再以绝对路径调用 /mnt/hello.sh
2.shell脚本
- 脚本是一种解释型语言
- 用 shell 脚本保存执行动作
- 用脚本判定命令的执行条件
- 用脚本来实现动作的批量执行
二、新建脚本自动添加注释信息
[root@localhost mnt]# vim /etc/vimrc
map <F4> ms:call MESS()<cr>'s
func MESS()
call append(0,"#################################")
call append(1,"#Author: gao".(" #"))
call append(2,"#Version: ".(" #"))
call append(3,"#Mail: gw@163.com".(" #"))
call append(4,"#Date: ".strftime("%Y-%m-%d").(" #"))
call append(5,"#Description: ".(" #"))
call append(6,"# ".(" #"))
call append(7,"#################################")
call append(8," ")
call append(9,"#!/bin/bash")
endfunc
[root@localhost mnt]# vim file.sh ##新建一个脚本会自动添加注释信息
配置文件所加内容(在建立一个脚本的情况下按F4会添加信息)
脚本的信息
注释掉按F4添加信息,改为自动给新建的脚本添加注释
三、编写脚本,执行该脚本显示ip
[root@localhost mnt]# vim ip_show.sh
ifconfig eth1 |awk -F " " '/inet\>/{print $2}'
[root@localhost mnt]# chmod +x ip_show.sh ##给一个可执行权限
[root@localhost mnt]# /mnt/ip_show.sh ##用绝对路径的方式调用
[root@localhost mnt]# sh ip_show.sh ##直接调用脚本
内容如下:
执行该脚本的情况
四、常用的shell命令
1.diff命令
diff 命令是用来比较两个文件或目录的不同
c –change d –delete a –add
<表示第一个文件中的内容
—-分割线
>表示第二个文件中的内容
[root@localhost mnt]# vim westos
[root@localhost mnt]# vim westos1
[root@localhost mnt]# cat westos
hello 123
linux
[root@localhost mnt]# cat westos1
hello 123
[root@localhost mnt]# diff westos westos1 ##第一个和第二个进行比较以第二个文件为准则
2d1 ##删除第一个文件的第二行才能和第二个文件的第一行匹配
< linux
对文件进行补丁
[root@localhost mnt]# diff -u westos westos1 ##生成补丁
--- westos 2018-06-09 22:44:21.237865024 -0400
+++ westos1 2018-06-09 22:45:25.921865024 -0400
@@ -1,2 +1 @@
hello 123
-linux
[root@localhost mnt]# diff -u westos westos1 > westos.path
f -u westos westos1 > westos.path
[root@localhost mnt]# yum install patch -y ##下载补丁工具
[root@localhost mnt]# patch westos westos.path
patching file westos
[root@localhost mnt]# cat westos
hello 123
补丁的同时进行备份
[root@localhost mnt]# patch -b westos westos.path ##进行备份生成.orig文件,-b保留原文件,并进行补丁
patching file westos
[root@localhost mnt]# ls
westos westos1 westos.orig westos.path
[root@localhost mnt]# cat westos.orig ##补丁之前的源文件
hello 123
linux
对目录的比较:
[root@localhost mnt]# mkdir linux
[root@localhost mnt]# mkdir unix
[root@localhost mnt]# ls
linux unix westos westos1 westos.orig westos.path
[root@localhost mnt]# touch linux/hello
[root@localhost mnt]# diff -r linux/ unix/
Only in linux/: hello
2.cut命令
cut 命令多用与字符截取
- cut -d 指定分隔符
- cut -f 1,7|1-7 指定截取的列 1,7(1和7) 1-7(1到7)
- cut -c 1,4|1-4 指定截取的字符位置
[root@localhost mnt]# cp /etc/passwd .
[root@localhost mnt]# ls
ip_show.sh passwd westos westos1
[root@localhost mnt]# vim passwd 删除一部分便于实验
[root@localhost mnt]# cut -d : -f 1-2 passwd ##以:为分隔符截取第一列和第二列
[root@localhost mnt]# cut -d : -f 1,7 passwd ##以:为分隔符截取第1列和第7列
[root@localhost mnt]# cut -c 1-4 passwd ##截取第一到第四个字符
3.sort命令
多用于字符排序
sort -n 纯数字排序
sort -r 倒序
sort -u 去掉重复数字
sort -o 输出到指定文件中
sort -t 指定分隔符
sort -k 指定要排序的列
[root@localhost mnt]# vim hello ##编辑一个由数字组成的文件
[root@localhost mnt]# cat hello
3
4
5
2
1
6
8
4
45
55
33
2
[root@localhost mnt]# sort -n hello ##正序排序
1
2
2
3
4
4
5
6
8
33
45
55
[root@localhost mnt]# sort -r hello ##倒序排序,默认只排第一位的数字
8
6
55
5
45
4
4
33
3
2
2
1
[root@localhost mnt]# sort -nr hello ##倒叙排序
55
45
33
8
6
5
4
4
3
2
2
1
[root@localhost mnt]# sort -nr -u hello ##倒叙排序,删除重复的项
55
45
33
8
6
5
4
3
2
1
[root@localhost mnt]# sort -nr hello -o file ##倒序排序输出到file文件
[root@localhost mnt]# cat file ##查看文件
55
45
33
8
6
5
4
4
3
2
2
1
[root@localhost mnt]# vim hello
[root@localhost mnt]# cat hello
[root@localhost mnt]# sort -t : -k 2 -n hello
4.uniq命令
对重复字符做相应的处理
- uniq -u 显示唯一的行
- uniq -d 显示重复的行
- uniq -c 每行显示一次并统计重复次数
[root@localhost mnt]# vim hello
[root@localhost mnt]# cat hello
[root@localhost mnt]# sort -n hello |uniq -c
[root@localhost mnt]# sort -n hello |uniq -d ##将文件正序排列,显示重复的数字
[root@localhost mnt]# sort -n hello |uniq -u ##将文件正序排列,显示不重复的数字
5.&&和||运算符
&& 用来执行条件成立后执行的命令,|| 用来执行条件不成立后执行的命令
[root@localhost mnt]# ping -c1 -w1 172.25.254.62 &> /dev/null && echo this is up|| echo this is down
this is up
## -c代表次数 -w代表等待时间
编写脚本判断ip是否存在
[root@localhost mnt]# vim ip_check.sh
#!/bin/bash
[ -z "$1" ] &&{
echo please give me a ipaddress!!
exit 1
}
ping -c1 -w1 $1 &> /dev/null &&{
echo "$1 is up"
}||{
echo "$1 is down"
}
执行脚本:
[root@localhost mnt]# sh ip_check.sh
please give me a ipaddress!!
[root@localhost mnt]# sh ip_check.sh 172.25.254.62
172.25.254.62 is up
[root@localhost mnt]# sh ip_check.sh 172.25.254.5
172.25.254.5 is down
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” ] ne表示不等于,a表示and都满足
[ “A”−ne“ A
”
−
n
e
“
B” -o “A”−gt“
A
”
−
g
t
“
B” ] o表示or至少满足一个
[ -z “A”]表示值为空[−n“ A
”
]
表
示
值
为
空
[
−
n
“
A” ] 表示值不为空
[ “file1” -ef “file2” ] ef 表示节点是否相同
[ “file1” -nt “file2” ] 表示new,file1是否比file2文件新
[ “file1” -ot “file2” ] 表示old,file1是否比file2文件老
[-e “file”] 文件是否存在
[-f “file”] 文件是否为普通文件
[-L “file”] 文件是否为符号链接
[-S “file”] 文件是否为套接字
[-b “file”] 文件是否为块设备
[-d “file”] 文件是否为目录
[-c “file”] 文件是否为特殊文件
判断两个数字是否相等
[root@localhost mnt]# a=1
[root@localhost mnt]# b=2
[root@localhost mnt]# [ "$a" == "$b" ] && echo yes || echo no
no
[root@localhost mnt]# test "$a" == "$b" && echo yes ||echo no
no
判断节点数是否相等
[root@localhost mnt]# touch file
[root@localhost mnt]# ln file file1
[root@localhost mnt]# ls -li *
8842676 -rw-r--r-- 2 root root 0 Jun 15 22:04 file
8842676 -rw-r--r-- 2 root root 0 Jun 15 22:04 file1
[root@localhost mnt]# [ "file" -ef "file1" ] && echo yes || echo no
yes
判断文件的新旧
[root@localhost mnt]# rm -rf file1
[root@localhost mnt]# touch file1
[root@localhost mnt]# [ "file" -ot "file1" ] && echo yes || echo no
yes
[root@localhost mnt]# [ "file" -nt "file1" ] && echo yes || echo no
no
编写脚本,输入一个数字判断是否在10以内
[root@localhost mnt]# vim num_check.sh
#!/bin/bash
[ -z "$1" ]&&{ ##$1表示输入的数字
echo please input a number after scripts!!
exit 1
}
[ "$1" -gt "0" -a "$1" -lt "10" ]&& { ##表示$1大于等于0并且小于等于10
echo "$1" is between 1~10
}||{
echo "$1" is not between 1~10
}
~
调用脚本:
[root@localhost mnt]# sh num_check.sh
please input a number after scripts!!
[root@localhost mnt]# sh num_check.sh 33
33 is not between 1~10
[root@localhost mnt]# sh num_check.sh 5
5 is between 1~10
编写脚本,判断文件是否存在,如果存在判断其类型
[root@localhost mnt]# vim file_check.sh
#!/bin/bash
[ -z "$1" ]&& {
echo "please input a filename after scripts!!"
exit 1
}
[ -e "$1" ]||{
echo "$1 is not exist!!"
exit 0
}
[ -L "$1" ]&&{
echo "$1 is a link"
exit 0
}
[ -f "$1" ]&&{
echo "$1 is a common file"
exit 0
}
[ -b "$1" ]&&{
echo "$1 is a block file"
}
调用脚本:
[root@localhost mnt]# sh file_check.sh
please input a filename after scripts!!
[root@localhost mnt]# sh file_check.sh file
file is a common file
7.tr命令
tr命令用于 大小写转换
编写脚本,判断输入的内容和hello是否相等
[root@localhost mnt]# vim test.sh
#!/bin/bash
[ "$1" = "hello" ]&& {
echo yes
}||{
echo no
}
脚本调用:
[root@localhost mnt]# sh test.sh hello
yes
[root@localhost mnt]# sh test.sh HELLO ##对于大写的hello不能识别
no
解决方案:
[root@localhost mnt]# vim test.sh
#!/bin/bash
WORD=$(echo $1 |tr 'A-Z' 'a-z') ##利用变量WORD,将输入的字符从大写转换为小写
[ "$WORD" = "hello" ]&& {
echo yes
}||{
echo no
}
脚本调用:
[root@localhost mnt]# sh test.sh hello
yes
[root@localhost mnt]# sh test.sh HELLO
yes
五、用脚本建立用户
基本思路:
1、查看脚本后所跟字符是不是两串
2、检测第一串字符是不是用户
3、建立不存在的用户并设定密码
[root@localhost mnt]# vim user_create.sh
#!/bin/bash
[ "$#" -eq "2" ]||{
echo "please input a username and password after script!!"
exit 1
}
Check_User=`getent passwd $1`
[ -n "$Check_User" ] &&{
echo $1 is exist!!
exit 1
}
useradd $1
echo $2 |passwd --stdin $1
~
脚本调用:
[root@localhost mnt]# sh user_create.sh
please input a username and password after script!!
[root@localhost mnt]# sh user_create.sh student 123
student is exist!!
[root@localhost mnt]# sh user_create.sh linux 123
Changing password for user linux.
passwd: all authentication tokens updated successfully.
[root@localhost mnt]# su - linux
[linux@localhost ~]$