一、window 環(huán)境腳本語法
- if 語句
Usage 1:
bat腳本中字符串不用加“”號(hào),如果添加后則許兩重雙引號(hào)才能相等
set computername=xyz
set dropLoc=machine-abc
if "%computername%" == "xyz" (
set dropLoc=machine-xyz
)
echo %dropLoc%
Usage 2:
goto debug表示調(diào)用debug函數(shù)
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啟動(dòng)腳本
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
對(duì)于xml文件的操作,讀取每一行內(nèi)容是不能使用延遲變量,否則文件中的!特殊字符將會(huì)被腳本移除,要保持原有空格必須在for中強(qiáng)制使用"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是指定下載路徑
獲取某個(gè)目錄下文件(包含子目錄)
@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表示不顯示文件的路徑名
檢查某個(gè)命令是否存在,例如檢查windows是否安裝了wget
WHERE wget >nul 2>nul
IF %ERRORLEVEL% EQU 0 (
ECHO scp found
) else (
GOTO :EOF
)
檢查服務(wù)
SC QUERY | FIND "nexus"
IF %ERRORLEVEL% EQU 0 (
echo stop
)else (
echo start
)
設(shè)置環(huán)境變量
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:=/!