HarmonyOS鸿蒙Next中如何运行shell命令,并获取结果

HarmonyOS鸿蒙Next中如何运行shell命令,并获取结果 如题。

它目前有提供函数ShellCmdResult,但是我在按照文档使用时AbilityDelegatorRegistry.getAbilityDelegator()结果为null。

所以如何才能运行shell命令并获取结果。

10 回复

根据文档只能在测试框架模块使用,那要是在应用中执行shell命令,该如何处理呢,还有就是我写好的测试用例,能不能通过在应用中执行shell命令来执行这个测试用例呢,希望有人解答下,谢谢

更多关于HarmonyOS鸿蒙Next中如何运行shell命令,并获取结果的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


说是只能在测试框架里用

let abilityDelegator: AbilityDelegatorRegistry.AbilityDelegator;
let result = await abilityDelegator.executeShellCommand(cmd,5000);

一样为空?华为的工程师看看吧,这个功能很重要的,谢谢!

同为null,求解

找HarmonyOS工作还需要会Flutter的哦,有需要Flutter教程的可以学学大地老师的教程,很不错,B站免费学的哦:BV1S4411E7LY/?p=17

我也遇到了同样的问题,AbilityDelegatorRegistry.getAbilityDelegator()结果为null。不知如何解决?

楼主最后是怎么解决的?

就是参考的文档啊。为啥还是null,

在HarmonyOS鸿蒙Next中,可以通过@ohos.process模块运行shell命令并获取结果。首先导入模块:

import process from '@ohos.process';

然后使用process.runCmd()执行命令,并通过回调函数获取结果。示例代码如下:

process.runCmd('ls', (err, stdOut, stdErr) => {
    if (err) {
        console.error('Error:', err);
        return;
    }
    console.log('stdOut:', stdOut);
    console.log('stdErr:', stdErr);
});

runCmd方法执行命令并返回标准输出和标准错误。

回到顶部