专题(七)数组
一、用法
| 写法 | 说明 |
| echo ${array[*]} | 返回数组中的每个项,用空格隔开 |
| echo ${array[@]} | 返回数组中的每个项,用空格隔开 |
| (字符串) | 按照分隔符分割字符串,默认分隔符为 空格 |
|
1、array_name=(ele1 ele2 ele3 ... elen) 2、array_name=() 3、array_name=(1 2 3) array_name[3]=4 4、ages=([3]=24 [5]=19 [10]=12) |
赋值号 1、第一种声明方式声明数组,其中定义了多个元素,元素之间用空格隔开;多个元素类型可以不相同,即ele1 可以是数字类型,ele2可以是字符串类型 2、第二种声明方式定义了空数组 3、第三种定义了数组之后,可以指定下标再定义一个元素,此时数组长度变为4了 4、第四种定义数组时,直接声明了元素,其中下标为3、5、10的元素都定义了值,此时数组的长度为3 数据下标从0开始 |
| ${#array_name[@]} | 获取数组长度 |
| ${#array_name[*]} | 获取数组长度 |
| ${array_name[*]} 或 ${array_name[@]} |
获取数组中的所有元素 echo ${array_name[@]} 表示输出所有元素,每个元素之间默认用空格隔开的 |
| ${#arr[2]} | 获取 arr 数组的第 2 个元素(假设它是字符串)的长度 |
| ${array_name[index]} | 读取下标为index的元素值 |
|
unset array_name[1] |
删除下标为1的元素 |
|
unset array_name |
删除整个数据,所有元素清空 |
|
array_name+=("新元素") |
追加元素 |
|
a=(1 2 33) echo ${a[@]:1:2} # 获取a数组的第2个和第3个元素 输出为 2 33 |
截取数组 |
二、案例
1、字符串分割成数组
默认分割符是空格+换行符
(1) 将字符串先分割成数组(默认按照空格进行分割),再通过 for 循环打印数据中的每项
#默认空格将字符串分割成数组 test_string="a b c;e f g" array=($test_string) for item in ${array[*]};do echo $item done
输出结果

#默认空格将字符串分割成数组 test_string="a b c;e f g" array=($test_string) echo "输出数组" ${array[@]} for item in ${array[@]};do echo $item done

(2) 按照指定字符进行分割
old_IFS=$IFS IFS=',' test_string="a,b,c,e,f,g" array=($test_string) echo "输出数组" ${array[@]} for item in ${array[@]};do echo $item done IFS=$old_IFS
2、使用psql工具查询到数据库结果,并将返回的结果分割成数组(这里仅取数据库返回的结果放在数组中,与Java代码获取结果一致)按照换行符进行分割
使用select count(*)查询指定表的记录总数,查找到的总数量放到数组中
old_IFS=$IFS IFS=$'\n' export PGPASSWORD=$target_db_super_pwd while(((_sync_cur_time-_sync_start_time)<sync_timeout)); do _array=(`$db_tool/psql -h $target_db_ip -p $db_port -U $db_super_user -d $db_name -c "select count(*) from public.xauth_user_default where username like '"${prefix}"_%'" 2>>$log_file`) #返回的结果按照换行符分割到了数组中,其中包括表头 if [ $? -ne 0 ];then echo $(date +%F%n%T) "WARN[$prefix][batch_insert]: query data form $target_db_ip,cannot execute sql[select count(*) from public.xauth_user_default where username like '"${prefix}"_%']" >> $log_file sleep 3 _sync_cur_time=`date +%s` _sync_rs=2 continue fi echo $(date +%F%n%T) "INFO[$prefix][batch_insert]: success to query data form $target_db_ip: ${#_array[@]}">>$log_file _len=${#_array[@]} if [ $_len -gt 2 ];then _last_row=${_array[_len-2]} #拉取真实的第一条数据,去掉表头 以及 ------- if [ -z $_last_row ];then echo $(date +%F%n%T) "ERROR[$prefix][batch_insert]: cannot get count of row [select count(*) from public.xauth_user_default where username like '"${prefix}"_%'].">>$log_file _sync_rs=2 break; else _count=`echo $_last_row |sed s/[[:space:]]//g` if [ -z $_count ] || [ $_count -ne $batch_size ];then echo $(date +%F%n%T) "WARN[$prefix][batch_insert]: query data form $target_db_ip,the size is not $batch_size,now is $_count,so continue to query. " >> $log_file usleep 50000 # 延迟50毫秒 _sync_cur_time=`date +%s` continue; fi _sync_rs=0 break; fi else echo $(date +%F%n%T) "ERROR[$prefix][batch_insert]: size of response data from [select count(*) from public.xauth_user_default where username like '"${prefix}"_%'] is not correct.">>$log_file _sync_rs=2 break; fi done IFS=$old_IFS
使用select 查询指定表的数据,查找到的数据放在数组中
#超管连接数据库,直接查询指定业务数据库所有表的数据条目总数 #返回的是要一个map类型格式 declare -A table_record_count_map_from_source queryTableCountForPostgres(){ local db_ip=$1 local db_port=$2 local db_super_user=$3 local db_tool=$4 local db_name=$5 old_IFS=$IFS IFS=$'\n' _table_recore_count_array=(`$db_tool/psql -h $db_ip -p $db_port -U $db_super_user -d $db_name -c "SELECT relname,n_live_tup FROM pg_stat_user_tables where schemaname='public' ORDER BY n_live_tup DESC;"`) IFS=$old_IFS _row_id=1 echo $(date +%F%n%T) "INFO: tables for $db_name from postgres "${#_table_recore_count_array[@]}>>$log_file echo $(date +%F%n%T) INFO: ${_table_recore_count_array[*]} >>$log_file echo " " >>$log_file for (( i=2; i<=$((${#_table_recore_count_array[@]}-2)); i=i+1));do _row_data=${_table_recore_count_array[i]} echo $(date +%F%n%T) "INFO: $_row_data ">>$log_file _tablename=`echo $_row_data |awk -F '|' '{print $1}' | sed s/[[:space:]]//g` _count=`echo $_row_data |awk -F '|' '{print $2}' | sed s/[[:space:]]//g` table_record_count_map_from_source[$_tablename]=$_count done }
1、
在数组中查找特定的值
https://www.cnblogs.com/smallredness/p/13502872.html
http://c.biancheng.net/view/810.html

浙公网安备 33010602011771号