Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shelljs exec() not executing variables #815

Closed
indra-Asurion opened this issue Jan 18, 2018 · 4 comments
Closed

Shelljs exec() not executing variables #815

indra-Asurion opened this issue Jan 18, 2018 · 4 comments
Labels
question Question from a user (which may not require code/documentation changes to the project) wontfix

Comments

@indra-Asurion
Copy link

indra-Asurion commented Jan 18, 2018

Node version (or tell us if you're using electron or some other framework):

v8.6.0

ShellJS version (the most recent version/Github branch you see the bug on):

"shelljs": "^0.8.0"

Operating system:

Macbook iOS high sierra 10.13.2

Description of the issue:

(New to Shelljs)
Shelljs exec command doesnot take variables. I tried all possible combinations like using double quotes, single quotes, assigning $ in front of the variable etc.

error : not found or adds all the help commands of the tool and prints out.

Need help please to understand the syntax on how to include variables inside exec function ? I might be not using the right syntax.
-Thanks

Example ShellJS command to reproduce the error:

Note filename: a.js

#!/usr/bin/env node
require('shelljs');
let output = exec('adb devices', {silent:false}).stdout;) // works perfectly fine

//Now if substitute dev in place of devices it doesn't work
let dev = "devices";
let output = exec('adb $dev', {silent:false}).stdout; //Doesn't work. errors out

//To execute the above file in the terminal, run :
node ./a.js

//note: adb is an android tool. you can use any simple command line like node --version etc

@nfischer
Copy link
Member

I think you're looking for something like:

var myvar = 'devices';
var output = shell.exec('adb ' + myvar);

@indra-Asurion
Copy link
Author

Thanks @nfischer. It works !!
Is there a way if the variable is in the middle like this:
The command is :

adb devices -l

shell.exec('adb ' + myvar + '-l'); // Throws error

I will also try to work on my end on how to concat the overall command if the variable is somewhere in the middle of the command. But if you get it first please let me know.

Thanks for the help again !!

@indra-Asurion
Copy link
Author

Found the Solution for the above issue:

shell.exec(adb ${myvar} -l) //This should work :)

@nfischer
Copy link
Member

shell.exec('adb ' + myvar + '-l'); // Throws error

Yeah, that would throw an error. That's because there's no space before -l. Your ES6 solution is fine (it adds the space). You can rewrite your ES6 solution in ES5 syntax too:

shell.exec('adb ' + myvar + ' -l'); // a space before '-l'

On a related note, if you need to access environmental variables, I would recommend accessing them via shell.env:

shell.echo(shell.env.PATH);
shell.cp('file.txt', shell.env.HOME); // can also use '~' instead of 'shell.env.HOME'
shell.exec('chown ' + shell.env.USER + ' file.txt');

@nfischer nfischer added wontfix question Question from a user (which may not require code/documentation changes to the project) labels Jan 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question from a user (which may not require code/documentation changes to the project) wontfix
Projects
None yet
Development

No branches or pull requests

2 participants