将3个函数合并为一个

时间:2011-03-16 06:54:20

标签: linux bash shell sh

我有3个功能。如何将它们合并为一个? 我有函数3中的表名列表。函数2具有与该表对应的节点名,函数1具有每个表中的列数。 我会更喜欢一个具有所有3个输入值的函数,而使用所需的函数仅取决于函数输入传递的线索。

function_1()
{
while read tbl col_num
do
  mysql test -e"call mysql.createtable('$tbl', $col_num);"    
done << mytbl_list
test.authadv 45
test.fee 29
test.finadv 54
mytbl_list
}

# match tables names with the respective nodes

function_2()
{
while read tbl tag
do
  php -e xmlread_new.php $filename $tbl $tag 
done << heredc
test.authadv AUTHADV
test.fee FEE
test.finadv FINADV
heredc
}


# export data to excel. Change the table names as required

function_3()
{
while read tbl_name
do
  mysql --table -e"select * from $tbl_name" > $report_name.txt
done << tbl_heredoc
test.authadv
test.fee
test.finadv
tbl_heredoc
}

2 个答案:

答案 0 :(得分:0)

我无法真正看到这些功能如何相互依赖。然而,它应该是更好的风格,以更好地拥有更多和更小的功能,而不是一个大的。

关键词:关注点分离,一个工作的工具。

如果您的功能较小,则更容易推理它们。测试它们更容易。找到一个重用其中一个的地方比较容易。

答案 1 :(得分:0)

使用数组存储表名和属性。创建第四个函数来读取和存储表名等,并将其存储到这些数组中。然后其他三个函数可以访问这些数组。

read_tables ()
{
    counter=0
    while read -r tbl col_num tag
    do
        tables[counter]=$tbl
        col_nums[counter]=$col_num
        tags[counter]=$tag
        ((counter++))
    done << 'TABLES'
        test.authadv 45 AUTHADV
        test.fee     29 FEE
        test.finadv  54 FINADV
TABLES
}

create_tables ()
{
    for ((i=0; i<counter; i++))
    do
        mysql test -e"call mysql.createtable('${tables[i]}', ${col_nums[i]});"    
    done
}

# match tables names with the respective nodes

match_tags ()
{
    for ((i=0; i<counter; i++))
    do
        php -e xmlread_new.php "$filename" "${tables[i]}" "${tags[i]}"
    done
}


# export data to excel. Change the table names as required

export_excel ()
{
    for ((i=0; i<counter; i++))
    do
        mysql --table -e"select * from ${tables[i]}" > "$report_name.txt"
    done
}
相关问题