shell调度多进程 函数 数组参数

  Shell将函数作为小型脚本处理,可以像普通脚本那样给其传递参数。默认情况下,脚本中定义的变量都是全局变量。局部变量:local temp。

  Passing arrays to functions.The art of passing an array variable to a script function can be confusing. If you try to pass the array variable as a single parameter, it won’t work:

 1 $ cat badtest3
 2 #!/bin/bash
 3 # trying to pass an array variable
 4 function testit {
 5 echo "The parameters are: $@"
 6 thisarray=$1
 7 echo "The received array is ${thisarray[*]}"
 8 }
 9 myarray=(1 2 3 4 5)
10 echo "The original array is: ${myarray[*]}"
11 testit $myarray
12 $ ./badtest3
13 The original array is: 1 2 3 4 5
14 The parameters are: 1
15 ./badtest3: thisarray[*]: bad array subscript
16 The received array is
17 $

  If you try using the array variable as a function parameter, the function only picks up the first value of the array variable.To solve this problem, you must disassemble the array variable into its individual values, then use the values as function parameters. Inside the function, you can reassemble all of the parameters into a new array variable. Here’s an example of doing this:

 1 $ cat test10
 2 #!/bin/bash
 3 # array variable to function test
 4 function testit {
 5 local newarray
 6 newarray=(`echo "$@"`)
 7 echo "The new array value is: ${newarray[*]}"
 8 }
 9 myarray=(1 2 3 4 5)
10 echo "The original array is ${myarray[*]}"
11 testit ${myarray[*]}
12 $ ./test10
13 The original array is 1 2 3 4 5
14 The new array value is: 1 2 3 4 5
15 $

项目示例:

 1 #!/bin/sh
 2 
 3 set -x
 4 
 5 #设置数据库相关变量
 6 WORK_DIR=/home/tmn/zhaoxj
 7 SQL_DIR=${WORK_DIR}/sqldir
 8 LOG_DIR=${WORK_DIR}/logdir
 9 
10 
11 #用户名及密码
12 DB_USER=***
13 DB_PWD=***
14 DB_SID=***
15 
16 #14个地市视图 1千万条数据 多耗时35分钟
17 CITY_ARR=(V_IMEI_CELL_GPRS_CD V_IMEI_CELL_GPRS_CS V_IMEI_CELL_GPRS_CZ V_IMEI_CELL_GPRS_HH V_IMEI_CELL_GPRS_HY V_IMEI_CELL_GPRS_JS V_IMEI_CELL_GPRS_LD V_IMEI_CELL_GPRS_SY V_IMEI_CELL_GPRS_UY V_IMEI_CELL_GPRS_XT V_IMEI_CELL_GPRS_YY V_IMEI_CELL_GPRS_YZ V_IMEI_CELL_GPRS_ZJ V_IMEI_CELL_GPRS_ZZ)
18 #终端1  耗时15分钟
19 TAC_ARR=(F_TACTYPE_CELL_DY F_TACTYPE_CITY_DY F_BUSITAC_TACTYPE_DY)
20 #终端2  耗时5分钟
21 TAC_ARR2=( F_TAC_CITY_DY F_TACBRND_CITY_DY F_TEWMTYPE_CITY_DY  F_TAC_CELL_DY F_TACBRND_CELL_DY F_TEWMTYPE_CELL_DY  F_BUSITAC_TEWMTYPE_DY F_BUSITAC_TACBRND_DY)
22 #用户
23 USR_ARR=(F_TAC_USER_DY F_LOCKUSER_IMEI_DY F_HIGHALL_CITY_DY)
24 
25 #根据时间参数 获得分区名称
26 Partition=P_1D_`echo $1|awk -F- '{print $1$2$3}'`
27 Timestamp=$1
28 
29 fuction SumSql {
30 
31     local NEWARR
32     local tabname
33     
34     NEWARR=(`echo "$@"`)
35     
36     for tabname in ${NEWARR[@]}
37     do
38     {
39         LOG_FILE="${LOG_DIR}/${tabname}_`date +%y%m%d` `date +%T`.log"
40         echo "(`date +%y-%m-%d` `date +%T`) Start synchro \n" >>$LOG_FILE        
41         
42         if echo ${tabname} | grep "^V_IMEI_CELL" >/dev/null 2>&1
43         then        
44            #汇总14个地市视图
45            sqlplus -s ${DB_USER}/${DB_PWD}@${DB_SID}  @${SQL_DIR}/F_IMEI_CELL_DY.sql  ${tabname} ${Partition} ${Timestamp} >>$LOG_FILE
46         else
47            sqlplus -s ${DB_USER}/${DB_PWD}@${DB_SID}  @${SQL_DIR}/${tabname}.sql ${Timestamp} ${Partition} >>$LOG_FILE    
48         fi
49   
50       echo "(`date +%y-%m-%d` `date +%T`) Over synchro  \n" >>$LOG_FILE
51     }&
52     done
53     
54     wait    
55 }
56 
57 #汇总14个地市基础数据
58 SumSql ${CITY_ARR[@]}
59 
60 #汇总终端1
61 SumSql ${TAC_ARR[@]}
62 
63 #汇总终端2 
64 SumSql ${TAC_ARR2[@]}
65 
66 #汇总用户
67 SumSql ${USR_ARR[@]}
posted @ 2012-06-14 10:06  PoleStar  阅读(1097)  评论(0编辑  收藏  举报