Home > Software design >  How to pass the value of variables to batch or shell script files automatically via command line (To
How to pass the value of variables to batch or shell script files automatically via command line (To

Time:09-23

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.

CodePudding user response:

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

CodePudding user response:

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.

  • Related