I simply can't find the way to read from the keyboard.
Inside the script file, I tried with this:
CLI run
CLI setPrompt
CLI readLine
CLI whatever
but it doesn't work.
Alternatively, a way of reading from a line that doesn't require you have libreadline installed, is to use:
myInputtedValue := File standardInput readLine
You can just do:
myInputtedValue := ReadLine readLine
Related
I wonder about simple equivalent of perl -ne 'some expression' to be able to use node CLI possibly with --eval '<some expression, func/arrow>' and --require some-line-by-line-enabler. Is there any module making it possible or what can be approach to write one?
I've also found i.e. https://github.com/j-/require-cli and wonder if this may be the right way to go. I was trying it preparing some very basic module exposing forward of readline.on('line', callback) but consuming the stdin does not work just out of the box.
You can eval scripts with Node from command line:
node -e "console.log('Hello!')"
I'm want to run a shell command via node and capture the result of stdout. My script works fine on OSX, but not on Ubuntu.
I've simplified the problem and script to the following node script:
var execSync = require('child_process').execSync,
result = execSync('echo "hello world" >> /dev/stdout');
// Do something with result
Results in:
/bin/sh: 1: cannot create /dev/stdout: No such device or address
I have tried replacing /dev/stdout with /dev/fd/1
I have tried changing the shell to bash... execSync('echo ...', {shell : '/bin/bash'})
Like I said, the problem above is simplified. The real script accepts as a parameter the name of a file where results should be written, so I need to resolve this by providing access to the stdout stream as a file descriptor, i.e. /dev/stdout.
How can I execute a command via node, while giving the command access to its own stdout stream?
On /dev/stdout
I don't have access to an OSX box, but from this issue on phantomjs, it seems that while on both OSX/BSD and Linux /dev/stdout is a symlink, nonetheless it seems to work differently between them. One of the commenters said it's standard on OSX to use /dev/stdout but not for Linux. In another random place I read statements that imply /dev/stdout is pretty much an OSX thing. There might be a clue in this answer as to why it doesn't work on Linux (seems to implicitly close the file descriptor when used this way).
Further related questions:
https://unix.stackexchange.com/questions/36403/portability-of-dev-stdout
bash redirect to /dev/stdout: Not a directory
The solution
I tried your code on Arch and it indeed gives me the same error, as do the variations mentioned - so this is not related to Ubuntu.
I found a blog post that describes how you can pass a file descriptor to execSync. Putting that together with what I got from here and here, I wrote this modified version of your code:
var fs = require('fs');
var path = require('path');
var fdout = fs.openSync(path.join(process.cwd(), 'stdout.txt'), 'a');
var fderr = fs.openSync(path.join(process.cwd(), 'stderr.txt'), 'a');
var execSync = require('child_process').execSync,
result = execSync('echo "hello world"', {stdio: [0,fdout,fderr] });
Unless I misunderstood your question, you want to be able to change where the output of the command in execSync goes. With this you can, using a file descriptor. You can still pass 1 and 2 if you want the called program to output to stdout and stderr as inherited by its parent, which you've already mentioned in the comments.
For future reference, this worked on Arch with kernel version 4.10.9-1-ARCH, on bash 4.4.12 and node v7.7.3.
I am trying to over-write a file using python and my code looks something like this:
from sys import argv
script = argv
Configuration_file = 'C:/Python33/argv.txt'
f= open(Configuration_file,'w')
f.write('script')
and when I try to run the file using command prompt by using the command
python argvnew.py roshan,
where argvnew.py is my python file and roshan is my argument. I expect that roshan replaces anything that is written within the argv.txt file mentioned in the program.
Is this the right way to do this?
You can get arguments by calling sys.argv[] array.
sys.argv[0] means script name itself.
You can then write it back to your file, open the file such as here.
Is there a way to preload some file before each time I run node (interactively), just like .vimrc, .bash_profile, etc.?
I use node mainly interactively, and I use the module CSV a lot, is there a way to avoid typing require('./csv') every time I start node?
Create an initialization file (for example ~/.noderc):
var csv = require('csv');
// put a blank line at the end of the file
Now add this line to your shell config (.bashrc / .zshrc / whatever shell you use):
alias nodei="cat ~/.noderc - | node -i"
VoilĂ !
#Ilan Frumer provided one way to do it. I think I'll give another choice here: build a REPL of your own.
From their documentation. You can find a way to write a repl of your own. You can add whatever scripts before and after the interations of it, and even use some advance API's.
For example, I created a file called .noderc.js under ~ as follows
repl = require('repl');
myFunc = function(){
console.log("Hello, there!");
};
repl.start("> ");
And you can go ahead and alias nodei="node ~/.noderc.js",
$ nodei
> myFunc()
Hello, there!
undefined
I need to be able to change the prompt on running an executable of a c file to get a custom prompt
E.g:
$ abc
abc>
Here the user can give the commands acceptable to the program.
I saw this happen for programs like MySQL and was wondering if it is possible to do this.
You can use gnu readline for custom prompt
#include <readline/readline.h>
#include <readline/history.h>
while (1)
{
command = readline ("$abc");
command = readline ("abc>");
//validate your command name
system(command);
add_history (command); ///add command in history
}
you can include the readline library in your program to make it have a modern command line interface.
Or you can simply build a loop get each line from input and get the tokens from that line of input to execute commands, and there printout your abc> prompt.