当将exec与&一起使用时,最终命令不会运行 [英] When using exec with &, the final command does not run

查看:50
本文介绍了当将exec与&一起使用时,最终命令不会运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果// fi 未运行,则似乎是该代码.这是我所拥有的:

It seems the code after if/fi is not running. Here is what I have:

我有一个脚本,/my/scripts/dir/directoryPercentFull.sh:

I have a script, /my/scripts/dir/directoryPercentFull.sh:

directoryPercentFull="$(df | grep '/aDir/anotherDir' | grep -o '...%' | sed 's/%//g' | sed 's/ //g')"
if [ $directoryPercentFull -gt 90 ]
then
    echo $directoryPercentFull
    exec /someDir/someOtherDir/test01.sh &
    exec /someDir/someOtherOtherDir/test02.sh &
    exec /someDir/yetAnotherDir/test03.sh
fi

echo "Processing Done"

被调用的脚本是:/someDir/someOtherDir/test01.sh

The scripts being called are: /someDir/someOtherDir/test01.sh

#!/usr/bin/env bash
echo "inside test01.sh"
sleep 5
echo "leaving test01.sh"

/someDir/someOtherOtherDir/test02.sh

/someDir/someOtherOtherDir/test02.sh

#!/usr/bin/env bash
echo "inside test02.sh"
sleep 5
echo "leaving test02.sh"

/someDir/yetAnotherDir/test03.sh

/someDir/yetAnotherDir/test03.sh

#!/usr/bin/env bash
echo "inside test03.sh"
sleep 5
echo "leaving test03.sh"

通过cd-/my/scripts/dir来运行脚本,然后执行./directoryPercentFull.sh可以得到:输出:

running the script by cd-ing to /my/scripts/dir and then doing ./directoryPercentFull.sh gives: OUTPUT:

93
inside test03.sh
inside test02.sh
inside test01.sh
leaving test03.sh
leaving test01.sh
leaving test02.sh

预期输出:

93
inside test01.sh
inside test02.sh
inside test03.sh
leaving test01.sh
leaving test02.sh
leaving test03.sh
Processing Done

echo命令的顺序没什么大不了的,尽管如果有人知道为什么它们分别以3,2,1,然后3,1,2,我不会讨厌解释.

The order of the echo commands are not that big of a deal, though if someone knows why they go 3,2,1, then 3,1,2, I wouldn't hate an explanation.

但是,我没有得到最后的处理完成.任何人都知道为什么没有出现在/my/scripts/dir/directoryPercentFull.sh 中的最终 echo 吗?我没有在最后一个 exec 语句之后放置& ,因为我不希望 if / fi 即可运行,直到全部处理完毕.

However, I am not getting that final Processing Done. Anyone have any clue why the final echo back in /my/scripts/dir/directoryPercentFull.sh does not occur? I have purposefully not placed an & after the last exec statement, as I don't want what what is after the if/fi to run until all of it is finished processing.

推荐答案

/someDir/someOtherDir/test01.sh &
/someDir/someOtherOtherDir/test02.sh &
/someDir/yetAnotherDir/test03.sh

摆脱所有 exec . exec 导致shell进程被给定命令替换,这意味着shell不会继续执行其他命令.

Get rid of all the execs. exec causes the shell process to be replaced by the given command, meaning the shell does not continue executing further commands.

echo命令的顺序没什么大不了的,尽管如果有人知道为什么它们分别以3,2,1,然后3,1,2,我不会讨厌解释.

The order of the echo commands are not that big of a deal, though if someone knows why they go 3,2,1, then 3,1,2, I wouldn't hate an explanation.

打印输出可以按任何顺序排列.这三个脚本是在并行进程中运行的,因此无法告诉它们 echo 打印输出的顺序.

The printouts could come in any order. The three scripts are run in parallel processes so there's no telling which order they echo their printouts.

这篇关于当将exec与&一起使用时,最终命令不会运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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