简体   繁体   English

如何通过命令行自动将变量的值传递给批处理或 shell 脚本文件(在 Jenkins 中使用)

[英]How to pass the value of variables to batch or shell script files automatically via command line (To use in Jenkins)

I got a batch file that asks for the user input.我得到了一个要求用户输入的批处理文件。 The batch script prod_release.bat is like:批处理脚本prod_release.bat如下:

set /p CH1=Select the build option: 
if '%CH1%'=='1' goto SELECT_APP_L
if '%CH1%'=='2' goto SELECT_APP_M
if '%CH1%'=='3' goto SELECT_APP_H

if '%CH1%'!='1' && '%CH1%'!='2' && '%CH1%'!='3' goto START

I need to give the CH1 value from the command line so the script will automatically take it.我需要从命令行提供 CH1 值,以便脚本自动获取它。

I tried as我试过

set CH1=2 && prod_release.bat

But still, it asks for the user input and waits till it is given.但是,它仍然要求用户输入并等待它给出。

Can you please help me with this你能帮我解决这个问题吗

They can't change this file since they are using this for other purposes.他们无法更改此文件,因为他们将其用于其他目的。

I can just use this in my application but cannot edit this.我可以在我的应用程序中使用它,但不能编辑它。 I need help on similar like Powershell and shell scripts too.我也需要类似 Powershell 和 shell 脚本的帮助。

I want to run this bat script via Jenkins (or) add to another bat or python application to run with my desired inputs without asking for the user interaction.我想通过 Jenkins 运行这个 bat 脚本(或)添加到另一个 bat 或 python 应用程序,以便在不要求用户交互的情况下使用我想要的输入运行。

I can't change the batch file.我无法更改批处理文件。 Think of it as I can only call it from another batch file.想一想,因为我只能从另一个批处理文件中调用它。 For example, I have to call this batch file from Jenkins bat"prod_release.bat" then it has to run without waiting for the choice.例如,我必须从 Jenkins bat"prod_release.bat" 调用这个批处理文件,然后它必须运行而不等待选择。

Instead of setting CH1=Select the build option: you can set the CH1=%1 and pass the value while executing the file like this而不是设置 CH1=选择构建选项:您可以设置 CH1=%1 并在执行文件时传递值,如下所示

set /p CH1=%1
if '%CH1%'=='1' goto SELECT_APP_L
if '%CH1%'=='2' goto SELECT_APP_M
if '%CH1%'=='3' goto SELECT_APP_H

prod_release.bat 2 prod_release.bat 2

Now when file executes value 2 will be assign to CH1 variable.现在,当文件执行时,值 2 将分配给 CH1 变量。 In the same way you can pass whatever value you required for CH1 while file execution example以同样的方式,您可以在文件执行示例中传递 CH1 所需的任何值

prod_release.bat 1 prod_release.bat 1

A choice prompt should be done with command CHOICE and not with set /P :应该使用命令CHOICE而不是set /P来完成选择提示:

:SelectBuildOption
%SystemRoot%\System32\choice.exe /C 123 /N /M "Select the build option:"
if errorlevel 3 goto SELECT_APP_H
if errorlevel 2 goto SELECT_APP_M
if errorlevel 1 goto SELECT_APP_L
goto SelectBuildOption

Please read my answer on How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?请阅读我关于如何停止 Windows 命令解释器在用户输入不正确时退出批处理文件执行的答案? It explains in full details the usage of set /P and the usage of CHOICE and explains also the advantages and disadvantages of both solutions for prompting a user for something.它详细解释了set /P的用法和CHOICE的用法,并解释了这两种解决方案在提示用户某事方面的优缺点。

There can be used the following code above the label SelectBuildOption in the batch file prod_release.bat if the batch file is executed by Jenkins with an argument like 1 for build as defined by SELECT_APP_L or 2 for a build as defined by SELECT_APP_M or 3 for a build as defined by SELECT_APP_H .如果批处理文件prod_release.bat Jenkins执行,其参数类似于1用于SelectBuildOption定义的构建,或2用于SELECT_APP_L定义的SELECT_APP_M ,或3用于 a按照SELECT_APP_H的定义构建。

if "%~1" == "1" goto SELECT_APP_L
if "%~1" == "2" goto SELECT_APP_M
if "%~1" == "3" goto SELECT_APP_H

It would be of course also possible to use other, more meaningful strings than 1 and 2 and 3 for the optional build option like:当然,对于可选的构建选项,也可以使用比12 3意义的字符串,例如:

if /I "%~1" == "APP_L" goto SELECT_APP_L
if /I "%~1" == "APP_M" goto SELECT_APP_M
if /I "%~1" == "APP_H" goto SELECT_APP_H

With that code the batch file can be called by Jenkins with APP_L or APP_M or APP_H (case-insensitive) without or with being enclosed in " .使用该代码,批处理文件可以由 Jenkins 调用,带有APP_LAPP_MAPP_H (不区分大小写),不带或带括在"中。

The code below the three IF conditions which is the code below the label SelectBuildOption is executed by cmd.exe if none of the three conditions is true because of batch file is called without any argument or the first argument is not equal with one of the three compared strings.如果三个条件都不为cmd.exe ,因为调用SelectBuildOption文件时没有任何参数,或者第一个参数不等于三个条件之一比较字符串。

Run call /?运行call /? in a command prompt to get output the usage help of the command CALL explaining how to reference batch file arguments.命令提示符中获取 output 命令CALL的用法帮助,解释如何引用批处理文件 arguments。

