2

I am trying to pass in an input as one line to an user-input prompt, but am having difficulty working out how to do it correctly.

Specifically I am trying to login to npm using npm adduser (npm login is its alias)

When it's a singular value it works but this only gets me so far: echo exampleuser| npm adduser Username: exampleuser Password: Password: npm ERR! cb() never called!

But unfortunately when I try to add multiple commands together it goes awry. Eg:
echo 'exampleuser examplepassword ex@email.com'| npm adduser or
echo 'exampleuser\r\nexamplepassword\r\nex@email.com'| npm adduser or
echo 'exampleuser&& echo examplepassword&& echo ex@email.com'| npm adduser, etc...

Gets back errors along the lines of Username: 'exampleuser examplepassword' npm WARN Name must be lowercase Username: Username: npm ERR! cb() never called!

Any suggestions would be greatly appreciated.

vitalbone
  • 101
  • 1
  • 4
  • `npm` as far as I know is a batch file and not a console application. So you can't run it with redirecting a string output by `echo` to handle __stdout__ to __stdin__ of the executable. You need to __call__ `npm.bat` with appropriate parameters using command `call`. Run in a command prompt window `call /?` for help on this command. And read the documentation of `npm` as you have obviously not done. It is advisable to run scripts and executables in a batch file not just with file name, but with file name + file extension (and if possible also with path). Then you would have seen your mistake. – Mofi Mar 30 '17 at 05:15
  • @Mofi Calm down champ. I have read the `npm` documentation extensively, but must have missed the part referring to `npm` as a batch file - care to point me to it? – vitalbone Mar 30 '17 at 06:32
  • I'm calm. I had never installed `npm` package. I just know that `npm` is a batch file on Windows (and most likely a shell script on Linux) because of reading many pages about `npm` usage issues on Windows in batch files found with using a www search engine with `Windows npm batch file` as search string. I wrote [this answer](http://stackoverflow.com/a/38676582/3074564) once in the past. It looks like `npm` batch script package is not well written at the moment. I suppose the `npm` shell script package is much better. – Mofi Mar 30 '17 at 07:17
  • If [npm Documentation](https://docs.npmjs.com/) really does not contain clearly the information that `npm` is a batch file with file extension ??? (bat or cmd) on Windows and a ??? (sh/bash/ksh/...) script on Linux, somebody using `npm` should really inform the authors of this website that this very important fact for script writers on any OS should be also mentioned in documentation, for example on [What is npm?](https://docs.npmjs.com/getting-started/what-is-npm) – Mofi Mar 30 '17 at 07:20
  • 1
    `echo 'text'` returns the text `'text'` literally, including the apostrophes; hence you should use `echo text`, and ensure there is no trailing space, which would also be returned; so with the pipe, it must read: `echo text| npm` (although I do not know whether or not `npm` reads data from the console input)... – aschipfl Mar 30 '17 at 07:44

1 Answers1

1

to pass several values, you have to pass several lines. echo isn't able to echo a line break, so you have to use several echo's

(
echo exampleUser
echo hisPassword
echo ex@email.com
)|npm adduser

As a single line:

(echo exampleUser&echo hisPassword&echo ex@email.com)|npm adduser

(note: NO space before &!)

(Note: this is how to pass several parameters; Can't test, if npm will actually accept them)

Stephan
  • 53,940
  • 10
  • 58
  • 91