• 3、My Scripts


    1、用for循环批量修改文件扩展名(P240)
    2、使用专业改名命令rename来实现
    3、通过脚本实现sshd、rsyslog、crond、network、sysstat服务在开机时自动启动(P244)  
    4、打印99乘法表(P246)
    5、用for循环打印1到100的和(P247)
    6、批量创建10个系统账号,并设置密码(P255)
    7、Linux系统产生随机数的6中方法(P257)
    8、select循环(P259)
    9break、continue、return、exit脚本演练
    10、开发shell脚本实现为服务配置多个IP(P271)

    11、利用RANDOM生成随机一段文字并利用md5sum加密保存到一个文件中,根据指定字符串(1ea2edc6fe31)找出破解其加密前的数字(P275)
    12、通过C语言型的for循环语句打印数组元素(P283)
    13、利用for循环语句打印字母数大于等于4的单词(P286)
    14、批量检查多个网址是否正常(P287)
    15、检查MySQL主从复制是否正常(P291)

                


    1、用for循环批量修改文件扩展名(P240)


    2、使用专业改名命令rename来实现

    3、通过脚本实现sshd、rsyslog、crond、network、sysstat服务在开机时自动启动(P244)            
    使用循环
    #for i in `chkconfig --list | grep 3:on | awk '{print $1}'`; do chkconfig --level 3 $i off; done
    通过bash命令
    chkconfig --list | grep 3:off | grep -vE "iscsi|iscsid|multipathor|netconsole|netfs|rdisc|restorecond|saslauthd" | awk '{print " chkconfig " $1 " on "}' | bash

    4、打印99乘法表(P246)

     1 #!/bin/bash
     2 #
     3 for num1 in `seq 9`; do
     4   for num2 in `seq 9`; do
     5     if [ $num1 -ge $num2 ]; then
     6       if (((num1*num2)>9)); then       //如果两个乘数相乘大于9,这是控制输出格式
     7         echo -en "${num1}x${num2}=$((num1*num2)) "    //结尾多一个空格
     8       else
     9         echo -en "${num1}x${num2}=$((num1*num2))  "   //结尾多两个空格
    10       fi
    11     fi
    12   done
    13 
    14 echo " "
    15 done
    [root@C ~]# bash 99.sh 
    1x1=1 
    2x1=2 2x2=4 
    3x1=3 3x2=6  3x3=9 
    4x1=4 4x2=8  4x3=12 4x4=16 
    5x1=5 5x2=10 5x3=15 5x4=20 5x5=25 
    6x1=6 6x2=12 6x3=18 6x4=24 6x5=30 6x6=36 
    7x1=7 7x2=14 7x3=21 7x4=28 7x5=35 7x6=42 7x7=49 
    8x1=8 8x2=16 8x3=24 8x4=32 8x5=40 8x6=48 8x7=56 8x8=64 
    9x1=9 9x2=18 9x3=27 9x4=36 9x5=45 9x6=54 9x7=63 9x8=72 9x9=81
    View Code

    5、用for循环打印1到100的和(P247)

    1 #!/bin/bash
    2 #
    3 i=0
    4 while ((i<=100)); do
    5   ((sum=sum+i))
    6   ((i++)) 
    7 done
    8 
    9 echo $sum
    方法1
    1 #!/bin/bash
    2 #
    3 for ((i=1;i<=100;i++)); do
    4   ((sum=sum+i))
    5 done
    6 
    7 echo $sum
    方法2

     # echo $(( (100+1) * 100/2))

    6、批量创建10个系统账号,并设置密码(P255)

     1 #!/bin/bash
     2 #
     3 . /etc/init.d/functions
     4 user="cos"
     5 passfile="/tmp/user.log"
     6 
     7 echo ------this is my script for adding users------
     8 for num in `seq -w 10`; do
     9   useradd $user$num
    10   pass="`echo "cos$RANDOM" | md5sum | cut -c 5-12`"
    11 
    12   echo -e "$user${num}:$pass" >>$passfile
    13 
    14   if [ $? -eq 0 ]; then
    15     action "Add $user$num is ok." /bin/true
    16   else
    17     action "Add $user$num is fail." /bin/true
    18   fi  
    19 done
    20 
    21 echo "----------Add Users----------"
    22 chpasswd < $passfile &>/dev/null   //读取密码文件进行密码设置
    23 cat $passfile
    批量添加密码1
     1 #!/bin/bash
     2 #
     3 . /etc/init.d/functions
     4 user="cos"
     5 passfile="/tmp/user.log"
     6 
     7 echo ------this is my script for adding users------
     8 for num in `seq -w 10`; do
     9   useradd $user$num
    10   pass="`echo "cos$RANDOM" | md5sum | cut -c 5-12`"
    11 
    12   echo "$pass" | passwd --stdin $user$num &>/dev/null && 
    13   echo -e "user:$user$num	passwd:$pass" >>$passfile
    14 
    15   if [ $? -eq 0 ]; then
    16     action "Add $user$num is ok." /bin/true
    17   else
    18     action "Add $user$num is fail." /bin/true
    19   fi  
    20 done
    21 
    22 echo "----------Add Users----------"
    23 cat $passfile
    批量添加用户2
    #!/bin/bash
    #
    user="cos"
    passfile="/tmp/user.log"
    
    for num in `seq -w 10`; do
      id $user$num &>/dev/null
      if [ $? -eq 0 ]; then
        userdel -r $user$num
        echo "$user$num was delete."
      else
        echo "$user$num is not exist."
      fi  
    done
    
    cat /dev/null >$passfile 
    批量删除用户

    7、Linux系统产生随机数的6中方法(P257)

    1、通过系统环境变量($RANDOM)实现
    
    # echo "hello world$RANDOM" | md5sum | cut -c 5-13
    66f32895e
    1
    2、通过openssl产生随机数   数字和大小写字符、特殊字符相结合
    
    # openssl rand -base64 80
    eTlRKlp2r6v/cMt4C3lkNpSU1i/NV8oCnzvMA35zyXLLzQObS3jpjeh5w3doNINk
    MGl0gWXji4vupUDyIOUIGV1te25vn+EcJxEjKTt8wqg=
    2
    4、通过/dev/urandom配合chksum生成随机数
    
    # head /dev/urandom | cksum
    1284149441 4169
    3
    5、通过UUID产生随机数
    
    # cat /proc/sys/kernel/random/uuid
    4b3d6a51-990b-4993-b130-cc9dae5df37f
    4
    3、通过时间(date)产生随机数
    # date +%s%N
    1533976483650549026
    5
    6、使用expect附带的mkpasswd生成随机数
    
    # yum install expect -y   //安装expect
    
    # mkpasswd -l 9 -d 2 -c 3 -C 3 -s 1 | md5sum | cut -c 3-12
    
    221d1f369b
    
    相关参数说明:
    
    -l:指定密码长度
    
    -d:指定密码中数字的数量
    
    -c:指定密码中小写字母的数量
    
    -C:指定密码中大写字母的数量
    
    -s:指定密码中特殊字符的数量
    6 

    8、select循环(P259)

     1 #!/bin/bash
     2 #
     3 select name in xiaohong xiaoming xiaoqiang; do
     4   echo $name
     5 done
     6
    # bash 3.sh 
    1) xiaohong
    2) xiaoming
    3) xiaoqiang
    #? 1
    xiaohong
    #? 2
    xiaoming
    #? 3
    xiaoqiang

    采用数组做变量列表

    1 #!/bin/bash
    2 #
    3 array=(hong ming qiang)
    4 
    5 select name in "${array[@]}"; do
    6   echo $name
    7 done
    View Code

    把命令结果作为变量列表

    1 #!/bin/bash
    2 #
    3 
    4 select name in `ls /etc`; do
    5   echo $name
    6 done
    View Code

    调整select循环菜单默提示符即利用select变量打印数字序号

     1 #!/bin/bash
     2 #
     3 
     4 PS3="Please select a num from menu:"   //PS3是控制select循环的提示符
     5 select name in hong qiang ming; do
     6   echo -e "You choice is:
     $REPLY) $name"  //REPLY是菜单项对应的数字,即用户输入的数字
     7 done
     8 
     9 # bash 1.sh 
    10 1) hong
    11 2) qiang
    12 3) ming
    13 Please select a num from menu:1
    14 You choice is:
    15  1) hong
    16 Please select a num from menu:2
    17 You choice is:
    18  2) qiang
    View Code 

    9、breakcontinuereturn、exit脚本演练(P269)

     1 #!/bin/bash
     2 #
     3 
     4 if [ $# -ne 1 ]; then
     5   echo "Usage:$0 {break|continue|exit|return}"
     6   exit 1
     7 fi
     8 
     9 test() {
    10   for ((i=0;i<=5;i++)); do
    11     if [ $i -eq 3 ]; then
    12       $*;          //如果$i=3,这里就接受函数外的参数,即test $*
    13     fi  
    14     echo $i
    15   done
    16   echo "I am in func."
    17 }
    18 
    19 test $*
    20 func_ret=$?
    21 if [ `echo $* | grep return | wc -l` -eq 1 ]; then
    22   echo "return is exit status:$func_ret"
    23 fi
    24 
    25 echo "ok"
    View Code

    continue是终止当前循环执行下一次循环,break是跳出当前循环(if循环),return是不但终止当前循环还跳出当前函数,exit是直接推出脚本

    10、开发shell脚本实现为服务配置多个IP(P271)

     1 #!/bin/bash
     2 #
     3 
     4 [ -f /etc/init.d/functions ] && . /etc/init.d/functions
     5 op() {
     6   if [ $1 == "del" ]; then
     7     list=`echo {157..147}`
     8   else
     9     list=`echo {147..157}`
    10   fi  
    11 
    12   for ip in $list; do
    13     if [ $ip -eq 150 ]; then
    14       continue
    15     fi  
    16     ip addr $1 192.168.184.$ip/24 dev eth0 label eth0:$ip &>/dev/null
    17     if [ $? -eq 0 ]; then
    18       action "$1 $ip" /bin/true
    19     else
    20       action "$1 $ip" /bin/false
    21     fi  
    22   done
    23 }
    24 
    25 case "$1" in
    26   add)
    27     op add ;;
    28   del)
    29     op del ;;
    30   readd)
    31     op del 
    32     echo "Ip was deleted"
    33     sleep 3
    34     echo "Ip will add"
    35     op add ;;
    36   *)
    37     echo "Usage:$0 {add|del|readd}"
    38 esac
    39                      
    View Code

    使用ifconfig配置别名IP

    #ifconfig eth0:0 192.168.184.147/24 up     添加IP

    #ifconfig eth0:0 192.168.184.147/24 down   删除IP

    使用IP命令配置ip

    #ip addr add 192.168.184.147/24 dev eth0 label eth0:147   添加IP

    #ip addr del 192.168.184.147/24 dev eth0 label eth0:147   删除IP

    11、利用RANDOM生成随机一段文字并利用md5sum加密保存到一个文件中,根据指定字符串(1ea2edc6fe31)找出破解其加密前的数字(P275)

     1 #!/bin/bash
     2 #
     3  
     4 #for n in {0..32767}; do   //生成随机数,并保存到文件里面
     5 #  echo "`echo $n | md5sum` $n" >>/tmp/1.log
     6 #done
     7 
     8 md5char="1ea2edc6fe31"    定义要查找的字符串
     9 #cat /tmp/1.log | while read line; do
    10 while read line; do
    11   if [ `echo $line | grep "$md5char" | wc -l` -eq 1 ]; then
    12     echo $line
    13     break
    14   fi  
    15 done </tmp/1.log
    View Code

    RANDOM随机数范围再0~32767   

    读取文件中的每一行参考:https://www.cnblogs.com/demonxian3/p/7886177.html

    数组

    12、通过C语言型的for循环语句打印数组元素(P283)

     1 #!/bin/bash
     2 #
     3 array=(1 2 3 4 5 6)
     4 for ((i=0;i<${#array[*]};i++)); do
     5 echo ${array[i]}
     6 done
     7 
     8 #!/bin/bash
     9 #
    10 array=(1 2 3 4 5 6)
    11 for i in ${array[*]}; do
    12   echo $i
    13 done
    View
     1 #!/bin/bash
     2 #
     3 array=(
     4   192.168.184.145
     5   192.168.184.146
     6   192.168.184.147
     7 )
     8 
     9 #for ((i=0;i<${#array[@]};i++)); do   //C语言for循环语法
    10 #  echo "${array[i]}"
    11 #done
    12 
    13 for n in ${array[*]}; do        //普通for循环语法
    14   echo $n
    15 done
    View Code

    静态数组:array=(1 2 3) 动态数组:array=($(ls))

    打印所有数组:${array[*]}  ${array[@]}

    打印数组长度:${#array[*]} ${#array[@]}

     1 #!/bin/bash
     2 #
     3 array=(
     4   192.168.184.145
     5   192.168.184.146
     6   192.168.184.147
     7 )
     8 
     9 #for ((i=0;i<${#array[@]};i++)); do
    10 #  echo "${array[i]}"
    11 #done
    12 
    13 for n in ${array[*]}; do
    14   echo $n
    15 done
    View Code

    13、利用for循环语句打印字母数大于等于4的单词(P286)

     1 #!/bin/bash
     2 #
     3 
     4 array=(I am exercising bash scripts I like it very much)
     5 
     6 #for ((i=0;i<${#array[*]};i++)); do
     7 #  if [ ${#array[$i]} -ge 4 ]; then
     8 #    echo "${array[$i]}"
     9 #  fi
    10 #done
    11 
    12 for word in ${array[*]}; do
    13   if [ `expr length $word` -ge 4 ]; then
    14     echo $word
    15   fi
    16 done
    View Code

    14、批量检查多个网址是否正常(P287)

     1 #!/bin/bash
     2 #
     3 . /etc/init.d/functions
     4 
     5 check_count=0
     6 url_list=(
     7 http://www.baidu.com
     8 https://i.cnblogs.com
     9 https://hao.360.cn
    10 http://www.ninini
    11 https://www.sina.com.cn
    12 )
    13 
    14 wait() {
    15   echo -n '3秒后,执行检查URL操作.';
    16   for ((i=0;i<3;i++)); do
    17     echo -n ".";sleep 1
    18   done
    19   echo
    20 }
    21 
    22 check_url() {
    23   wait
    24   for ((i=0;i<${#url_list[*]};i++)); do
    25     wget -T 3 --tries=2 --spider ${url_list[$i]} &>/dev/null
    26     if [ $? -eq 0 ]; then
    27       action "${url_list[$i]}" /bin/true
    28     else
    29       action "${url_list[$i]}" /bin/false
    30     fi  
    31   done
    32   ((check_count++))
    33 }
    34 
    35 main() {
    36   while true; do
    37     check_url
    38     echo "----------check count:${check_count}-----------"
    39     sleep 3
    40   done
    41 }
    42 
    43 main
    View Code

    15、检查MySQL主从复制是否正常(P291)

    slave.log

    Slave_IO_Running:No
    Slave_SQL_Running:No
    Seconds_Behind_Master:0

     1 #!/bin/bash
     2 #
     3 
     4 status=($(awk -F ':' '/_Running|_Behind/{print $NF}' slave.log))
     5 
     6 for ((i=0;i<${#status[*]};i++)); do
     7   count=0
     8   if [ "${status[${i}]}" != "yes" -a "${status[${i}]}" != 0 ]; then
     9     let count+=1
    10   fi  
    11 done
    12 
    13 if [ $count -ne 0 ]; then
    14   echo "mysql replcation is failed"
    15 else
    16   echo "mysql replcation is success"
    17 fi
    View Code
  • 相关阅读:
    【转】Android Hook框架Xposed详解
    【转】不需要 Root,也能用上强大的 Xposed 框架:VirtualXposed
    【转】手把手教你读取Android版微信和手Q的聊天记录(仅作技术研究学习)
    【转】关于音频焦点的理解
    test
    Linux只下载不安装软件包
    Linux用户创建/磁盘挂载相关命令
    telnet的安装配置及xinetd的讨论
    vsftp安装配置教程
    wordpress安装教程
  • 原文地址:https://www.cnblogs.com/hanshanxiaoheshang/p/9416698.html
Copyright © 2020-2023  润新知