I recommend to read also my answer on Symbol equivalent to NEQ, LSS, GTR, etc. in Windows batch files .我建议还阅读我对Windows 批处理文件中与 NEQ、LSS、GTR 等等效的符号的回答。 It explains in full details how a string comparison is done by the internal command IF of the Windows Command Processor cmd.exe .它详细解释了如何通过 Windows 命令处理器cmd.exe的内部命令IF完成字符串比较。

If the Jenkins job defines an environment variable for the build option to use, then just replace %~1 by %VariableName% on the three IF command lines and the batch file works also for usage by Jenkins with build option defined by the environment variable while the batch file can be still used for manual execution.如果 Jenkins 作业为要使用的构建选项定义了一个环境变量,那么只需在三个IF命令行上将%~1替换为%VariableName% ,该批处理文件也适用于 Jenkins 使用由环境变量定义的构建选项,而批处理文件仍可用于手动执行。

Example with batch file supporting optionally an environment variable BuildOption defined outside of the batch file by the parent process:批处理文件示例支持可选的环境变量BuildOption由父进程在批处理文件之外定义:

if not defined BuildOption goto SelectBuildOption
if /I "%BuildOption%" == "APP_L" goto SELECT_APP_L
if /I "%BuildOption%" == "APP_M" goto SELECT_APP_M
if /I "%BuildOption%" == "APP_H" goto SELECT_APP_H

It is common practice that scripts used for build processes support optionally either arguments to control the build process or one or more environment variables for automated builds while prompting the user of the script for the necessary options controlling the build process on using the script without the optional argument(s) or without the optional environment variables.通常的做法是,用于构建过程的脚本可选地支持 arguments 来控制构建过程或一个或多个环境变量用于自动构建,同时提示脚本用户提供控制构建过程的必要选项参数或没有可选的环境变量。

So I strongly recommend to contact the author of the batch file and ask him to enhance the batch file prod_release.bat as suggested above for easy usage in a fully automated build process.因此,我强烈建议联系批处理文件的作者,并要求他按照上面的建议增强批处理文件prod_release.bat ,以便在全自动构建过程中轻松使用。 The execution behavior for manual usage does not change with this modifications which is important in your environment.手动使用的执行行为不会随着此修改而改变,这在您的环境中很重要。

Another possibility would be to output the answer for the prompt and redirect this output from stderr to stdin of the batch file.另一种可能性是 output 提示的答案并将此 output 从stderr重定向到批处理文件的标准stdin

Example:例子:

(echo 1)|prod_release.bat

This command line lets cmd.exe redirect the string with the bytes 0x31 0x0D 0x0A ( 1 as ASCII character + carriage return + line-feed) to standard input stream of the batch file which is read by first set /P or choice in the batch file from the input stream and therefore results in an automatic answer of the first prompt with (hopefully) an acceptable choice.此命令行让cmd.exe将带有字节 0x31 0x0D 0x0A( 1作为 ASCII 字符 + 回车 + 换行符)的字符串重定向到批处理文件的标准输入 stream,该批处理文件由第一set /Pchoice读取来自输入 stream 的文件,因此会导致第一个提示的自动回答(希望)是可接受的选择。

But such an approach is very uncommon for scripts used in build processes.但是这种方法对于构建过程中使用的脚本来说非常少见。 A small modification of the called script file like running an executable or command reading first from the input stream could result with this workaround solution in a wrong execution of the script and results therefore again in a not working automated build process.对调用的脚本文件进行小的修改,例如运行可执行文件或首先从输入 stream 读取的命令,可能会导致此变通解决方案导致脚本执行错误,并因此再次导致无法正常工作的自动构建过程。 This workaround solution is for that reason really awful in comparison to making the enhancement of the script to support optional arguments or optional environment variables to define the necessary build options by the parent process instead of always prompting a user for the necessary build options.出于这个原因,与增强脚本以支持可选的 arguments 或可选的环境变量以通过父进程定义必要的构建选项而不是总是提示用户提供必要的构建选项相比,这种变通解决方案非常糟糕。

A good coded script used for a build process supports optional arguments or optional environment variables defined outside of the script to control the options for the build process and prompts for these options if the script is executed without the argument(s) or without the environment variable(s) defined by the parent process, ie when the script is executed by a user.用于构建过程的良好编码脚本支持可选 arguments 或在脚本外部定义的可选环境变量,以控制构建过程的选项,并在脚本在没有参数或没有环境变量的情况下执行时提示这些选项(s) 由父进程定义,即当脚本由用户执行时。

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

相关问题 如何将shell变量作为命令行参数传递给shell脚本 - How to pass shell variables as Command Line Argument to a shell script 通过 shell 脚本中的命令行参数将参数传递给 sql 文件 - Pass arguments to a sql file via command line arguments in shell script 将 jenkins 管道中设置的变量传递给 shell 脚本 - To pass variables set in jenkins pipeline to shell script 如何在shell脚本中集成Spring批处理命令行调用 - How to integrate Spring batch command line invocation in a shell script 如何在bash shell脚本的perl命令调用中使用shell变量? - How to use shell variables in perl command call in a bash shell script? 如何执行任意的Shell脚本并通过Python传递多个变量? - How to execute an arbitrary shell script and pass multiple variables via Python? 如何在Shell脚本中批处理文件? - How to batch files in shell script? 如何通过shell脚本中的命令行在Expect中传递参数 - How to pass argument in Expect through the command line in a shell script 如何在shell脚本中使用命令行参数传递参数 - How to pass parameters using command line arguments in shell script 如何将命令行参数传递给spark-shell scala脚本? - How to pass the command line argument to spark-shell scala script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM