【问题标题】:pass the while loop record to function [duplicate]将while循环记录传递给函数[重复]
【发布时间】:2020-05-15 05:03:42
【问题描述】:

我如何将参数传递给 linux 中的函数。 我在一个文件中逐行reding并在后台运行esch记录

while read -r record
    do
        reccount=$(( reccount + 1 ))
        #function call  
        proceed_tasks_function(record) &
done

到函数

proceed_tasks_function(/*take the record from function call*/)
{
### parse input record

          contact_email=`echo "$record" | cut -f5 -d ''`
              echo "contact email is $contact_email" 
          credit_card_id=`echo "$record" | cut -f6 -d ''`
              echo "credit card id is $credit_card_id"
          ref_nr=`echo "$record" | cut -f7 -d ''`
              echo "reference nr is $ref_nr"
          cny_cd=`echo "$record" | cut -f8 -d ''`
              echo "country code is $cny_cd"
          lang=`echo "$record" | cut -f9 -d ''`
              echo "language is $lang"
          pmt_ir=`echo "$record" | cut -f13 -d ''`
              echo "payment ir is $pmt_ir"
}

【问题讨论】:

    标签: linux bash shell sh ksh


    【解决方案1】:

    shell 中的函数与任何其他命令一样:参数仅列在命令之后,不需要或不允许使用括号。

    while read -r record
        do
            reccount=$(( reccount + 1 ))
            proceed_tasks_function "$record" &
    done
    

    【讨论】:

    • 在函数内部,您可以使用 $1、$2、$* 引用传递的参数,语法与脚本参数相同
    猜你喜欢
    • 2019-11-29
    • 2015-11-10
    • 2018-07-20
    • 2014-01-17
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    • 2013-10-25
    • 2019-08-13
    相关资源
    最近更新 更多