shell学习第二天
shell学习第二天
什么是hash 哈希
Hash,一般翻译做散列、杂凑,或音译为哈希,是把任意长度的输入(又叫做预映射pre-image)通过散列算法变换成固定长度的输出,该输出就是散列值(哈希值)。
比方说—> 我把字母: dingjiang —》通过哈希函数(方法) 转换成 一个值
md5
MD5信息摘要算法(英语:MD5 Message-Digest Algorithm),一种被广泛使用的密码散列函数,可以产生出一个128位(16字节)的散列值(hash value),用于确保信息传输完整一致。
如果知道自己的文件有没有被动过
把自己的文件保存一份,然后通过md5sum 文件名 记住这个哈希值,这样可以用来检验文件是否被动过
或者通过 diff 命令
diff 文件1 文件2
没有输出就代表两个文件一样,有输出就不一样
seq 产生序列
[root@gh-shell ~]# seq 5
1
2
3
4
5
[root@gh-shell ~]# seq 5 8
5
6
7
8
[root@gh-shell ~]#
[root@gh-shell ~]# seq 8 -1 5
8
7
6
5
[root@gh-shell ~]# seq 1 +2 10
1
3
5
7
9
[root@gh-shell ~]#
[root@gh-shell shell]# cat for.sh
#!/bin/bash
for i in $(seq $1)
do
echo $i
done
[root@gh-shell shell]# bash for.sh 6
1
2
3
4
5
6
[root@gh-shell shell]#
根据这个可以更改昨天的作业2
[root@gh-shell shell]# cat create_pass.sh
#!/bin/bash
#清空一下/shell/user_pwd.txt
>/shell/user_pwd.txt
user=$1
num=$2
for i in $(seq $num)
do
username="$user$i"
useradd $username
pass=$(echo $RANDOM|md5sum|cut -b 1-10)
echo $pass|passwd --stdin $username
#保存账户和密码
echo "$username $pass" >>/shell/user_pwd.txt
echo "User $username created and password set."
done
[root@gh-shell shell]# bash create_pass2.sh gf 10
更改用户 gf1 的密码 。
passwd:所有的身份验证令牌已经成功更新。
User gf1 created and password set.
更改用户 gf2 的密码 。
passwd:所有的身份验证令牌已经成功更新。
User gf2 created and password set.
更改用户 gf3 的密码 。
passwd:所有的身份验证令牌已经成功更新。
User gf3 created and password set.
更改用户 gf4 的密码 。
passwd:所有的身份验证令牌已经成功更新。
User gf4 created and password set.
更改用户 gf5 的密码 。
passwd:所有的身份验证令牌已经成功更新。
User gf5 created and password set.
更改用户 gf6 的密码 。
passwd:所有的身份验证令牌已经成功更新。
User gf6 created and password set.
更改用户 gf7 的密码 。
passwd:所有的身份验证令牌已经成功更新。
User gf7 created and password set.
更改用户 gf8 的密码 。
passwd:所有的身份验证令牌已经成功更新。
User gf8 created and password set.
更改用户 gf9 的密码 。
passwd:所有的身份验证令牌已经成功更新。
User gf9 created and password set.
更改用户 gf10 的密码 。
passwd:所有的身份验证令牌已经成功更新。
User gf10 created and password set.
[root@gh-shell shell]#
一键删除用户
[root@gh-shell shell]# cat rmcreate.sh
#!/bin/bash
username=$1
num=$2
for i in $(seq $num)
do
user="$username$i"
userdel -r $user
echo "删除$user成功。。。"
done
[root@gh-shell shell]#
写一个监控内存的脚本
[root@gh-shell shell]# cat mem_monitor.sh
#!/bin/bash
#定义日志的文件名和日期
cdate=$(date +%Y%m%d%H%M%S)
logfile="/tmp/memlog_$0.log"
#得到机器的ip地址
ip_addr=$(ip add|grep "ens33$"|awk '{print $2}')
#总内存和使用的内存
mem_all=$(free -m|head -2|tail -1|awk '{print $2}')
mem_used=$(free -m|head -2|tail -1|awk '{print $3}')
#计算内存的使用率
percent=$(echo "scale=2;$mem_used/$mem_all*100"|bc)
#定义一个告警阈值,然后进行比较
threshold=8
#让bc进行小数的比较,得到输出结果值,1表示成立 0 失败
flag=$(echo "$percent > $threshold"|bc)
#判断
if (( $flag == 1 ));then
echo "$cdate $ip_addr Total:${mem_all}M Mem used:${percent}%" >>$logfile
fi
[root@gh-shell shell]#
[root@gh-shell shell]# bash mem_monitor.sh
[root@gh-shell shell]# cat /tmp/memlog_mem_monitor.sh.log
20240112161200 192.168.153.161/24 Total:1819M Mem used:12.00%
[root@gh-shell shell]#
小知识点
free -h 看内存
[root@gh-shell shell]# free -h
total used free shared buff/cache available
Mem: 1.8G 238M 1.4G 9.5M 166M 1.4G
Swap: 2.0G 0B 2.0G
[root@gh-shell shell]#
buffer和cache是有区别的 一个是写进磁盘 一个是读取 cache,临时存放数据
提取一个字段 awk
[root@gh-shell shell]# free -m|head -2|tail -1|awk '{print $2}'
1819
[root@gh-shell shell]#
awk是linux里的字段(列)截取的命令
'{}'是固定语法
print 是awk里的输出的命令
$1 是第1个字段
$2 是第2个字段
$0 代表一行
玩一下
[root@gh-shell shell]# w|head -4|tail -1|awk '{print $3}'
192.168.153.1
[root@gh-shell shell]#
清除缓存
[root@gh-shell shell]# echo 3 >/proc/sys/vm/drop_caches #告诉linux系统去清楚缓存里的数据,释放内存空间
[root@gh-shell shell]# free -m
total used free shared buff/cache available
Mem: 1819 228 1533 9 57 1484
Swap: 2047 0 2047
[root@gh-shell shell]#
自带计算器 bc
[root@gh-shell shell]# bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
1+5
6
1.1+5.6
6.7
quit
[root@gh-shell shell]#
[root@gh-shell shell]# echo 4.5+6.7|bc
11.2
[root@gh-shell shell]#
[root@gh-shell shell]# echo "scale=2;56/5"|bc # 保留小数点后两位scale
11.20
[root@gh-shell shell]#
[root@gh-shell shell]# echo "170.00 > 90"|bc
1 # 1表示正确
[root@gh-shell shell]# echo "17.00 > 90"|bc
0 # 0表示错误
[root@gh-shell shell]# result=$(echo "17.00 > 90"|bc)
[root@gh-shell shell]# echo $result
0
[root@gh-shell shell]# result=$(echo "170.00 > 90"|bc)
[root@gh-shell shell]# echo $result
1