如何从 Atom 电子应用程序调用 Shell 脚本或 python 脚本

How to call Shell script or python script in from a Atom electron app

本文关键字:脚本 Shell 调用 python 应用程序 Atom      更新时间:2023-09-26

我正在尝试使用Atom电子为Mac和Windows编写桌面应用程序。

我在这里需要的是:

一个按钮。

当用户单击该按钮时,它将运行以下shell(或python脚本):

ping x.x.x.x

结果将显示在文本区域中。

我尝试使用[shelljs]和[yargs],但似乎它不适用于原子电子。

我想要的只是使用JAVASCRIPT编写桌面应用程序(当然使用GUI),调用一些脚本(shell&& python)来完成一些自动化工作。

任何建议将不胜感激,谢谢:)

可以直接

用 Node 完成,可以使用 child_process 模块。请注意,这是异步的。

const exec = require('child_process').exec;
function execute(command, callback) {
    exec(command, (error, stdout, stderr) => { 
        callback(stdout); 
    });
};
// call the function
execute('ping -c 4 0.0.0.0', (output) => {
    console.log(output);
});

我鼓励你也看看npm,有大量的模块可以帮助你做你想做的事情,而无需调用python脚本。

试试 node-powershell npm。您可以直接执行 shell 脚本命令并显示结果。

var shell = require('node-powershell')
var ps = new shell()
ps.addCommand('ping -c 4 0.0.0.0')
ps.invoke()
.then(function (output) {
    console.log(output)
})
.catch(function (err) {
    console.log(err)
    ps.dispose()
})

请参阅:https://www.npmjs.com/package/node-powershell

您可以使用child_process使用以下代码来存档您尝试执行的操作

var exec = require('child_process').exec
function Callback(err, stdout, stderr) {
    if (err) {
        console.log(`exec error: ${err}`);
        return;
    }else{
        console.log(`${stdout}`);
    }
}
res = exec('ping xxx.xxx.xxx', Callback);