node.js - 如何区分linux原生命令和第三方软件命令

标签 node.js linux

老师想让我用nodejs实现调用shell命令的功能,并期望在调用第三方软件命令时打印可执行路径。

var exec = require('child_process').exec;
const iconv = require('iconv-lite');
function execute(cmd) {
            if(not linux original command) cmd='whereis '+cmd  
            exec(cmd, {encoding: 'buffer'}, (error, stdout) => {
                if(error) console.log(error);;
                console.log(iconv.decode(stdout,'gbk'))
            });
}
execute("ls -l");  //  the result is 'drwxr-xr-x 2 root root 4096 ...'
execute("node") // the result is 'usr/local/node'

最佳答案

如果您想查看内核中内置了哪些命令,也许这可以帮助您(使用 which 命令):

> which -a which
which: shell built-in command
/usr/bin/which
> which -a find
/usr/bin/find

type 命令也有效:

> type -a which
which is a shell builtin
which is /usr/bin/which

> type -a cd
cd is an alias for __enhancd::cd
cd is a shell builtin
cd is /usr/bin/cd

> type -a ls
ls is an alias for ls -G
ls is /bin/ls

关于node.js - 如何区分linux原生命令和第三方软件命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56712969/

相关文章:

node.js - Firebase 的云功能 : admin vs root lookups

javascript - 使用 forEach() 返回数组值

linux - 如何使用 --harmony 选项使 nodejs 文件可执行

linux - 如何同步网络服务器

linux - 在linux中获取CLOCK_TICK_RATE值

c - fork-exec-dup 后没有返回终端提示

linux - 尝试运行扩展 GIT 的脚本

c - 在C编程中获取设备名称[Linux]

node.js - 如何从 package.json 中列出的 node_modules 中删除所有包

javascript - 如何有条件地使用带有 ES6 样式导入的内置 Node.js 或浏览器 API 方法?