Array parameters of shell multi-process function Scheduling

Source: Internet
Author: User

Shell processes a function as a small script and can pass parameters to it like a normal script. By default, all variables defined in the script are global variables. Local variable: Local temp.

Passing arrays to funeter. 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 specified ed 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 specified ed 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 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 $

Project Example:

 1 #! /Bin/ Sh  2  3 Set- X  4   5   # Setting database variables  6 Work_dir =/home/TMN/ Zhaoxj  7 SQL _dir =$ {work_dir }/ Sqldir  8 Log_dir =$ {work_dir }/ Logdir  9   10   11   # Username and password 12 Db_user = *** 13 Db_pwd = *** 14 Db_sid = *** 15   16   # It takes 35 minutes to view more than 10 million data entries in 14 prefecture views  17 City_arr = (Please export your own v_imei_cell_upls_js v_imei_cell_gls_ld v_imei_cell_cells_sy ~v_imei_cell_gls_xt v_imei_cell_cell_gls_yy)  18   # Terminal 1 takes 15 minutes  19 Tac_arr = (F_tactype_cell_dy f_tactype_city_dy f_busitac_tactype_dy)  20   # Terminal 2 takes 5 minutes  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 1_f_busitac_tacbrnd_dy)  22   # User  23 Usr_arr = (F_tac_user_dy f_lockuser_imei_dy f_highall_city_dy)  24   25   # Obtain the partition name based on the Time Parameter 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   # Summarize 14 prefecture views  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   # Collect basic data of 14 cities  58   Sumsql $ {city_arr [@]}  59   60   # Summary Terminal 1  61   Sumsql $ {tac_arr [@]}  62  63   # Terminal 2  64   Sumsql ${tac_arr2 [@]}  65   66   # Summarize users  67 Sumsql $ {usr_arr [@]}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.