shell 数组

1、数组定义

[root@mysql-server ~]# array=(1 2 3)  
[root@mysql-server ~]# echo ${array[*]}   #获取所有元素
1 2 3
[root@mysql-server ~]# echo ${array[@]}
1 2 3
[root@mysql-server ~]# echo ${#array[@]}  #打印数组长度
3
[root@mysql-server ~]# echo ${#array[*]}
3

[root@mysql-server ~]# echo ${array[0]} #获取对应的数组元素
1

   [root@mysql-server ~]# echo ${array[1]}
   2


[root@mysql-server ~]# cat 1.sh   #for循环遍历数组元素
#!/bin/bash
array=(1 2 3 4 5)
for item in ${array[*]}
do
echo $item
done
[root@mysql-server ~]# sh 1.sh
1
2
3
4
5

 

打印如下数组中字母数不大于6的单词

[root@mysql-server ~]# cat 1.sh 
#!/bin/bash
array=(I am martin student welcome to learning python)
for ((i=0;i<${#array[*]};i++))
do
    if [ ${#array[$i]} -lt 6 ];then
        echo "${array[$i]}"
    fi

done
[root@mysql-server ~]# sh 1.sh 
I
am
to

批量检查多个网站地址是否正常

[root@mysql-server ~]# cat 1.sh 
#!/bin/bash
check_count=0
url_list=(
http://www.qq.com
http://www.163.com
http://www.baidu.com
)
. /etc/init.d/functions
wait(){
    echo -n 'check web .....';
    for ((i=0;i<3;i++))
    do
        echo -n ".";sleep 1
    done

}

check_url(){
    wait
    for ((i=0;i<`echo ${#url_list[*]}`;i++))
    do
        wget -o /dev/null -T 3 --tries=1 --spider ${url_list[$i]} &>/dev/null
        if [ $? -eq 0 ];then
            action "${url_list[$i]}" /bin/true
        else
            action "${url_list[$i]}" /bin/false
        fi
    done
    ((check_count++))

}

main(){
    while true
    do
        check_url
        echo "-----check count:${check_count}-----"
        sleep 5
     done

}

main

判断mysql主从复制是否正常

mysql> show slave statusG;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.80.140
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 332
               Relay_Log_File: lnmp01-relay-bin.000002
                Relay_Log_Pos: 253
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 332
              Relay_Log_Space: 410
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
1 row in set (0.00 sec)



[root@lnmp01 ~]# cat 2.sh
#!/bin/bash
CheckDb(){
status=(`awk -F ':' '/_Running|_Behind/ {print $NF}' slave.log`)
for ((i=0;i<${#status[*]};i++))
do
count=0
if [ "${status[$i]}" != "Yes" -a "${status[$i]}" != "0" ];then
let count+=1
fi
done

if [ ${count} -ne 0 ];then
echo "mysql replication is failed"
else
echo "mysql replication is succeed"
fi

}
main(){
while true
do
CheckDb
sleep 3
done

}

main

[root@lnmp01 ~]# sh 2.sh
mysql replication is succeed
mysql replication is succeed
mysql replication is succeed
mysql replication is succeed

原文地址:https://www.cnblogs.com/hellojackyleon/p/9047455.html