张贺,多年互联网行业工作经验,担任过网络工程师、系统集成工程师、LINUX系统运维工程师
笔者微信:zhanghe15069028807,现居济南历下区
for
基础知识
for (( i=10;i>=1;i--))
do
echo num is $i
done
[root@test2 tmp]# /bin/bash test1.sh
num is 10
num is 9
num is 8
num is 7
num is 6
num is 5
num is 4
num is 3
num is 2
num is 1
#!/usr/bin/bash
a=10
b=1
for (( a>=1;b<=10; ))
do
echo $a $b
let a--
let b++
done
[root@test2 tmp]# /bin/bash test1.sh
10 1
9 2
8 3
7 4
6 5
5 6
4 7
3 8
2 9
1 10
#!/usr/bin/bash
for (( a=1,b=9;a<10;a++,b-- ))
do
num=$(( $a + $b ))
echo num is $a + $b = $num
done
[root@test2 tmp]# /bin/bash test1.sh
num is 1 + 9 = 10
num is 2 + 8 = 10
num is 3 + 7 = 10
num is 4 + 6 = 10
num is 5 + 5 = 10
num is 6 + 4 = 10
num is 7 + 3 = 10
num is 8 + 2 = 10
num is 9 + 1 = 10
批量添加用户
#!/usr/bin/bash
for i in $(cat user.txt)
do
user=$(echo $i|awk -F ":" '{print $1}')
pass=$(echo $i|awk -F ":" '{print $2}')
echo "用户是 $user 密码是$pass"
id $user &>/dev/null
if [ $? -eq 0 ];then
echo "$user 已存在"
else
useradd $user
echo "$pass" | passwd --stdin $user &>/dev/null
fi
done
#!/usr/bin/bash
read -p "请输入你要创建的用户前缀: " user_qz
read -p "请输入你要创建的用户数量: " user_num
echo "你创建的用户是 ${user_qz}1 ~ ${user_qz}${user_num}"
read -p "你要创建的用户如下,你确定要创建吗?[ y|n ] " readly
case $readly in
y)
for i in $(seq $user_num)
do
user=${user_qz}${i}
id $user &>/dev/null
if [ $? -eq 0 ];then
echo "useradd: user $user already exists"
else
useradd $user
echo "useradd: user $user add successfully."
fi
done
;;
n)
echo "你想好了再创建......"
exit
;;
*)
echo "请不要乱输入...."
exit 1
esac
#!/usr/bin/bash
if [ ! $UID -eq 0 ] && [ ! $USER == "root" ];then
echo "无权限执行......"
exit
fi
read -p "请输入你要创建的用户前缀: " user_qz
if [ -z $user_qz ];then
echo "请输入有效的值....."
exit
fi
read -p "请输入你要创建的用户数量: " user_num
if [[ ! $user_num =~ ^[0-9]+$ ]];then
echo "请输入整数"
exit
fi
echo "你创建的用户是 ${user_qz}1 ..${user_qz}${user_num}"
read -p "你要创建的用户如下,你确定要创建吗?[ y/n ] " readly
case $readly in
y|yes|YES)
for i in $(seq $user_num)
do
user=${user_qz}${i}
id $user &>/dev/null
if [ $? -eq 0 ];then
echo "useradd: user $user already exists"
else
useradd $user
echo "useradd: user $user add successfully."
fi
done
;;
n|no|NO)
;;
*)
echo "你想好了再创建......"
;;
esac
#!/usr/bin/bash
if [ ! $UID -eq 0 ] && [ ! $USER == "root" ];then
echo "无权限执行......"
exit
fi
read -p "请输入你要创建的用户前缀: " user_qz
if [ -z $user_qz ];then
echo "请输入有效的值....."
exit
fi
read -p "请输入你要创建的用户数量: " user_num
if [[ ! $user_num =~ ^[0-9]+$ ]];then
echo "请输入整数"
exit
fi
read -p "请输入你创建用户需要的统一密码: " user_pass
echo "你创建的用户是 ${user_qz}1 ..${user_qz}${user_num}"
read -p "你要创建的用户如下,你确定要创建吗?[ y/n ] " readly
case $readly in
y|yes|YES)
for i in $(seq $user_num)
do
user=${user_qz}${i}
id $user &>/dev/null
if [ $? -eq 0 ];then
echo "useradd: user $user already exists"
else
useradd $user
echo "$user_pass" | passwd --stdin $user &>/dev/null
echo "useradd: user $user add successfully."
fi
done
;;
n|no|NO)
;;
*)
echo "你想好了再创建......"
;;
esac
#!/usr/bin/bash
if [ ! $UID -eq 0 ] && [ ! $USER == "root" ];then
echo "无权限执行......"
exit
fi
read -p "请输入你要创建的用户前缀: " user_qz
if [ -z $user_qz ];then
echo "请输入有效的值....."
exit
fi
read -p "请输入你要创建的用户数量: " user_num
if [[ ! $user_num =~ ^[0-9]+$ ]];then
echo "请输入整数"
exit
fi
echo "你创建的用户是 ${user_qz}1 ..${user_qz}${user_num}"
read -p "你要创建的用户如下,你确定要创建吗?[ y/n ] " readly
case $readly in
y|yes|YES)
for i in $(seq $user_num)
do
user=${user_qz}${i}
id $user &>/dev/null
if [ $? -eq 0 ];then
echo "useradd: user $user already exists"
else
user_pass=$(echo $((RANDOM))|md5sum |cut -c 2-10)
useradd $user
echo "$user_pass" | passwd --stdin $user &>/dev/null
echo "用户: $user 密码: $user_pass" >> /tmp/user.log
echo "useradd: user $user add successfully. cat /tmp/user.log"
fi
done
;;
n|no|NO)
;;
*)
echo "你想好了再创建......"
;;
esac
批量删除用户
#!/usr/bin/bash
if [ ! $UID -eq 0 ] && [ ! $USER == "root" ];then
echo "无权限执行......"
exit
fi
read -p "请输入你要删除的用户前缀: " del_qz
if [ -z $del_qz ];then
echo "请输入有效的值....."
exit
fi
read -p "请输入你要删除的用户数量: " user_num
if [[ ! $user_num =~ ^[0-9]+$ ]];then
echo "请输入整数"
exit
fi
echo "你删除的用户是 ${del_qz}1 ..${del_qz}${user_num}"
read -p "你要删除的用户如下,你确定吗?[ y/n ] " readly
case $readly in
y|yes|YES)
for i in $(seq $user_num)
do
user=${del_qz}${i}
id $user &>/dev/null
if [ $? -eq 0 ];then
userdel -r $user
echo "$user 用户删除成功....."
else
echo "$user 用户不存在..."
fi
done
;;
n|no|NO)
exit
;;
*)
echo "你想好了再创建......"
;;
esac
批量探测主机
#!/usr/bin/bash
#for (( 1=1;i<=254;i++))
> /tmp/ip.log
for i in {1..254}
do
{
ip=172.16.1.$i
ping -c 1 -W 1 $ip &>/dev/null
if [ $? -eq 0 ];then
echo "$ip is ok"
echo "$ip is ok" >>/tmp/ip.log
else
echo "$ip is down"
fi
} &
done
wait
echo "Sao Miao IP is Done"
echo "Test SSH Port Starting......"
IFS=$'
'
for i in $(cat /tmp/ip.log)
do
port_ip=$(echo $i |awk '{print $1}')
nmap $port_ip |grep "22" &>/dev/null
if [ $? -eq 0 ];then
echo "$port_ip 22 is ok!!"
echo "$port_ip 22 is ok!!" >> /tmp/port.log
fi
done
猜字游戏
- 随机输出一个1--100的数字
- 要求用户输入的必须是数字
- 输入的必须是数字
- 如果比随机数小则表示比随机数小了,大则提示比随机数大了
- 正确则退出 错误则继续死循环
- 最后统计猜对了多少次,错了多少次
#!/usr/bin/bash
sj=$(echo $((RANDOM % 100 + 1 )))
echo $sj
i=0
while true
do
read -p "请输入一个你需要猜的数字: " cc
if [[ ! $cc =~ ^[0-9]+$ ]];then
echo "请输入整数"
continue
fi
#将用户的输入的数字与随机数进行比较
if [ $cc -gt $sj ];then
echo "你猜的太大"
elif [ $cc -lt $sj ];then
echo "你猜的太小,继续加油"
else
echo "猜对了....."
break
fi
#进行数值自增
let i++
done
echo "你总共失败了多少次 $i"
echo "你总共猜了多少次 $(( $i + 1 ))"
跳出
当我们使用循环语句进行循环的过程当中,有时候需要在未达到循环结束条件时强制跳出循环,那么shell给我们提供了内置的方法来实现该功能:exit,break,contunue。
- exit,直接退出脚本
- break,结束当前的循环,但会执行循环之后的所有代码
- contunue,忽略本次循环剩余的所有代码,直接进行下一次循环,直到循环结束。
//exit
for i in {1..3}
do
echo 123
exit
echo 456
done
echo 789
//执行结果
[root@test2 tmp]# /bin/bash 1.sh
123
//break
for i in {1..3}
do
echo 123
break
echo 456
done
echo 789
//效果
[root@test2 tmp]# /bin/bash 1.sh
123
789
//continue
for i in {1..3}
do
echo 123
continue
echo 456
done
echo 789
//效果
[root@test2 tmp]# /bin/bash 1.sh
123
123
123
789
分库分表备份
#!/usr/bin/bash
db_name=$(mysql -uroot -p123.com -e "show databases;"|sed 1d |grep -v ".*_schema")
DB_User=root
DB_Pass=123.com
Date=$(date +%F)
for database_name in $db_name
do
#1.准备每个库的目录
DB_Path=/mysql_dump/$database_name
if [ ! -d $DB_Path ];then
mkdir -p $DB_Path
fi
#2.备份数据库
mysqldump -u$DB_User -p$DB_Pass --single-transaction
-B $database_name > $DB_Path/${database_name}_${Date}.sql
echo -e "