首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从后台进程linux shell脚本中获得结果?

如何从后台进程linux shell脚本中获得结果?
EN

Stack Overflow用户
提问于 2013-08-08 23:58:23
回答 3查看 1.5K关注 0票数 3

例如,假设我想数10个大文件的行数,然后打印一个总计。

代码语言:javascript
运行
复制
for f in files
do
    #this creates a background process for each file
    wc -l $f | awk '{print $1}' &
done

我试过这样的方法:

代码语言:javascript
运行
复制
for f in files
do
    #this does not work :/
    n=$( expr $(wc -l $f | awk '{print $1}') + $n ) &
done

echo $n
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-08-09 05:20:51

您可能应该使用gnu并行:

代码语言:javascript
运行
复制
find . -maxdepth 1 -type f | parallel --gnu 'wc -l' | awk 'BEGIN {n=0} {n += $1} END {print n}'

否则,xargs采用并行模式:

代码语言:javascript
运行
复制
find . -maxdepth 1 -type f | xargs -n1 -P4 wc -l | awk 'BEGIN {n=0} {n += $1} END {print n}'

另一个选项,如果这不符合您的需要,是写到临时文件。如果不想写入磁盘,只需写入/dev/shm即可。这是大多数Linux系统上的一个ramdisk。

代码语言:javascript
运行
复制
#!/bin/bash

declare -a temp_files

count=0
for f in *
do
  if [[ -f "$f" ]]; then
    temp_files[$count]="$(mktemp /dev/shm/${f}-XXXXXX)"
    ((count++))
  fi
done

count=0
for f in *
do
  if [[ -f "$f" ]]; then
    cat "$f" | wc -l > "${temp_files[$count]}" &
    ((count++))
  fi
done

wait

cat "${temp_files[@]}" | awk 'BEGIN {n=0} {n += $1} END {print n}'

for tf in "${temp_files[@]}"
do
  rm "$tf"
done

顺便说一句,这可以被看作是一个映射-减少与wc做映射和awk做缩减。

票数 1
EN

Stack Overflow用户

发布于 2013-08-09 00:04:43

我终于找到了一个使用匿名管道和bash的工作解决方案:

代码语言:javascript
运行
复制
#!/bin/bash

# this executes a separate shell and opens a new pipe, where the 
# reading endpoint is fd 3 in our shell and the writing endpoint
# stdout of the other process. Note that you don't need the 
# background operator (&) as exec starts a completely independent process.
exec 3< <(./a.sh 2&1)


# ... do other stuff


# write the contents of the pipe to a variable. If the other process
# hasn't already terminated, cat will block.
output=$(cat <&3)
票数 3
EN

Stack Overflow用户

发布于 2013-08-09 00:13:30

您可以将其写入文件或更好的文件中,在数据到达时立即收听fifo。

下面是一个关于它们如何工作的小例子:

代码语言:javascript
运行
复制
# create the fifo
mkfifo test

# listen to it
while true; do if read line <test; then echo $line; fi done

# in another shell 
echo 'hi there'

# notice 'hi there' being printed in the first shell

所以你可以

代码语言:javascript
运行
复制
for f in files
do
    #this creates a background process for each file
    wc -l $f | awk '{print $1}' > fifo &
done

听fifo的大小。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18138195

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档