dstat
dstat 是一个使用python语言编写的获取linux系统的性能参数的命令(cpu,内存,磁盘,网络带宽)
[root@lier 517]# dstat -am
----total-cpu-usage---- -dsk/total- -net/total- ---paging-- ---system-- ------memory-usage-----
usr sys idl wai hiq siq| read writ| recv send| in out | int csw | used buff cach free
13 35 52 0 0 0| 0 0 | 182B 358B| 0 0 |3003 2994 | 380M 2108k 521M 916M
12 35 53 0 0 0| 0 0 | 60B 1058B| 0 0 |3649 4066 | 380M 2108k 521M 916M
12 35 53 1 0 0| 0 0 | 60B 426B| 0 0 |3236 3230 | 380M 2108k 521M 916M
15 32 53 0 0 0| 0 0 | 60B 426B| 0 0 |2579 2148 | 380M 2108k 521M 916M
15 33 52 0 0 1| 0 0 | 60B 426B| 0 0 |2127 1571 | 380M 2108k 521M 917M
15 33 52 0 0 0| 0 0 | 60B 426B| 0 0 |2227 1633 | 380M 2108k 521M 917M
12 34 53 0 0 1| 0 0 | 60B 426B| 0 0 |2110 1549 | 380M 2108k 521M 916M
13 34 53 0 0 0| 0 0 | 60B 426B| 0 0 |2153 1553 | 380M 2108k 521M 917M
13 34 53 0 0 1| 0 0 | 60B 426B| 0 0 |2120 1539 | 380M 2108k 521M 917M
14 34 52 0 0 0| 0 0 | 120B 516B| 0 0 |2237 1584 | 380M 2108k 521M 916M
for循环
1、使用{1…100}:
[root@lier 517]# cat for.sh
#!/bin/bash
# {1..10} 产生一个1到10的序列
for i in {1..10}
do
echo "sanchuang $i"
done
[root@lier 517]# bash for.sh
sanchuang 1
sanchuang 2
sanchuang 3
sanchuang 4
sanchuang 5
sanchuang 6
sanchuang 7
sanchuang 8
sanchuang 9
sanchuang 10
seq
seq 用来产生一个数字的序列–》集合
# 产生1-10的序列
[root@master 517]# seq 10
1
2
3
4
5
6
7
8
9
10
# 产生5-10的序列
[root@master 517]# seq 5 10
5
6
7
8
9
10
# 5是起始值 2 是步长 step 10 最后的数字
[root@master 517]# seq 5 2 10
5
7
9
# 产生1-10中的奇数序列
[root@master 517]# seq 1 2 10
1
3
5
7
9
# 产生1-10中的偶数序列
[root@master 517]# seq 2 2 10
2
4
6
8
10
2、seq在for循环中的使用
[root@lier 517]# cat for.sh
#!/bin/bash
# 指定seq生成$1个序列
for i in $(seq $1)
do
echo $i
done
[root@lier 517]# bash for.sh 5
1
2
3
4
5
3、使用目录下的文件 例如:/boot/*
[root@lier 517]# cat for.sh
#!/bin/bash
# 读取/boot目录下的所有文件
for i in /boot/*
do
echo $i
done
[root@lier 517]# bash for.sh
/boot/config-3.10.0-1160.62.1.el7.x86_64
/boot/config-3.10.0-327.el7.x86_64
/boot/efi
/boot/grub
/boot/grub2
/boot/initramfs-0-rescue-e57b9369a2ce49149016d51c29303550.img
/boot/initramfs-3.10.0-1160.62.1.el7.x86_64.img
/boot/initramfs-3.10.0-1160.62.1.el7.x86_64kdump.img
/boot/initramfs-3.10.0-327.el7.x86_64.img
/boot/initramfs-3.10.0-327.el7.x86_64kdump.img
/boot/initrd-plymouth.img
/boot/symvers-3.10.0-1160.62.1.el7.x86_64.gz
/boot/symvers-3.10.0-327.el7.x86_64.gz
/boot/System.map-3.10.0-1160.62.1.el7.x86_64
/boot/System.map-3.10.0-327.el7.x86_64
/boot/vmlinuz-0-rescue-e57b9369a2ce49149016d51c29303550
/boot/vmlinuz-3.10.0-1160.62.1.el7.x86_64
/boot/vmlinuz-3.10.0-327.el7.x86_64
4、读取给出的字符串
[root@lier 517]# cat for.sh
#!/bin/bash
for i in "wangdan" "siyuxiang" "fuxintao"
do
echo $i
done
[root@lier 517]# bash for.sh
wangdan
siyuxiang
fuxintao
5、遍历命令中的返回结果
[root@lier 517]# cat for.sh
#!/bin/bash
for i in $( ls /etc )
do
echo $i
done
如果不用$引用命令的值
[root@lier 517]# cat for.sh
#!/bin/bash
for i in ls /etc
do
echo $i
done
[root@lier 517]# bash for.sh
ls
/etc
6、</etc/hosts读取文件内容
[root@lier 517]# cat for.sh
#!/bin/bash
# 读取/etc/hosts 下面的所有内容,只能读取文件,不能读取文件夹
for i in $(</etc/hosts) # -->读取/etc/hosts是文件 -->等同于使用read()函数读取/etc/hosts文件里的内容
do
echo $i
done
[root@lier 517]# bash for.sh
127.0.0.1
localhost
localhost.localdomain
localhost4
localhost4.localdomain4
::1
localhost
localhost.localdomain
localhost6
localhost6.localdomain6
7、引用位置变量
[root@lier 517]# cat for.sh
#!/bin/bash
for i in $*
do
echo "位置变量: $i"
done
[root@lier 517]# bash for.sh 5 feng li liu wen zhang jiu
位置变量: 5
位置变量: feng
位置变量: li
位置变量: liu
位置变量: wen
位置变量: zhang
位置变量: jiu
位置变量
位置变量: position variable
给脚本传递参数–》传参
$0:脚本名
$1:第1个位置变量
$2:第2个位置变量 依次类推
KaTeX parse error: Expected 'EOF', got '#' at position 1: #̲、@:给脚本传递的位置变量的个数
$*:所有位置变量的值
$$ : 是当前 终端的pid号
[root@master 517]# cat pos_var.sh
#!/bin/bash
echo "脚本名: $0"
echo "第1个位置变量: $1"
echo "第2个位置变量: $2"
echo "第3个位置变量: $3"
echo "第4个位置变量: $4"
echo "第5个位置变量: $5"
echo "第6个位置变量: $6"
echo "一共有 $# 个位置变量"
echo "它们的值如下所示: $*"
echo "它们的值如下所示: $@"
[root@master 517]# bash pos_var.sh liubang xiangyu songjiang yaolifan yangyongjie lijunlin lisi wangwu
脚本名: pos_var.sh
第1个位置变量: liubang
第2个位置变量: xiangyu
第3个位置变量: songjiang
第4个位置变量: yaolifan
第5个位置变量: yangyongjie
第6个位置变量: lijunlin
一共有 8 个位置变量
它们的值如下所示: liubang xiangyu songjiang yaolifan yangyongjie lijunlin lisi wangwu
它们的值如下所示: liubang xiangyu songjiang yaolifan yangyongjie lijunlin lisi wangwu
$$查看当前的pid进程号
[root@lier 517]# echo $$
56876
[root@lier 517]# ps -aux|grep 56876
root 41849 0.0 0.0 112824 988 pts/3 R+ 16:44 0:00 grep --color=auto 56876
root 56876 0.0 0.1 115800 2228 pts/3 Ss 14:40 0:00 -bash
练习
- 新建10个用户: 用户名以sanchuang开头 ,sanchuang1 sanchuang2 … sanchuang10
useradd sanchuang1
[root@master 517]# cat adduser.sh
#!/bin/bash
for i in {1..10}
do
useradd sanchuang$i
done
查看是否创建成功:
# 1、查看是否有用户名的id
[root@master 517]# id sanchuang1
uid=1014(sanchuang1) gid=1015(sanchuang1) 组=1015(sanchuang1)
# 2、登录用户
[root@master 517]# su - sanchuang1
[sanchuang1@master ~]$ exit
登出
# 3、/etc/passwd 会保存所有的用户的信息,新的用户的信息在文件末尾,查看后10行
[root@master 517]# cat /etc/passwd|tail
sanchuang1:x:1014:1015::/home/sanchuang1:/bin/bash
sanchuang2:x:1015:1016::/home/sanchuang2:/bin/bash
sanchuang3:x:1016:1017::/home/sanchuang3:/bin/bash
sanchuang4:x:1017:1018::/home/sanchuang4:/bin/bash
sanchuang5:x:1018:1019::/home/sanchuang5:/bin/bash
sanchuang6:x:1019:1020::/home/sanchuang6:/bin/bash
sanchuang7:x:1020:1021::/home/sanchuang7:/bin/bash
sanchuang8:x:1021:1022::/home/sanchuang8:/bin/bash
sanchuang9:x:1022:1023::/home/sanchuang9:/bin/bash
sanchuang10:x:1023:1024::/home/sanchuang10:/bin/bash
[root@master 517]#
2.有一个文件user.txt 里面有很多用户名,使用脚本批量新建user.txt里的用户
fengdeyong shenjiemi liuchang dengchao
wanglianfang wangxiao yangqin liwenqian liziying
[root@master 517]# cat user.txt
fengdeyong shenjiemi liuchang dengchao
wanglianfang wangxiao yangqin liwenqian liziying
[root@master 517]# cat adduser2.sh
#!/bin/bash
#for i in $(cat user.txt)
for i in $(</lianxi/517/user.txt)
do
useradd $i
done
[root@master 517]#
while循环
死循环的几种方式
while:
while true
while [1]
每秒输出一次i,无限输出
[root@master 517]# cat while.sh
#!/bin/bash
i=1
# 构建一个死循环
while :
do
echo $i
((i++)) # 每循环一次,i+1
sleep 1 # 延时1s
done
当i<=10时执行循环,否则跳出循环
[root@master 517]# cat while.sh
#!/bin/bash
i=1
while ((i<=10))
do
echo $i
((i++))
sleep 1
done
${!a} : 表示引用变量a的值指向的值,两次引用
[root@lier 517]# a=b
[root@lier 517]# b=123
[root@lier 517]# echo $a
b
[root@lier 517]# echo ${!a}
123
a 表 示 引 用 了 a 的 值 , 也 就 是 b , 而 a表示引用了a的值,也就是b,而 a表示引用了a的值,也就是b,而{!a}表示引用两次,首先引用a的值b,再引用b的值,也就是123
示例:
[root@master 517]# cat test.sh
#!/bin/bash
feng=fengdeyong
echo ${!1}
[root@master 517]# bash test.sh feng
fengdeyong
脚本中${!1}表示引用位置变量1的值指向的值,也就是变量feng的值fengdeyong
练习
使用脚本输出english成绩大于60的人的名字和成绩,科目
while read用法
[root@master 517]# cat grade.txt
id name grade subject
1 cali 80 chinese
2 rose 90 english
3 tom 99 math
4 jarry 59 english
5 julia 85 chinese
[root@master 517]# cat top.sh
#!/bin/bash
while read id name grade subject #id name grade subject 是变量,将grade.txt一行里的不同字段赋值给变量
do
if [[ $subject == "english" ]] && (( $grade >=60 ));then
echo "$name : $grade : $subject"
fi
done <grade.txt # 读取文件grade.txt里面的内容
作业:
[root@master 517]# cat grade.txt
id name grade subject
1 cali 80 chinese
2 rose 90 english
3 tom 99 math
4 jarry 59 english
5 julia 85 chinese
[root@master 517]#
使用脚本输出english成绩大于60的人的名字和成绩,科目
chinese成绩大于60的人的名字和成绩,科目shell脚本实现的
python实现
shell
[root@lier 517]# cat grade.sh
#!/bin/bash
# 将grade.txt文件中每一行里面的值分别赋值给变量id,name,grade,subject
while read id name grade subject
do
if ([[ $subject == "english" ]] && (( $grade >= 60 ))) || ([[ $subject == "chinese" ]] && (( $grade >= 60 )));then
echo "name: $name grade: $grade subject: $subject"
fi
done <grade.txt # 读取grade.txt文件
[root@lier 517]# bash grade.sh
name: cali grade: 80 subject: chinese
name: rose grade: 90 subject: english
name: julia grade: 85 subject: chinese
[root@lier 517]# cat grade.txt
id name grade subject
1 cali 80 chinese
2 rose 90 english
3 tom 99 math
4 jarry 59 english
5 julia 85 chinese
python
[root@lier 517]# cat grade.py
#!/bin/python3
with open('grade.txt', 'r+') as fp:
for line in fp:
line = line.split()
if ('english' in line and int(line[2]) > 60) or ('chinese' in line and int(line[2]) > 60):
print(f"name: {line[1]} grade: {line[2]} subject: {line[3]}")
[root@lier 517]# python3 grade.py
name: cali grade: 80 subject: chinese
name: rose grade: 90 subject: english
name: julia grade: 85 subject: chinese