0
我正在嘗試創建一個Grunt任務,該任務將運行'finger -lp'命令來捕獲登錄到計算機的用戶的名稱和登錄名。從終端執行時從grunt shell命令中捕獲用戶名
這裏的「手指-lp」的標準輸出:
Login: abc123 Name: John Doe
Directory: /Users/abc123 Shell: /bin/bash
On since Thu Oct 30 19:40 (EDT) on console, idle 19:34 (messages off)
On since Fri Oct 31 10:24 (EDT) on ttys002
No Mail.
下面是如何我使用grunt.util.spawn運行相同的指令:
module.exports = function (grunt) {
grunt.registerTask('username', '',
function() {
var done = this.async();
grunt.util.spawn({
cmd: 'finger',
args: ['-lp'],
opts: {stdio: 'inherit'},
fallback: ''
}, function (err, result, code) {
console.info('RESULT: ',err, result, code);
done();
});
});
};
這是上面console.info聲明的輸出:
[email protected]:~/WorkFiles/john-doe/$ grunt username
Running "username" task
Login: abc123 Name: John Doe
Directory: /Users/abc123 Shell: /bin/bash
On since Thu Oct 30 19:40 (EDT) on console, idle 19:34 (messages off)
On since Fri Oct 31 10:24 (EDT) on ttys002
No Mail.
RESULT: null { stdout: '', stderr: '', code: 0, toString: [Function] } 0
因此,Grunt任務沒有執行e命令finger -lp
,其方式是將結果輸出到終端,但不被grunt.util.spawn
過程捕獲。 result
對象中的stdout
爲空。
RESULT: null { stdout: '', stderr: '', code: 0, toString: [Function] } 0
我下面從node的例子,因爲它是什麼grunt.util.spawn用途。