Massimiliano
Massimiliano

Reputation: 190

Is possible to set an adb command as a variable in batch?

i tried to set a variable equal to an adb command but it doesn't work, here is the code

set ten=10
set nine=9
set eight_1=8.1
set eight=8
set seven=7
set version = "adb shell getprop ro.build.version.release"
if %ten% == %version% goto :menu
if %nine% == %version% goto :menu
if %eight_1% == %version% goto :menu
if %eight% == %version% goto :menu
if %seven% == %version% goto :menu 

Can anyone help me to figure it out?

Upvotes: 0

Views: 2562

Answers (1)

Stephan
Stephan

Reputation: 56188

cmd is very simple. The set command just sets a variable to a string. There are two ways to catch the output of a command into a variable. One involves a temporary file. Writing the output to a file and re-read it:

adb shell getprop ro.build.version.release >file.tmp
<file.tmp set /p "var="
echo %var%

The other looks a bit more complicated (you'll get used to it) and doesn't use a file:

for /f "delims=" %%a in ('adb shell getprop ro.build.version.release') do set "var=%%a"
echo %var%

Both versions rely on a command output of one line only (though both can be expanded to iterate over several lines).

Notes:
Don't put spaces around the = with the set command - they become part of the variable name respectively of the value.

For best practice use the syntax set "var=value" (note where the quotes are). It prevents from stray trailing spaces (hard to spot when troubleshooting) and saves against some "poison characters"

For similar reasons, use the following if syntax: if "%ten%" == "%version%" goto :menu
Should a variable be empty or contain spaces, without the quotes you'll get a syntax error.

Upvotes: 2

Related Questions