我的老师希望我实现使用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'
答案 0 :(得分:0)
如果您想查看内核中内置了哪些命令,也许可以帮到您(使用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