Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a node.js command line tool that I'd like to run in order to generate an asset in response to an HTTP Request that I receive in Vapor 4. Is it possible to do this?
I guess it is something like this
func requestHandler(_ req: Request) throws -> EventLoopFuture<HTTPStatus> {
let promise = req.eventLoop.makePromise(of: HTTPStatus.self)
let process = Process()
// e.g. use `which node` to find path to `node`
process.executableURL = URL(fileURLWithPath: "/path/to/binary") // e.g. /usr/bin/node
// in which folder execute the command, it is optional
process.currentDirectoryPath = "/path/to/folder"
// optional arguments, e.g. if your arguments are -c release then it should be ["-c", "release"]
process.arguments = ["arg1", "arg2", "argN"]
// wait for termination in closure
process.terminationHandler = { process in
switch process.terminationStatus {
// probably normal termination via SIGTERM or when process successfully finished
case 0:
promise.succeed(.ok)
default:
promise.fail(Abort(.failedDependency, reason: "Process finished with code \(process.terminationStatus)"))
}
}
// don't forget to launch it
try process.run()
return promise.futureResult
}
Learning Node and get stuck with this example:
// Adding child_process from the Node build-in Module:
let exec = require("child_process").exec;
// Requesting time:
let child = exec("uptime", function(err, stdout, stderr){
if (err) {
console.log("Error: " + stderr);
} else {
console.log("Output is: " + stdout);
}
});
console.log("PID is: " + child.pid);
Can someone explain, why is this code running in Linux and not in Windows. Sorry, this may be a dumb question, but I am newbie and currently learning Node.
P.S. sorry if my question is not correct, I'm still getting used to post questions here. Thank you in advance!
I think uptime is not windows command. It only works in Linux
You can use
systeminfo | find "Boot Time"
inplace of uptime. like
exec('systeminfo | find "Boot Time"')
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I have 1000 of URLs. I need a tools that get my URLs and export all the text that appears on those pages.
I need the texts that a is shown on the webpages, not the background html code.
Do you know any software or way to do it?
Save this as bat file (i.e. innerTextGet.bat):
#if (#X)==(#Y) #end /* JScript comment
#echo off
cscript //E:JScript //nologo "%~f0" %*
::pause
exit /b %errorlevel%
#if (#X)==(#Y) #end JScript comment */
var link=WScript.Arguments.Item(0);
var saveTo=WScript.Arguments.Item(1);
var IE = new ActiveXObject("InternetExplorer.Application");
IE.Visible=false;
IE.Navigate2(link);
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
}
var counter=0;
while (IE.Busy && counter<60*60*10) {
//WScript.Echo(IE.Busy);
sleep(1000);
counter++;
}
if(IE.Busy){
WScript.Echo("Cant wait 4ever");
WScript.Quit(10);
}
function writeContent(file,content) {
var ado = WScript.CreateObject("ADODB.Stream");
ado.Type = 2; // adTypeText = 2
ado.CharSet = "iso-8859-1"; // right code page for output (no adjustments)
//ado.Mode=2;
ado.Open();
ado.WriteText(content);
ado.SaveToFile(file, 2);
ado.Close();
}
var innerText=IE.document.body.innerText;
IE.Quit();
writeContent(saveTo,innerText);
And use it like:
call innerTextGet.bat "https://stackoverflow.com/questions/46611374/save-texts-on-webpages-1000-pages" result.txt
It is not fail safe - does not check if the result file already exists if the parameters are correctly passed and so on, but it works at least. It again uses innerText property of InternetExplorer.Application object as proposed by #omegastripes though I prefer jscript because it is easier to be plugged into batch file.
As you gave no information about where the links are stored I assume you know how to read and iterate through them.
As the title suggests, I need to find a way to get the list of running applications (atom, chrome, etc.). I am currently using:
var exec = require('child_process').exec
exec('tasklist', (error, stdout, stderr) {
// stdout contains a list of running processes.
})
However this also gives services and hidden applications (redis-server, etc.) and doesn't return whether or not the window is currently active or not. Is there a way for this to be done? For reference, this is for a Windows system, but a cross-operating system solution would be preferable.
I found the wonderful winctl library allowed me to do what I needed. I used the following code:
const winctl = require('winctl')
// Iterate over all windows with a custom filter
winctl.FindWindows(win => win.isVisible() && win.getTitle()).then(windows => {
console.log("Visible windows:");
windows.sort((a,b) => a.getTitle().localeCompare(b.getTitle())).forEach(window => console.log(" - %s [pid=%d, hwnd=%d, parent=%d]", window.getTitle(), window.getPid(), window.getHwnd(), window.getParent()));
});
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
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)
Python. No joking.
Scripting languages are scripting languages, and Python is a particularly nice one that many people find very approachable.
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.
https://github.com/PowerShell/PowerShell
Perl, Python, and Ruby
Ok, I'm sure you already know that, but someone had to say it.
Perl is the oldest and most popular.
If you like objects, you will probably love Ruby. It has an elaborate object system inspired by Smalltalk.
Python has this cool block-structure-by-indent syntax.
Unix is a gold mine of advanced scripting tools...
Hotwire
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();
}
});
}
}
}
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.
jq is not quite an object-oriented shell, but it provides some of the benefits which object-oriented shells may have; I use it a lot, together with shell scripts, for such tasks.