object-oriented shell for linux? [closed]

2019-02-08 10:14发布

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)?

edit: especially if similar to bash or powershell or cmd etc. syntax (=''standard'' shell syntax)

7条回答
我欲成王,谁敢阻挡
2楼-- · 2019-02-08 11:11

NodeJS can 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.

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:

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:

#!/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();
                }
            });
        }
    }
}
查看更多
登录 后发表回答