Shell脚本等待命令永无止境

时间:2018-10-06 14:30:43

标签: android linux macos shell sh

我正在编写一个脚本文件以从外壳运行Android测试用例,我有一些Android ADB commands可以在脚本文件中运行测试用例以执行并让wait等待第一组测试用例第一组结束后结束,然后几秒钟开始。

脚本文件:代码段

#!/bin/bash
# set up code

#First set of test cases
adb -s emulator-5554 shell am instrument -w com.android.demo.app.tests1/android.support.test.runner.AndroidJUnitRunner &
adb -s emulator-5556 shell am instrument -w com.android.demo.app.tests2/android.support.test.runner.AndroidJUnitRunner &
wait # Second set test cases does not started because this **wait** never ends

#Second set of test cases
adb -s emulator-5554 shell am instrument -w com.android.demo.app.tests3/android.support.test.runner.AndroidJUnitRunner &
adb -s emulator-5556 shell am instrument -w com.android.demo.app.tests4/android.support.test.runner.AndroidJUnitRunner &
wait

echo '--------- Test cases done --------------'

如果我运行脚本,则执行脚本的第一个运行例集(test1和test2),并且等待永不结束,以启动第二个测试例集 但是Android Studio(适用于Android的IDE)表示,已针对第一个设置的test1和test2完成了测试用例。 注意:ADB在Android Emulator上运行测试用例。 请帮助我为什么我没有得到等待结束。

2 个答案:

答案 0 :(得分:0)

我使用以下代码进行了测试:

#!/bin/bash
#set 1
ls -la &
ls -la &
wait # wait here

#set 2
pwd &
pwd &
wait # wait here

代码可以按预期运行,没有中断或错误。

请尝试运行test3和test4而不运行test1和test2,看看是否有帮助。

编辑:另外,从另一个答案here,您应该能够捕获上一个命令的PID(processID),​​并将该PID用作等待参数。

答案 1 :(得分:0)

wait等待当前shell的所有 children 。如上例所示,您可能要先分叉一个孩子,然后再开始分派测试用例。示例:

#!/bin/bash

sleep 40 &  # something unrelated you forgot to mention

# test case set 1:
sleep 5 &   # first test case
sleep 7 &   # second test case
wait  # this now will wait for the two test cases _AND_ the sleep 40 from above.

有时候“不相关的”分叉的东西不容易发现,所以不要草率行事。

如果这是导致您出现问题的原因,则可以通过使用其他外壳生成另一个孩子来修复它,如下所示:

(sleep 40 &)  # use a different shell to spawn off the child
相关问题