• fort循环


    张贺,多年互联网行业工作经验,担任过网络工程师、系统集成工程师、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. 随机输出一个1--100的数字
    2. 要求用户输入的必须是数字
    3. 输入的必须是数字
    4. 如果比随机数小则表示比随机数小了,大则提示比随机数大了
    5. 正确则退出 错误则继续死循环
    6. 最后统计猜对了多少次,错了多少次
    #!/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 "33[31m $database_name 数据库 已经备份完成..... 33[0m"
    	
    	#3.备份数据库的每一个表
    	tb_name=$(mysql -u$DB_User -p$DB_Pass -e "use ${database_name};show tables;"|sed 1d)
    	
    	#4.批量的备份数据库的表
    	for table_name in $tb_name
    	do
    		#5.备份数据库的表数据
    		mysqldump -u$DB_User -p$DB_Pass --single-transaction 
    		$database_name $table_name > $DB_Path/${database_name}_${table_name}_tables_${Date}.sql
    		echo -e "33[32m 备份$database_name 的数据库中的 $table_name 数据表,备份成功 33[0m"
    	done
    done
    

    主从状态监测

    #!/usr/bin/bash
    IO_Status=$(mysql -uroot -p123.com -e "show slave statusG"|awk '/Slave_IO_Running/ {print $2}')
    SQL_Status=$(mysql -uroot -p123.com -e "show slave statusG"|awk '/Slave_SQL_Running/ {print $2}')
    
    
    slave_sql_error_message(){
    	mysql -uroot -p123.com -e "show slave statusG"|grep "Last_SQL" > /tmp/sql_err.log
    	mail -s "MySQL Master SLave SQL Error $(date +%F)" 552408925@qq.com < /tmp/sql_err.log
    	echo "邮件通知成功......"
    }
    
    
    slave_io_error_message(){
    	mysql -uroot -p123.com -e "show slave statusG"|grep "Last_IO" > /tmp/io_err.log
    	mail -s "MySQL Master SLave IO Error $(date +%F)" 552408925@qq.com < /tmp/io_err.log
    	echo "邮件通知成功......"
    }
    
    
    
    if [ $IO_Status == "Yes" ] && [ $SQL_Status == "Yes" ];then
    	echo "MySQL主从正常"
    else
    	#1.判断IO异常
    	if [ ! $IO_Status == "Yes" ];then
    		slave_io_error_message
    		exit 
    	fi
    
    	#2.判断SQL异常
    	if [ ! $SQL_Status == "Yes" ];then
    		SQL_Err=$(mysql -uroot -p123.com -e "show slave statusG"|awk '/Last_SQL_Errno/ {print $2}')
    		#3.精细化判断主从不同步的问题
    		case $SQL_Err in 
    			1007)
    				echo "主从的SQL出现问题,尝试使用set global sql_slave_skip_counter=1; 跳过错误"
    				sleep 2
    				mysql -uroot -p123.com -e "stop slave;set global sql_slave_skip_counter=1;start slave;"
    				SQL_Err_1=$(mysql -uroot -p123.com -e "show slave statusG"|awk '/Last_SQL_Errno/ {print $2}')
    				if [ $SQL_Err_1 -eq 0 ];then
    					echo "尝试跳过了一次,恢复MySQL数据库成功"
    				else
    					slave_sql_error_message
    				fi
    				;;
    			1032)
    					slave_sql_error_message
    				;;
    			*)
    					slave_sql_error_message
    		esac
    	fi
    fi
    
    
    
  • 相关阅读:
    Jenkins+Ansible+Gitlab自动化部署三剑客(四)--Jenkins Linux shell集成
    Jenkins+Ansible+Gitlab自动化部署三剑客(三)--Jenkins
    腾讯云从业者线上课程(一)--云计算技术架构
    腾讯云从业者线上课程(一)--云计算发展历史
    nmcli 静态方式添加IP地址
    nmcli
    systemctl 控制单元
    ansible组件 Ad-Hoc
    ansible 定义主机用户和密码
    ansible 主机清单 /etc/ansible/hosts
  • 原文地址:https://www.cnblogs.com/yizhangheka/p/12562237.html
Copyright © 2020-2023  润新知