0

I got a batch file that asks for the user input. The batch script prod_release.bat is like:

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.

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.

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.

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.

  • Batch files use %1 as first argument and %2 as second argument and so on. – Robert Cotterman Sep 23 '22 at 03:51
  • `!=` is invalid syntax in an `if`; you need ` neq ` (spaces either side of `neq` required) and the `&&` should not be used. – Magoo Sep 23 '22 at 03:54
  • I will provide your inputs on syntax improvement to the devoloper. But these didn't answer my question – oruganti sivamuralikrishna Sep 23 '22 at 04:30
  • If you had made the effort to read the usage information for the `IF` command, you'd have clearly noted the `NOT` option. Open a Command Prompt window, type `if /?` and press the `[ENTER]` key to see it. ```If NOT "%CH1%" == "1" IF NOT "%CH1%" == "2" IF NOT "%CH1%" == "3" GOTO START```. BTW, you cannot call the person who put together that `.bat` file a Developer, it is absolutely rubbish scripting. Please let them know, that I said so. – Compo Sep 23 '22 at 11:54
  • Thanks for the input. I will definitely forward the comments to them. – oruganti sivamuralikrishna Sep 24 '22 at 07:43

2 Answers2

0

Instead of setting CH1=Select the build option: you can set the CH1=%1 and pass the value while executing the file like this

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

Now when file executes value 2 will be assign to CH1 variable. In the same way you can pass whatever value you required for CH1 while file execution example

prod_release.bat 1

0

A choice prompt should be done with command CHOICE and not with 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? 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.

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.

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:

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 ".

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.

Run call /? in a command prompt to get output the usage help of the command CALL explaining how to reference batch file arguments.

I recommend to read also my answer on Symbol equivalent to NEQ, LSS, GTR, etc. in Windows batch files. It explains in full details how a string comparison is done by the internal command IF of the Windows Command Processor cmd.exe.

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.

Example with batch file supporting optionally an environment variable BuildOption defined outside of the batch file by the parent process:

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 one or more 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 variable(s).

So I strongly recommend to contact the author of the batch file and ask to enhance the batch file prod_release.bat as suggested above for easy usage in a fully automated build process. The execution behavior for manual usage does not change with thesse modifications which are important for the fully automated build by Jenkins.

Another possibility would be to output the answer for the prompt and redirect this output from stdout of cmd.exe to stdin of the batch file.

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.

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. 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.

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, i.e. when the script is executed by a user.

Mofi
  • 46,139
  • 17
  • 80
  • 143