面向对象的 linux 外壳?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4497168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
object-oriented shell for linux?
提问by Abbafei
Is there anything similar to Microsoft Powershell (an object-oriented shell built on the .NET framework) for Linux (possibly built on Java, GObject, or its own object type/nothing)?
是否有类似于 Microsoft Powershell(基于 .NET 框架构建的面向对象外壳)的 Linux(可能基于 Java、GObject 或它自己的对象类型/无)的东西?
edit: especially if similar to bash or powershell or cmd etc. syntax (=''standard'' shell syntax)
编辑:特别是如果类似于 bash 或 powershell 或 cmd 等语法(=''standard'' shell 语法)
回答by T.J. Crowder
NodeJScan do that, in fact it's one of the samples included in the download. Use it interactively, or (probably more usefully) write shell scripts in JavaScript.
NodeJS可以做到这一点,实际上它是下载中包含的示例之一。以交互方式使用它,或者(可能更有用)用 JavaScript 编写 shell 脚本。
For example:
例如:
#!/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);
});
...but that's just the high-level interface that buffers all the output for you, etc. You can get a lot more down and dirty than that if you like.
...但这只是为您缓冲所有输出等的高级接口。如果您愿意,您可以获得比这更糟糕和肮脏的东西。
NodeJS takes asynchronicity as the normal state of affairs, and so if you want a "traditional" shell script, you may find it's not a good match as it doesn't (as of this writing, as far as I know) offer a synchronous version of exec
. So an ad hoc series of serial statements becomes an exercise in callbacks:
NodeJS 将异步性作为事务的正常状态,因此如果您想要一个“传统”的 shell 脚本,您可能会发现它不是一个很好的匹配,因为它没有(在撰写本文时,据我所知)提供同步的版本exec
。所以一系列特别的串行语句变成了回调练习:
exec('first_command', function(error) {
if (error != null) {
exec('second_command', function(error) {
if (error != null) {
// ....
}
});
}
});
...but of course, you can create a function that handles that for you and takes (say) an array of sequential statements to execute (and then install it as a module via Node's module sysstem). So for instance:
...但当然,您可以创建一个函数来为您处理它并采用(例如)一组顺序语句来执行(然后通过 Node 的模块系统将其安装为一个模块)。所以例如:
#!/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("Done\n");
});
// ===> 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();
}
});
}
}
}
回答by Will
Python. No joking.
Python。没有开玩笑。
Scripting languages are scripting languages, and Python is a particularly nice one that many people find very approachable.
脚本语言就是脚本语言,Python 是一种特别好的语言,很多人觉得它很容易上手。
回答by DigitalRoss
Perl, Python, and Ruby
Perl、Python 和 Ruby
Ok, I'm sure you already know that, but someone had to say it.
好吧,我相信你已经知道了,但有人不得不说。
Perl is the oldest and most popular.
Perl 是最古老和最受欢迎的。
If you like objects, you will probably love Ruby. It has an elaborate object system inspired by Smalltalk.
如果您喜欢对象,您可能会喜欢 Ruby。它有一个受 Smalltalk 启发的精心设计的对象系统。
Python has this cool block-structure-by-indent syntax.
Python 有这种很酷的按缩进块结构的语法。
Unix is a gold mine of advanced scripting tools...
Unix 是高级脚本工具的金矿...
回答by sorpigal
You should rethink why it is you think you need an object-oriented shell. That said, if you're set trying weird shells you can't go wrong with zoid. Unlike many of the other suggestions I see here it really is a shell. On the other hand, if you don't know or don't like Perl you probably won't be happy.
你应该重新思考为什么你认为你需要一个面向对象的 shell。也就是说,如果你准备尝试奇怪的 shell,那么zoid就不会出错。与我在这里看到的许多其他建议不同,它确实是一个外壳。另一方面,如果您不知道或不喜欢 Perl,您可能不会高兴。
回答by Abbafei
回答by Carson McManus
Even though this question is pretty old, I think its worth mentioning that in August 2016 Microsoft made Powershell open-source and cross platform. Instructions for installation are on github.
尽管这个问题已经很老了,但我认为值得一提的是,微软在 2016 年 8 月使 Powershell 开源和跨平台。安装说明在github上。