shell中常用的基础命令
shell中常用的基础命令
命令执行流顺序:
从上到下(默认),特殊可变
程序:由数据和逻辑组成
1.diff(比较两个文件的不同)
企业中打补丁更新版本数据
输出信息分析:
[num1,num2][a|c|d][num3,num4]
num1,num2 ##第一个文件中的行
a ##添加
c ##更改
d ##删除
< ##第一个文件中的内容
> ##第二个文件中的内容
num3,num4 ##第二个文件中的行
cd /mnt/
ls
vim westos
cat westos > westos.new
vim westos.new
cat westos
hello westos
cat westos.new
hello westos
2021-03-07
diff westos westos.new
##第一个文件添加一行
diff westos.new westos
##第二个文件删除一行
vim westos
cat westos
hello westo
diff westos westos.new
##更改
diff -u westos westos.new > westos.path
##生成补丁
ls
常用参数:
-b ##忽略空格
-B ##忽略空行
-i ##忽略大小写
-c ##显示文件所有内容并标识不同
-r ##对比目录
-u ##合并输出
vim westos.new
diff westos westos.new
diff -b westos westos.new
##添加空格,可以对比
vim westos
diff -b westos westos.new
##-b忽略空格,完全一致
vim westos.new
diff westos westos.new
diff -B westos westos.new
##-B忽略空行,完全一致
vim westos.new
diff westos westos.new
diff -i westos westos.new
##-i忽略大小写
vim westos.new
diff -i westos westos.new
vim westos
vim westos.new
diff westos westos.new
diff -c westos westos.new
##-c显示文件所有内容并标识不同
##一样不一样的都显示
ls
rm -fr *
ls
mkdir westos westos1
ls
mkdir westos/westos westos1/westos
touch westos/westos/file
diff westos/ westos1/
diff -r westos/ westos1/
##-r对比目录
##递归,指出目录的不同
2.patch(打补丁)
打补丁:更新后的脚本和原始脚本
patch 原文件 补丁文件
-b ##备份原文件
vim westos
cp westos westos.new
ls
echo 123 >> westos.new
cat westos westos.new
westos hello
westos hello
123
tail westos westos.new
diff -u westos westos.new > westos.path
##-u合并输出
##生成补丁,原文件+补丁文件
ls
cat westos.path
--- westos 2021-03-07 09:46:42.553000000 +0800
+++ westos.new 2021-03-07 09:47:16.882000000 +0800
@@ -1 +1,2 @@
westos hello
+123
dnf install patch -y
##安装打补丁工具
patch westos westos.path
diff westos westos.new
vim westos
cat westos
ls
rm -fr *
vim westos
vim westos.new
diff -u westos westos.new > westos.path
ls
patch -b westos westos.path
##-b生成原文件副本
ls
cat westos
hello westos
123
cat westos.orig
hello westos
3.cut(截取列)
cut
-d : ##指定:为分隔符
-f
##指定显示的列
##5 第五列
##3,5 3和5列
##3-5 3到5列
##5- 第五列以后
##-5 到第五列
cd /mnt/
ls
rm -fr *
cp /etc/passwd .
ls
vim passwd
cat passwd
head -n 3 passwd | tail -n 1
##查看前三行中的最后一行
cut -d : -f 1 passwd
##查看第一列
cut -d x -f 1 passwd
##分割符
cut -d x -f 2 passwd
cut -d : -f 1 passwd
cut -d : -f 1,7 passwd
cut -d : -f 1-7 passwd
##查看第一列-第七列
cut -d : -f 1-3 passwd
cut -d : -f 3- passwd
##查看第三列到最后
ls
vim passwd
##:与空格都占列数 x为第四列
cut -c 1-4 passwd
##第一个字符到第四个字符
cut -c 2- passwd
##第二个字符到最后一个字符
4.sort(排序)
-n ##纯数字排序
-r ##倒序
-u ##去掉重复
-o ##输出到指定文件
-t ##指定分隔符
-k ##指定排序的列
ls
rm -fr passwd
vim westos
cat westos
sort westos
##排序,只是排了第一个字符
sort -n westos
##纯数字排序
sort -rn westos
##倒序
sort -rnu westos
##去掉重复
vim westos
sort -n westos
sort -t : -k 2 -n westos
##按输出排序
5.uniq(重复部分进行处理)
##统计重复的个数
vim westos
sort -n westos
sort -n westos | uniq -d
##合并重复并统计重复个数
##uniq不能对文件,只能处理输出
sort -n westos | uniq -c
##显示重复的行
sort -n westos | uniq -u
##显示唯一的行
测试
要求:
1.ifconfig网卡
可以显示此网卡的信息
显示信息中包含此网卡使用的ip地址
请用命令过滤此ip并在输出时只显示ip其他信息不显示
2.找出能登陆系统用户中UID最大的用户,并显示其名称
ifconfig ens3 | grep "inet "| cut -d " " -f 10
##过滤inet
grep -E "bash$|sh$" /etc/passwd | sort -t : -k 3 | tail -n 1 | cut -d : -f 1
##过滤可登陆系统的用户
id westos
id lee
id root
sort -rnt : -k 3 /etc/passwd | awk -F : '/bash$|sh$/{print $1}' | head -n1
##awk搜索
##&&并且
6.tr(转义字符)
tr 'a-z' 'A-Z' ##小写转大写
tr 'A-Z' 'a-z' ##大写转小写
echo westos WESTOS | tr 'a-z' 'A-Z'
##都转换成大写
echo westos WESTOS | tr 'A-Z' 'a-z'
##都转换成小写
echo westos WESTOS | tr 'w' 'h'
##w换成h
echo westos WESTOS | tr 'we' 'h'
##w和e都替换成h
echo westos WESTOS | tr 'westos' 'l'
##westos都替换成l
##tr只能替换成单个字符
echo westos WESTOS | tr 'westos' 's'
7.test(逻辑判断)
test数字对比
test = [ ] ##[ ]就相当于test命令
"test $a = $b" = [ "$a" = " $b" ]
test数字对比
=
!=
-eq ##等于
-ne ##不等于
-le ##小于等于
-lt ##小于
-ge ##大于等于
test的条件关系
-a ##并且
-o ##或者
test对空的判定
-n ##nozero 判定内容不为空
-z ##zero 判定内容为空
cd
a=1
b=1
[ "$a" = "$b" ] && echo yes || echo no
[ "$a" -eq "$b" ] && echo yes || echo no
##判断a是否等于b
[ "$a" != "$b" ] && echo yes || echo no
b=2
[ "$a" != "$b" ] && echo yes || echo no
[ "$a" -ne "$b" ] && echo yes || echo no
[ "$a" < "$b" ] && echo yes || echo no
[ "$a" -lt "$b" ] && echo yes || echo no
[ "$a" -le "$b" ] && echo yes || echo no
[ "$a" -gt "$b" ] && echo yes || echo no
[ "$a" -ge "$b" ] && echo yes || echo no
b=2
[ "$b" -gt "0" -a "$b" -lt "10" ] && echo yes || echo no
##判断b是否在10之内
b=20
[ "$b" -gt "0" -a "$b" -lt "10" ] && echo yes || echo no
[ "$b" -le "0" -o "$b" -ge "10" ] && echo yes || echo
##小于等于0或者大于等于10
no
cd /mnt/
echo $c
[ -z "$c" ] && echo yes || echo no
[ -n "$c" ] && echo yes || echo no
##-z zero判断是否为空
##-n no zero 判断不为空
c=5
[ -z "$c" ] && echo yes || echo no
[ -n "$c" ] && echo yes || echo no
[ ! -n "$c" ] && echo yes || echo no
##条件反选
##为空
[ ! -z "$c" ] && echo yes || echo no
##不为空
对于文件类型的判定
test对于文件的判定
-ef ##文件节点号是否一致
-nt ##文件1是不是比文件2新
-ot ##文件1是不是比文件2老
-d ##目录
-S ##套接字
-L ##软连接
-e ##存在
-f ##普通文件
-b ##块设备
-c ##字符设备
ls
ls -i
##判断文件节点号
ln /mnt/westos /mnt/westos1
##做软连接
ls -i
##节点号不一致,仍显示yes
[ "/mnt/westos" -ef "/mnt/westos1" ] && echo yes || echo no
ln -s /mnt/westos /mnt/westos2
ls -i
[ "/mnt/westos" -ef "/mnt/westos2" ] && echo yes || echo no
ll
touch /mnt/westos3
ls -i
[ "/mnt/westos" -ef "/mnt/westos3" ] && echo yes || echo no
##westos较westos3新
[ "/mnt/westos" -nt "/mnt/westos3" ] && echo yes || echo no
##westos较westos3先建立
[ "/mnt/westos" -ot "/mnt/westos3" ] && echo yes || echo no
实验
要求:
vim /mnt/westos.sh
/mnt/westos.sh create delete
/mnt/westos.sh create lee
当lee存在时
lee is exist
当lee不存在
会建立lee并显示lee is created
/mnt/westos.sh delete lee
当lee存在时
删除lee并显示lee deleted
当lee不存在时什么的都不做
当/mnt/westos.sh haha lee
报错:
error:wrong option haha , Please input create and delete following script
当/mnt/westos.sh后跟字符串个数不足两个时报错:
Usage: /mnt/westos.sh <create | delete > <username>
#!/bin/bash
[ "$#" -lt 2 ] && {
echo "Usage: /mnt/westos.sh <create | delete > <username>"
exit
}
##字符串个数小于2
OPTS=`echo $1 | tr 'A-Z' 'a-z'`
##字符转义,更加严谨
[ "$OPTS" = "create" -o "$OPTS" = "delete" ] || {
echo -e "\033[31mError: wrong option $* , Please input create and de lete following script\033[0m"
exit
}
[ "$OPTS" = "create" ] && {
id $2 &> /dev/null && {
echo "user $2 is exist"
}||{
useradd $2 && echo "$2 is created"
}
}||{
id $2 &> /dev/null && userdel -r $2 && echo "user $2 is deleted"
}
##等于create或delete
8.条件判断
&& ##前面命令执行成功
|| ##否则,前面命令执行失败则
实验1
vim test.sh
ping -c1 -w1 172.25.254.11 &> /dev/null && {
##无用信息都扔掉
##成功即为yes
##失败即为no
date
echo yes
}||{
date
echo no
}
##多条命令 缩进
##先执行date,再执行echo
##否在后面
ping 172.25.254.11
sh test.sh
实验2
要求:
编写测试脚本
/mnt/westossh /mnt/westos haha
haha is exist haha is not exist
cd /mnt/
vim westos,sh
[ -z "$*" ] && {
##-z zero判断是否为空
##-n no zero 判断不为空
echo -e "\033{31mError: Please input username following script\033[0m"
exit
}
##颜色代码
##当条件不成立,执行后面括号
id $* &> /dev/null && {
echo $* is exist
}||{
echo $* is not exist
}
sh westos.sh
sh westos.sh haha
sh westos.sh westos
实验3
#!/bin/bash
[ -z "$*" ] && {
echo "ERROR: Wrong format, Please input check file following script"
exit
}
[ -e "$*" ] || {
echo "$* is not exist!"
exit
}
##-e判断文件是否存在
[ -L "$*" ] && {
echo "$* is link file"
exit
}
##-L判断文件是否软连接
[ -S "$*" ] && {
echo "$* is tao socket"
exit
}
##-S判断文件是否为套接字
[ -f "$*" ] && {
echo "$* is common file"
exit
}
##-f判断文件是否为普通文件
[ -d "$*" ] && {
echo "$* is directory"
exit
}
##-d判断文件是否目录
##-b判断文件是否块设备
##-c判断文件是否字符设备