简体   繁体   English

Shell.js exec()不执行变量

[英]Shelljs exec() not executing variables

Shelljs exec command doesnot take variables. Shelljs exec命令不接受变量。 I tried all possible combinations like using double quotes, single quotes, assigning $ in front of the variable etc. below is the script: 我尝试了所有可能的组合,例如使用双引号,单引号,在变量前面分配$等。下面是脚本:

#!/usr/bin/env node
require('shelljs');
for(let i = 0; i < data.length; i++)
{
  let dev = data[i];
  let platform_version = exec('adb -s $dev', {silent:false}).stdout;
}

error : dev not found or adds all the help commands of adb and prints out. 错误:找不到开发人员或添加了adb的所有帮助命令并打印出来。

can anyone please help ? 谁能帮忙吗? Thanks 谢谢

note: adb is an android tool. 注意:adb是一个android工具。 you can use any simple commandline like echo etc 您可以使用任何简单的命令行,例如echo等

You cannot do string interpolation in JavaScript like 'adb -s $dev' . 您无法在JavaScript中像'adb -s $dev'这样进行字符串插值。 The $dev variable will not be replaced by its value. $dev变量不会被其值替换。 You can use the + operator instead to form the string. 您可以使用+运算符来形成字符串。 See the code below. 请参见下面的代码。

let platform_version = exec('adb -s ' + dev, {silent:false}).stdout;

Note: In ECMAScript 6, you can use template literals to get similar result since they allow string interpolation. 注意:在ECMAScript 6中,您可以使用模板文字来获得相似的结果,因为它们允许字符串插值。 Template literals are written within ` (backticks). 模板文字写在` (反引号)内。 Which means the code `adb -s ${dev}` should also give the desired result. 这意味着代码`adb -s ${dev}`也应该给出期望的结果。

Using Template Literals we can solve the issue. 使用模板文字,我们可以解决问题。 But there is a small change in the variables. 但是变量有很小的变化。

let platform_version = exec(`adb -s ${dev}`, {silent:false}).stdout;

Need to use wrap the variable in curly braces to get the desired result. 需要使用大括号将变量包装起来以获得所需的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM