Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://nodejs.org/" rel="nofollow">NodeJS</a> 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.</p> <p>For example:</p> <pre><code>#!/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); }); </code></pre> <p>...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.</p> <p>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 <code>exec</code>. So an ad hoc series of serial statements becomes an exercise in callbacks:</p> <pre><code>exec('first_command', function(error) { if (error != null) { exec('second_command', function(error) { if (error != null) { // .... } }); } }); </code></pre> <p>...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:</p> <pre><code>#!/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"); }); // ===&gt; This would be in a module, not in the script itself &lt;=== function execSeries(series, options, callback) { var index = 0, results = []; // Make 'options' optional if (!callback &amp;&amp; typeof options === "function") { callback = options; options = undefined; } // Default options options = options || {}; // Go callNext(); function callNext() { if (index &gt;= 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 &amp;&amp; error != null) { // Yes, and there was an error; stop callback(results); } else { // No, continue callNext(); } }); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload