如何从awk调用用户定义的函数? [英] How to call a user defined function from awk?

查看:86
本文介绍了如何从awk调用用户定义的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接到数据库并运行查询,这是我的脚本

I am trying to connect to a db and run a query this is my script

#!/bin/bash
awk '
/----FINAL----/ { f=1 }                     
f=="" || /^--.*--$/ { print; next }   
f { b=b (b==""?"":ORS) $0               
    if(b~/;$/) {                        
        result=dbconnect($b) 
        print result                       #dosomething
        b=""                           
    }
}' sql1.sql

function dbconnect(b)
{
/opt/lampp/bin/mysql -u root myncr <<QUERY_INPUT
$b
QUERY_INPUT

if [ "$?" -eq 0 ];
 then
    return true;
else
    return false;
fi
}

它显示的错误是dbconnect从未定义;我试图在awk中定义函数,但是它不起作用.

The error it shows is dbconnect never defined; I have tried to define the function inside awk but it doesn't work.

我的SQL文件如下:

---select command------
select * from mytes;
select * from mytest;
select * from mytest;

---insert command------
--INSERT INTO `mytest`(`qlid`, `name`, `email`) VALUES ('nj20302','nancy','nancy@gmail.com');

--update---
UPDATE `mytest` 
SET `name`='james' 
WHERE qlid='jm20322';

----FINAL----
;
select * from mytest;
select * from mytest;

---select command------
---select command------
select * from mytes;
select * from mytest;
select * from mytest;
select * from  mytest;

UPDATE `mytest` 
SET `name`='wendy' 
WHERE qlid='wp50322';

我不想使用system("./dbconnect.sh " $b)

推荐答案

恐怕melpomene正确,您不能从awk程序调用bash函数(当然,不能使用任何会改变的参数).

I'm afraid melpomene has it correct, you cannot call bash functions from the awk program (well, not with a parameter that changes anyway).

但是,这是一个小示例程序,显示了不同的策略:

However, here's a little example program that shows a different strategy:

function dbfunction() { 
  echo Processing "'$1'" 
}

awk ' {print $0} ' | while
  read dbcommand  
do
  dbfunction "$dbcommand"
done

因此,一般的想法是awk输出的每一行都在下面的while循环中执行.这样可以避免系统调用,并保持awk程序的性能.您可以使用awk脚本进行文本处理,也可以使用bash进行SQL处理.

So the general idea is that every line of the awk output is executed in the following while loop. This avoids the system call and keeps your awk program performant. You do your text processing in the awk script and you do your SQL stuff from bash.

当然,我为您编写的循环存在问题.首先,如果一个命令不止一行,那么它将无法正常工作(因为read命令读到该行的末尾).

Of course, there are problems with the loop I wrote for you. First of all, if a command is more than one line things won't work so well ( since the read command reads up to the end of the line ).

我已经测试"了一下.这是您输入的示例运行.记住,您需要更改输入,以便所有SQL语句都显示在一行上:

I've "tested" a little. Here is a sample run with your inputs. Remember, you'll need to change your input so all SQL statements appear on a single line:

function dbfunction() { 
  echo Executing "'$1'"
}

awk '
/----FINAL----/ { f=1 }                     
f=="" || /^--.*--$/ {  next }   
f { b=b (b==""?"":ORS) $0               
    if(b~/;$/) {                        
        print b  # do something
        b=""                           
    }
}' sql1.sql | while 
  read sql
do
  dbfunction "$sql"
done

这是执行:

Executing ';'
Executing 'select *from mytest;'
Executing 'select *from'
Executing 'mytest;'
Executing 'select *from mytes;'
Executing 'select *from mytest;'
Executing 'select *from mytest;'
Executing 'select *from'
Executing 'mytest;'
Executing 'UPDATE `mytest` SET `name`='wendy''
Executing 'WHERE qlid='wp50322';'

这篇关于如何从awk调用用户定义的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