shell調度多進程 函數 數組參數

來源:互聯網
上載者:User

  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 $myarray12 $ ./badtest313 The original array is: 1 2 3 4 514 The parameters are: 115 ./badtest3: thisarray[*]: bad array subscript16 The received array is17 $

  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 $ ./test1013 The original array is 1 2 3 4 514 The new array value is: 1 2 3 4 515 $

項目樣本:

 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=$128 29 fuction SumSql {30 31     local NEWARR32     local tabname33     34     NEWARR=(`echo "$@"`)35     36     for tabname in ${NEWARR[@]}37     do38     {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>&143         then        44            #匯總14個地市視圖45            sqlplus -s ${DB_USER}/${DB_PWD}@${DB_SID}  @${SQL_DIR}/F_IMEI_CELL_DY.sql  ${tabname} ${Partition} ${Timestamp} >>$LOG_FILE46         else47            sqlplus -s ${DB_USER}/${DB_PWD}@${DB_SID}  @${SQL_DIR}/${tabname}.sql ${Timestamp} ${Partition} >>$LOG_FILE    48         fi49   50       echo "(`date +%y-%m-%d` `date +%T`) Over synchro  \n" >>$LOG_FILE51     }&52     done53     54     wait    55 }56 57 #匯總14個地市基礎資料58 SumSql ${CITY_ARR[@]}59 60 #匯總終端161 SumSql ${TAC_ARR[@]}62 63 #匯總終端2 64 SumSql ${TAC_ARR2[@]}65 66 #匯總使用者67 SumSql ${USR_ARR[@]}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.