一、window 环境脚本语法
- if 语句
Usage 1:
bat脚本中字符串不用加“”号,如果添加后则许两重双引号才能相等
set computername=xyz
set dropLoc=machine-abc
if "%computername%" == "xyz" (
set dropLoc=machine-xyz
)
echo %dropLoc%
Usage 2:
goto debug表示调用debug函数
rem 这是注释
set DEBUG_OPTS=
if ""%1"" == ""debug"" (
set DEBUG_OPTS= -Xloggc:../logs/gc.log -verbose:gc -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=../logs
goto debug
)
springboot启动脚本
echo off
set APP_NAME=springboot-vue.jar
set CONFIG= -Dlogging.path=../logs -Dlogging.config=../config/log4j2.xml -Dspring.config.location=../config/application.yml
set DEBUG_OPTS=
if ""%1"" == ""debug"" (
set DEBUG_OPTS= -Xloggc:../logs/gc.log -verbose:gc -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=../logs
goto debug
)
set JMX_OPTS=
if ""%1"" == ""jmx"" (
set JMX_OPTS= -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9888 -Dcom.sun.management.jmxremote.ssl=FALSE -Dcom.sun.management.jmxremote.authenticate=FALSE
goto jmx
)
echo "Starting the %APP_NAME%"
java -Xms512m -Xmx512m -server %DEBUG_OPTS% %JMX_OPTS% %CONFIG% -jar ../lib/%APP_NAME%
goto end
:debug
echo "debug"
java -Xms512m -Xmx512m -server %DEBUG_OPTS% %CONFIG% -jar ../lib/%APP_NAME%
goto end
:jmx
java -Xms512m -Xmx512m -server %JMX_OPTS% %CONFIG% -jar ../lib/%APP_NAME%
goto end
:end
pause
bat处理替换xml文件中的字符
@echo off
set filename=mylove.xml
setlocal DisableDelayedExpansion
SET MyVar=../logs
set oldStr=logs
(for /F "delims=" %%G in (%filename%) do (
set "str=%%G"
setlocal EnableDelayedExpansion
set "strTrim={x}!str!{x}"
if "!strTrim!"=="{x}{x}" (
CALL :BlankLine
) ELSE (
set "str=!str:%oldStr%=%MyVar%!"
IF "!str!"=="" (
CALL :BlankLine
) ELSE (
set "strTrim=!strTrim: ={x}!"
set "strTrim=!strTrim: ={x}!"
set "strTrim=!strTrim:{x}{x}={x}!"
set "strTrim=!strTrim:{x}{x}={x}!"
:: I had to repeat the set "strTrim=!strTrim:{x}{x}={x}!" several times to get rid of all combinations of blanks - I could also have edited the template, of course...
IF "!strTrim!"=="{x}" (CALL :BlankLine) ELSE (ECHO !str!)
)
)
endlocal
)) > filename.xml
move "%filename%.tmp" "%filename%"
goto :eof
:BlankLine
ECHO(
goto :eof
在xml文件的字符行下新添加一行
setlocal enabledelayedexpansion
set linecount=0
set match_line=0;
(
FOR /F "delims=" %%A IN (d:\settings.xml) DO (
setlocal DisableDelayedExpansion
ECHO %%A
endlocal
IF "%%A" EQU " <localRepository>/path/to/local/repo</localRepository>" (
set /A match_line=!linecount!+1
)
if !linecount! equ !match_line! (
TYPE d:\Line_to_add.txt
echo.
)
set /a linecount=!linecount!+1
)
) >d:\temp.xml
move /y d:\temp.xml d:\settings.xml
对于xml文件的操作,读取每一行内容是不能使用延迟变量,否则文件中的!特殊字符将会被脚本移除,要保持原有空格必须在for中强制使用"delims="
batch中条用powershell下载文件
PowerShell.exe curl https://search.maven.org/remotecontent?filepath=kg/apc/jmeter-plugins-manager/0.20/jmeter-plugins-manager-0.20.jar -O jmeter-plugins-manager-0.20.jar
-O是指定下载路径
获取某个目录下文件(包含子目录)
@echo off
::
set num=0
setlocal enabledelayedexpansion
set conf_dir=../config
::匹配所有文件,匹配制定后缀文件可写为.properties,.xml
for /R "%conf_dir%" %%s in () do (
::set str=!str!,%conf_dir%/%%~nxs
set /a num+=1
IF !num! equ 1 (
set str= !str!%conf_dir%/%%~nxs
) ELSE (
set str= !str!,%conf_dir%/%%~nxs
)
)
echo %str%
pause
~nx表示不显示文件的路径名
检查某个命令是否存在,例如检查windows是否安装了wget
WHERE wget >nul 2>nul
IF %ERRORLEVEL% EQU 0 (
ECHO scp found
) else (
GOTO :EOF
)
检查服务
SC QUERY | FIND "nexus"
IF %ERRORLEVEL% EQU 0 (
echo stop
)else (
echo start
)
设置环境变量
set wget_home=D:\ProgramFiles\wget
set path_temp=%wget_home%;%Path%
set myPath=%wget_home%
For /F "Delims=" %%I In ('echo %Path% ^| find /C /I "%myPath%"') Do set pathExists=%%I 2>Nul
If %pathExists%==0 (
wmic ENVIRONMENT where "name='Path' and username='<system>'" set VariableValue="%path_temp%"
)else (
echo INFO: %myPath% exists in PATH
)
win7+文本替换,使用powershell
@echo off
set ffile='myfile.txt'
set fold='FOO'
set fnew='BAR'
powershell -Command "(gc %ffile%) -replace %fold%, %fnew% | Out-File %ffile% -encoding utf8"
替换字符串中的字符
将\替换成/
set final_config_location=!config_location:=/!