for,while
for循环
for NUM in 1 2 3
for NUM in {1..3}
for NUM in `seq 1 3`
for NUM in {a..f}
for NUM in {1..$a}
for ((A=1;A<=10;A++))
for NUM in `seq 1 2 10`
查看网段里面连通的主机
#!/bin/bash
for NUM in {0..254}
do
ping -c1 -w1 172.25.254.$NUM &> /dev/null &&{
echo -e "172.25.254.$NUM"
} || {
echo ""
}
done
~
~
备份数据库linux,mysql,test,,westos,并且放在/mnt/maysql_backup里面
#!/bin/bash
DATABASE_MESSAGE=`mysql -uroot -EN -e "show databases;" | grep -E "^\*|schema$" -v`
mkdir -p /mnt/mysql_dump
for DATABASE_NAME in $DATABASE_MESSAGE
do
mysqldump -uroot $DATABASE_NAME > /mnt/mysql_dump/${DATABASE_NAME}.sql
[ "$?" -eq "0" ] && {
echo -e "\033[47m$DATABASE_NAME is backuped \033[0m"
}
done
while循环
格式
while 条件
do
done
实验1
使用whiel时实刷新uptime
[root@desktop ~]# vim mysql.sh
[root@desktop ~]# vim uptime.sh
[root@desktop ~]# sh uptime.sh
^C:37:53 up 2:40, 2 users, load average: 0.01, 0.04, 0.0505
[root@desktop ~]# cat uptime.sh
#!/bin/bash
while true
do
echo -n `uptime`
echo -ne "\r \r"
sleep 1
done
如何用非交互的方式传送邮件
[root@localhost ~]# echo hello world | mail -s "test" root 非交互式 -s表示主题是test
You have mail in /var/spool/mail/root
[root@localhost ~]# mail 直接查看
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/spool/mail/root": 2 messages 1 new
1 root Thu Jun 21 13:31 21/768 "test"
>N 2 root Thu Jun 21 13:31 20/757 "test"
&
Message 2:
From root@localhost.localname Thu Jun 21 13:31:52 2018
Return-Path: <root@localhost.localname>
From: root <root@localhost.localname>
Date: Thu, 21 Jun 2018 13:31:52 -0400
To: root@localhost.localname
Subject: test
User-Agent: Heirloom mailx 12.5 7/5/10
Content-Type: text/plain; charset=us-ascii
Status: R
hello world 邮件内容
根分区的使用量达到了80%就给root用户法邮件报警
[root@desktop ~]# vim warn.sh
[root@desktop ~]# cat warn.sh
#!/bin/bash
DISK_NUM=`df | awk '/\/$/{print $5}' | awk -F "%" '{print $1}'`
while true
do
[ "$DISK_NUM" -ge "80" ] && {
echo "Your / will full !!! " | mail -s "warning" root
}
sleep 1
done