老师想让我用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/