linux 对象shell,面向linux的面向对象的shell?_oop_开发99编程知识库

NodeJS 做到,事實上,它是包含在下載的示例之一。 使用它以交互或有益的( 可能是多個) Javascript中編寫shell腳本。

例如:#!/usr/local/bin/node

var sys = require('sys'),

exec = require('child_process').exec;

// Run `ls`:

exec('ls -lh /usr', function(error, output, erroutput) {

sys.print('output: ' + output);

sys.print('erroutput: ' + erroutput);

});

。。。但這隻是高層介面,該介面緩存所有的輸出等。 你可以獲得更多流行比如果你喜歡。

NodeJs將asynchronicity作為正常狀況,因此如果需要" 傳統" shell腳本,你可能會發現很不好,因為它不匹配( 在撰寫本文時,據我所知) 提供的同步版本 exec. 所以臨時一系列串列語句成為政黨政治回調:exec('first_command', function(error) {

if (error != null) {

exec('second_command', function(error) {

if (error != null) {

// ....

}

});

}

});

...但當然,可以為你創建的函數可以處理和接受( 說) 的數組順序執行的語句( 然後安裝它作為模塊通過Node是模塊sysstem ) 。 下面舉個實例:#!/usr/local/bin/node

var sys = require('sys'),

exec = require('child_process').exec;

execSeries([

'ls -ld /usr',

'foobar',

'ls -ld /etc'

], {echo: true}, function(results) {

sys.print("Donen");

});

// ===> This would be in a module, not in the script itself <===

function execSeries(series, options, callback) {

var index = 0,

results = [];

// Make 'options' optional

if (!callback && typeof options === "function") {

callback = options;

options = undefined;

}

// Default options

options = options || {};

// Go

callNext();

function callNext() {

if (index >= series.length) {

// Done

callback(results);

}

else {

// Call the next one

exec(series[index++], function(error, stdout, stderr) {

// Record result

results.push({error: error, stdout: stdout, stderr: stderr});

// Echo?

if (options.echo) {

if (error == null) {

sys.print(stdout);

}

else {

sys.print("Error: " + error + "n");

}

}

// Stop on error?

if (options.breakOnError && error != null) {

// Yes, and there was an error; stop

callback(results);

}

else {

// No, continue

callNext();

}

});

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值