linux - 将 3 个功能合并为一个

标签 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
}

最佳答案

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

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/

相关文章:

python - 难以通过 python 使用 qhull 管道

c - 从 GTK 条目中获取文本

linux - ESP8266 Linux : Programming and Flashing

regex - grep/pcregrep/sed/awk 最后一次匹配后到文件末尾的数据

linux - unix shell 中 echo 和 @echo 的区别

python - Python 3.5.1 Shell 中的简单 http 服务器

regex - 在没有 Bashisms 的情况下检查 Shell 中的子字符串

linux - 在一个命令中更改目录并执行文件

linux - bash:用于踢除 localhost 之外的所有 ssh 条目的脚本

ios - 命令/bin/sh 发出的错误