简体   繁体   English

如何在Windows批处理文件中获取返回的值

[英]How to get the returned value in a Windows batch file

In Windows batch file I want the return value ie the apkVerison number. 在Windows批处理文件中,我需要返回值,即apkVerison号。 When I run gulp getVersion on the command line or inside a batch file it works fine. 当我在命令行或批处理文件中运行gulp getVersion时,它可以正常工作。 I want save the version number to a variable inside the batch file. 我想将版本号保存到批处理文件中的变量中。

gulpfile.js:
  gulp.task('getVersion', function () {
      var data = require('./package.json')
      console.log('Version = ' + data.apkDependencies.apkVersion)
      return data.apkDependencies.apkVersion;
    });

abc.bat:
    set x=gulp getVersion 
    echo %x%

The batch file does not work. 批处理文件不起作用。 echo just echos "gulp getVersion" 回声只是回声“ gulp getVersion”

Sorry I should have mentioned when I run gulp this is its output 对不起,我应该在运行gulp时提到它的输出

gulp getVersion
[22:16:44] Using gulpfile ~\CareWheelsCorp\CareWheels\gulpfile.js
[22:16:44] Starting 'getVersion'...
Version = 9.1.13
[22:16:44] Finished 'getVersion' after 1.91 ms

As you can see Version is hidden inside, I need that 9.1.13 number 如您所见,版本隐藏在里面,我需要9.1.13号

It works perfectly happily - just as you have asked it to do, to echo the contents of x . 就像您要求的那样,它可以完美地愉快地工作,以echox的内容。

If you want to execute the contents of x , you need 如果要执行 x的内容,则需要

%x%

and if you want to get the return value, you need 如果要获取返回值,则需要

for /f "delims=" %%r in ('%x%') do set "return=%%r"
echo %return%

(assuming that the executable responds with a result from stdout ) (假设可执行文件以stdout的结果作为响应)


Given the clarification, 经过澄清,

for /f "tokens=1*delims== " %%r in ('%x%') do if "%%r"=="Version" set "return=%%s"
echo %return%

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

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