2018-05-22.log
将每天的磁盘使用状态写入到对应日期的文件[root@manager zhoumo]# cat var1.sh
#!/usr/bin/bash
Date=$(date +%F)
Path=/backup/log
[ -d $Path ] || mkdir -p $Path
df -h >> $Path/$Date.log
192.168.56.1 - - [21/May/2018:20:44:06 -0400] "GET /index.html HTTP/1.0" 404 169 "-" "ApacheBench/2.3" "-"/code/index.html
awk '{print $1}' access.log |sort |uniq -c |sort -nr
[root@manager zhoumo]# cat var3.sh
#!/usr/bin/bash
mem=$(ps aux |awk '{print $6}' |grep -v "RSS")
sum=0
for i in $mem
do
sum=$(( $sum + $i))
done
echo "总的内存大小为:$sum"
echo "以人性化显示为: $[ $sum / 1024 ] M "
1.批量修改txt为txt.bak
2.把所有的.bak文件打包压缩为123.tar.gz
3.批量还原文件的名字,及把增加的.bak再删除
[root@manager zhoumo]# cat var4.sh
#!/usr/bin/bash
File=$(find /backup/ -type f -name "*.txt")
#1.修改
for i in $File
do
mv $i $i.bak
done
sleep 1
echo "改名成功。。"
#2.打包
Bak_File=$(find /backup/ -type f -name "*.bak")
tar czf /backup/123.tar.gz ${Bak_File} &> /dev/null
sleep 1
echo "打包成功。。"
for i in $Bak_File
do
mv $i ${i%.*}
done
echo "还原成功。。"
[root@manager zhoumo]# cat var5.sh
#!/usr/bin/bash
num=$(awk -F ":" '$3>1000' /etc/passwd |wc -l)
if [ $num -gt 0 ];then
echo "你的Linux系统中有$num个普通用户"
else
echo "No common users"
fi
方式二:
[root@manager zhoumo]# cat var5.sh
#!/usr/bin/bash
i=0
while read line
do
User_Uid=$(echo $line |awk -F ":" '{print $3}')
User=$(echo $line |awk -F ":" '{print $1}')
User_shell=$(echo $line |awk -F ":" '{print $NF}')
if [ $User_Uid -ge 1000 -a $User_shell == "/bin/bash" ];then
echo "$User 是普通用户"
let i++
fi
done</etc/passwd
echo "一共有$i个用户"
思路:我们要用到一个文件就是.bash_history,然后再去sort、uniq
一条命令即可:
sort /root/.bash_history |sort |uniq -c |sort -nr |head
用shell处理以下内容
1、按单词出现频率降序排序!
2、按字母出现频率降序排序!
the squid project provides a number of resources toassist users design,implement and support squid installations. Please browsethe documentation and support sections for more infomation
1、按单词出现频率降序排序!
[root@manager ~]# echo "the squid project provides a number of resources toassist users design,implement and support squid installations. Please browsethe documentation and support sections for more infomation" |sed 's#[,.]# #g' |xargs -n1 |sort |uniq -c |sort -nr
2、按字母出现频率降序排序!
[root@manager ~]# echo "the squid project provides a number of resources toassist users design,implement and support squid installations. Please browsethe documentation and support sections for more infomation" |sed 's#[,.]# #g'|grep -o '[a-Z]' |sort |uniq -c |sort -nr
[root@manager zhoumo]# cat var9.sh
#!/usr/bin/bash
for i in {1..255}
do {
ip=10.0.0.$i
#1.第一次判断
ping -W1 -c1 $ip &> /dev/null
if [ $? -eq 0 ];then
echo "$ip 当前在线"
else
#2.如果判断第一次不存活,则在进行一次for循环,循环3次
for j in {1..3}
do
ping -W1 -c1 $ip &> /dev/null
if [ $? -eq 0 ];then
echo "$ip 主机存活"
else
echo "$ip 当前未在线"
fi
done
fi
} &
done
wait
Command action
h 显示命令帮助
f 显示磁盘分区
d 显示磁盘挂载
m 查看内存使用
u 查看系统负载
q 退出程序
[root@manager zhoumo]# cat caidan.sh
#!/usr/bin/bash
caidan(){
cat<--------------------------------------------------
Command action
h 显示命令帮助
f 显示磁盘分区
d 显示磁盘挂载
m 查看内存使用
u 查看系统负载
q 退出程序
--------------------------------------------------
EOF
}
caidan
while true
do
read -p "请选择你要查看系统的选项:[ h | f | d | m | u | q ]" Action
case $Action in
h)
clear
caidan
;;
f)
fdisk -l
;;
d)
df -Th
;;
m)
free -m
;;
u)
uptime
;;
q)
exit 1
;;
*)
read -p "请选择你要查看系统的选项:[ h | f | d | m | u | q ]" Action
esac
done
1.用户登陆该服务器则自动执行该脚本。
2.脚本提示可连接主机列表。
3.该脚本无法直接退出。
1.用户登陆该服务器则自动执行该脚本。 pass
2.脚本提示可连接主机列表。
3.该脚本无法直接退出。 pass
[root@chengyinwu tmp]# cat fun7.sh
#!/bin/bash
#********************************************************************
#Author: chengyinwu
#QQ: 991540698
#Date: 2019-11-04
#FileName: fun7.sh
#URL: https://www.yinwucheng.com
#Description:
#********************************************************************
meminfo () {
cat <<-EOF
----------------------------------
------请选择你要连接的主机--------
| 1) lb01-172.16.1.5 |
| 2) lb02-172.16.1.6 |
| 3) web01-172.16.1.7 |
| 4) web02-172.16.1.8 |
| 5) web03-172.16.1.9 |
| 6) db01-172.16.1.51 |
| 7) redis-172.16.1.52 |
| 8) nfs-172.16.1.31 |
| 9) backup-172.16.1.41 |
| h) help |
---------------------------------
EOF
}
#调用函数
meminfo
trap "" HUP INT TSTP
while true
do
read -p "请选择你要连接的主机:" Host
case $Host in
1|lb01)
ssh root@172.16.1.5
;;
2|lb02)
ssh root@172.16.1.6
;;
3|web01)
ssh root@172.16.1.7
;;
4|web02)
ssh root@172.16.1.8
;;
5|web03)
ssh root@172.16.1.9
;;
6|db01)
ssh root@172.16.1.51
;;
7|redis)
ssh root@172.16.1.52
;;
8|nfs)
ssh root@172.16.1.31
;;
9|backup)
ssh root@172.16.1.41
;;
h)
clear
meminfo
;;
exec)
exit
;;
*)
continue
esac
done
[root@chengyinwu tmp]# cat caidan.sh
#!/bin/bash
#********************************************************************
#Author: chengyinwu
#QQ: 991540698
#Date: 2019-11-04
#FileName: caidan.sh
#URL: https://www.yinwucheng.com
#Description:
#********************************************************************
mem_option () {
cat <<-EOF
----------------主菜单--------------
| 1) Install Nginx |
| 2) Install MySQL |
| 3) Install PHP |
| 4) quit |
------------------------------------
EOF
}
mem_install_nginx () {
cat <<-EOF
-------------Install Nginx----------
| 1) Install Nginx1.12 |
| 2) Install Nginx1.13 |
| 3) Install Nginx1.14 |
| 4) 返回上一级菜单 |
------------------------------------
EOF
}
mem_install_mysql () {
cat <<-EOF
-------------Install MySQL----------
| 1) Install MySQL5.6 |
| 2) Install MySQL5.7 |
| 3) Install MySQL7.0 |
| 4) 返回上一级菜单 |
------------------------------------
EOF
}
mem_install_php () {
cat <<-EOF
-------------Install PHP----------
| 1) Install php-fpm 7.1 |
| 2) Install php-fpm 7.2 |
| 3) Install php-fpm 7.3 |
| 4) 返回上一级菜单 |
-----------------------------------
EOF
}
while true
do
mem_option
read -p "请选择对应的选项:[ 1 | 2 | 3 | 4 ]" mem_1
case $mem_1 in
1)
clear
while true
do
mem_install_nginx
read -p "请选择你要安装nginx的版本:[ 1 | 2 | 3 | 4 ]" mem_2
case $mem_2 in
1)
echo "install nginx1.12 is done..."
;;
2)
echo "install nginx1.13 is done..."
;;
3)
echo "install nginx1.14 is done..."
;;
4)
clear
break
;;
esac
done
;;
2)
clear
while true
do
mem_install_mysql
read -p "请选择你要安装mysql的版本:[ 1 | 2 | 3 | 4 ]" mem_3
case $mem_3 in
1)
echo "install mysql5.6 is done..."
;;
2)
echo "install mysql5.7 is done..."
;;
3)
echo "install mysql7.0 is done..."
;;
4)
clear
break
;;
esac
done
;;
3)
clear
while true
do
mem_install_php
read -p "请选择你要安装php的版本:[ 1 | 2 | 3 | 4 ]" mem_4
case $mem_4 in
1)
echo "install php7.1 is done..."
;;
2)
echo "install php7.2 is done..."
;;
3)
echo "install php7.3 is done..."
;;
4)
clear
break
;;
esac
done
;;
4)
exit
;;
*)
continue
esac
done