Bash 在后台运行一个函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23371083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash run a function in background
提问by Atomiklan
Have a relatively simple question here. I need to run a function in the background in bash. Normally I would do it just like so:
这里有一个相对简单的问题。我需要在 bash 的后台运行一个函数。通常我会这样做:
FUNCTION &
but things are a bit more complicated than that. I have the following line that runs the main function for each record in a text database. I cant really edit this code all that much without vastly changing the rest of the entire project, but im still open to new ideas.
但事情比这要复杂一些。我有以下行为文本数据库中的每条记录运行 main 函数。如果不对整个项目的其余部分进行大量更改,我无法真正编辑此代码,但我仍然对新想法持开放态度。
cat databases/$WAN | grep -v \# | while read LINE; do MAIN; done
I want to spawn a new terminal in background for each record to do a sort of parallel type processing, making things go much faster. Main takes a minute to process for each record. This however does not work.
我想在后台为每条记录生成一个新终端,以进行某种并行类型处理,使事情进展得更快。Main 需要一分钟来处理每条记录。然而这不起作用。
cat databases/$WAN | grep -v \# | while read LINE; do MAIN &; done
Any suggestions?
有什么建议?
* UPDATE *
* 更新 *
Thanks for all the responses. Let me see if I can answer some of those questions.
感谢所有的回应。让我看看我是否能回答其中的一些问题。
gniourf_gniourf - Yes I know using cat like this is wrong. This was early on, and critical code, so I have not updated it yet. I now read into the while loop for most things I do. I will fix it eventually. You may be right about syntax. When I break it up like so, things seem to work now:
gniourf_gniourf - 是的,我知道像这样使用 cat 是错误的。这是早期的关键代码,所以我还没有更新它。我现在阅读了我所做的大多数事情的 while 循环。我最终会修复它。您可能对语法是正确的。当我像这样分解它时,事情现在似乎起作用了:
cat databases/$WAN | grep -v \# | while read LINE
do
MAIN & > /dev/null 2>&1
done
So that fixes the background problem. I wonder what was messed up in my single line syntax. Thanks
这样就解决了背景问题。我想知道我的单行语法有什么问题。谢谢
chepner - I don't believe LINE is a variable. I could be wrong though. Some things about Bash still confuse me. Maybe it is and is a variable that the entire record from the database gets stored to prior to processing.
chepner - 我不相信 LINE 是一个变量。不过我可能是错的。关于 Bash 的一些事情仍然让我感到困惑。也许它是一个变量,在处理之前将数据库中的整个记录存储到该变量中。
Bruce K - Waiting is exactly what I was trying to avoid. If I let it run in the same terminal one at a time, it will slowly process each record in order. If I push each record to a seperate terminal for processing, all records will be processed simultaneously (at least in our eyes). The additional overhead is intentional in order to speed up how quickly the loop through the database occurs.
Bruce K - 等待正是我想要避免的。如果我让它一次一个地运行在同一个终端中,它会按顺序慢慢处理每条记录。如果我将每条记录推送到单独的终端进行处理,则所有记录都将同时处理(至少在我们看来)。额外的开销是有意的,目的是加快通过数据库的循环发生的速度。
Radix - Yes you're right. I'll read up on that. Thanks for the link.
基数 - 是的,你是对的。我会仔细阅读。谢谢你的链接。
回答by aqn
This worked for me:
这对我有用:
$ function testt(){ echo "lineee is <$lineee>";}
$ grep 5432 /etc/services|while read lineee;do testt&done
lineee is <postgres 5432/udp # POSTGRES>
lineee is <postgres 5432/tcp # POSTGRES>
If, for some reason, your MAIN function is not seeing a LINE variable, you can try:
如果由于某种原因,您的 MAIN 函数没有看到 LINE 变量,您可以尝试:
"export" the LINE variable beforehand:
预先“导出” LINE 变量:
$ export LINE
$ # do your thing
Or, pass the line read as an argument to the function:
或者,将读取的行作为参数传递给函数:
$ function testt(){ LINE=""; echo "LINE is <$LINE>";}
$ grep 5432 /etc/services|while read LINE;do testt "$LINE"&done