bash regex命令在windows cmd脚本中等效

时间:2014-09-01 14:49:47

标签: windows command

这个bash命令的等价物是什么:

[[ $(uname) =~ NT-6.3 ]] && set_win81

在windows cmd脚本中?

我正在跳跃使用类似的东西:

if (ver | findstr /C:"Microsoft Windows [Version 6.3") ( goto :set_win81 )

3 个答案:

答案 0 :(得分:2)

这样的东西就足够了。

ver | findstr /i /c:"Version 6.3" >nul && goto :set_win81

以下是我为此任务而烦恼的一些代码:

@echo off
set "windows="
VER | find  " 4.1." > nul && set windows=W98
VER | find  " 4.0." > nul && set windows=NT
VER | find  " 5.0." > nul && set windows=W2K
VER | find  " 5.1." > nul && set windows=XP
VER | find  " 5.2." > nul && set windows=XP 64-Bit or Server 2003 or Server 2003 R2 
VER | find  " 6.0." > nul && set windows=Vista or server 2008
VER | find  " 6.1." > nul && set windows=Win7 or server 2008 R2
VER | find  " 6.2." > nul && set windows=Windows 8
VER | find  " 6.3." > nul && set windows=Server 2012 R2 or Windows 8.1
if defined windows (
echo %windows%
) else (
echo unknown operating system
)
pause

答案 1 :(得分:2)

作为解析ver输出的替代方法,您可以使用注册表:

setlocal 

set reg_info=reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v CurrentVersion
for /f "skip=2 tokens=3" %%l in ('%reg_info%') do set win_ver=%%l

if "%win_ver%" equ "6.2" goto :set_win80
if "%win_ver%" equ "6.3" goto :set_win81

REM etc etc

关键是你可以使用带有一些设置的for /f循环来解析多行,多列数据,例如reg query的输出。

答案 2 :(得分:1)

@echo off
setlocal
:: Get windows Version numbers
set ver=""
set con="Microsoft Windows Version 6.3"
for /f "tokens=2 delims=[]" %%G in ('ver') do set ver=%%G
echo found version %ver%
if %%ver EQU %%con (
goto set_win81)

echo NO win81
exit/B

:set_win81 
echo win81
exit/B