我有 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
}
最佳答案
使用数组来存储您的表名和属性。创建第四个函数来读取表名等并将其存储到这些数组中。然后其他三个函数可以访问这些数组。
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
}
关于linux - 将 3 个功能合并为一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5321951/