Bash - 在后台运行程序仍会导致脚本等待

时间:2015-10-20 13:55:10

标签: bash

我为我最喜欢的编辑器nedit写了一个简单的包装器脚本。它将获取一个参数列表并在一个窗口中打开所有非gzip文件,它将获取每个gzip压缩文件,将它们转换为临时文件,并在单独的窗口中打开它们。但是,运行nedit会导致脚本等到窗口关闭,即使我使用&nohup。我在这里错过了什么吗?

#!/bin/bash

declare -a nongzipped
for file in $@; do
    if file $file | grep -q gzip; then
        timestamp=$(date +"%F_%T")
        tempfile="tmp_$timestamp"
        $( zcat $file > $tempfile )
        $( nedit -background lightskyblue3 $tempfile & )
        $( rm $tempfile )
    else
        nongzipped+=("$file")
    fi
done

if [ ${#nongzipped[@]} -ne 0 ]; then
    $( nedit ${nongzipped[@]} & )
fi

2 个答案:

答案 0 :(得分:4)

命令替换$(...)引发的shell在后台作业完成之前无法退出。 (将sleep &$( sleep & )进行比较。)无论如何你都不需要它们,所以只需删除它们。

if file $file | grep -q gzip; then
    timestamp=$(date +"%F_%T")
    tempfile="tmp_$timestamp"
    zcat $file > $tempfile
    nedit -background lightskyblue3 $tempfile &
    rm $tempfile
else
    nongzipped+=("$file")
fi    

答案 1 :(得分:1)

此语法通常用于获取$(command)的输出并将其分配给变量。这可能就是你的脚本必须等待的原因。

您最好在没有任何特殊语法的情况下调用您的程序,只需command

相关问题