Skip to content

Commit

Permalink
升级版本1.02
Browse files Browse the repository at this point in the history
  • Loading branch information
PowerWordTree committed Feb 7, 2023
1 parent 3a92d74 commit 3d3baf9
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 123 deletions.
5 changes: 4 additions & 1 deletion History.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
1.00:
支持RUN、URL、SLEEP三种配置文件项。
1.01:
添加ECHO、RUNWAIT配置文件项。
添加ECHO、RUNWAIT配置文件项。
1.02:
添加PORT、ZOOKEEPER配置文件项。

205 changes: 102 additions & 103 deletions SequentialStartup.CMD
Original file line number Diff line number Diff line change
@@ -1,103 +1,102 @@
::顺序启动脚本
::@author FB
::@version 1.01

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
CD /D "%~dp0"
SET "PATH=%CD%\bin;%CD%\script;%PATH%"

::处理命令行参数
:: 参数1: 配置文件
IF EXIST "%~1" (
SET "CFGFILE=%~1"
) ELSE (
SET "CFGFILE=%~dpn0.cfg"
)

::开始执行
CALL :ECHO "========== 开始执行 " " =========="
FOR /F "eol=# tokens=1,* delims== usebackq" %%I IN ("%CFGFILE%") DO (
CALL :TRIM "VARNAME" "%%I"
CALL :TRIM "VARDATA" "%%J"
IF /I "!VARNAME!" == "RUN" (
CALL :ECHO "##" "运行命令: !VARDATA!"
CALL :GET_PATH "RUN_PATH" "!VARDATA!"
START "!VARDATA!" /D "!RUN_PATH!" !VARDATA!
SET "RUN_PATH="
)
IF /I "!VARNAME!" == "RUNWAIT" (
CALL :ECHO "##" "运行等待: !VARDATA!"
CALL :GET_PATH "RUN_PATH" "!VARDATA!"
START /WAIT "!VARDATA!" /D "!RUN_PATH!" !VARDATA!
SET "RUN_PATH="
)
IF /I "!VARNAME!" == "URL" (
CALL :ECHO "##" "检测网址: !VARDATA!"
CALL :CHECK_URL "!VARDATA!"
)
IF /I "!VARNAME!" == "SLEEP" (
CALL :ECHO "##" "等待时间: !VARDATA!"
CALL :SLEEP !VARDATA!
)
IF /I "!VARNAME!" == "ECHO" (
ECHO.!VARDATA!
)
)

::执行结束
CALL :ECHO "========== 执行结束 " " =========="
GOTO :EOF


::检查网址
:: 参数1: 网址
:CHECK_URL
FOR /F "tokens=*" %%I IN ('curl -I -m 10 -o NUL -s -w %%{http_code} "%~1"') DO (
IF NOT "%%I" == "200" IF NOT "%%I" == "302" (
CALL :SLEEP 3
GOTO :CHECK_URL
)
)
GOTO :EOF

::延迟
:: 参数1: 时间(秒)
:SLEEP
IF "%~1" == "" GOTO :EOF
CHOICE /T "%~1" /D "Y" 1>NUL 2>NUL
SET "SLEEPPARAM="
GOTO :EOF

::包含时间的输出
:: 参数1: 前缀文字
:: 参数2: 后缀文字
:ECHO
@ECHO %~1 %DATE% %TIME% %~2
GOTO :EOF

::去空格
:: 参数1: 输出到变量名
:: 参数2: 目标字符串
:TRIM
SET "TRIMSTR=%~2"
:TRIMLEFT
IF "%TRIMSTR:~0,1%" == " " (
SET "TRIMSTR=%TRIMSTR:~1%"
GOTO :TRIMLEFT
)
:TRIMRIGHT
IF "%TRIMSTR:~-1%" == " " (
SET "TRIMSTR=%TRIMSTR:~0,-1%"
GOTO :TRIMLEFT
)
SET "%~1=%TRIMSTR%"
SET "TRIMSTR="
GOTO :EOF

::截取文件路径
:: 参数1: 保存到变量名
:: 参数2: 完整文件路径
:GET_PATH
SET "%~1=%~dp2"
GOTO :EOF
::顺序启动脚本
::@author FB
::@version 1.02

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
CD /D "%~dp0"
SET "PATH=%CD%\bin;%CD%\script;%PATH%"

::处理命令行参数
:: 参数1: 配置文件
IF EXIST "%~1" (
SET "CFGFILE=%~1"
) ELSE (
SET "CFGFILE=%~dpn0.cfg"
)

::开始执行
ECHO ========== 开始执行 !DATE! !TIME! ==========
FOR /F "eol=# tokens=1,* usebackq delims==" %%I IN ("%CFGFILE%") DO (
CALL Common.CMD TRIM %%I
SET "VARNAME=!$!"
CALL Common.CMD TRIM %%J
SET "VARDATA=!$!"
IF /I "!VARNAME!" == "RUN" (
ECHO ## !DATE! !TIME! 运行: !VARDATA!
CALL :RUN !VARDATA!
)
IF /I "!VARNAME!" == "RUNWAIT" (
ECHO ## !DATE! !TIME! 运行并等待: !VARDATA!
CALL :RUNWAIT !VARDATA!
)
IF /I "!VARNAME!" == "URL" (
ECHO ## !DATE! !TIME! 等待网址: !VARDATA!
CALL :URL "!VARDATA!"
)
IF /I "!VARNAME!" == "PORT" (
ECHO ## !DATE! !TIME! 等待端口: !VARDATA!
FOR /F "tokens=1,* delims=:" %%A IN ("!VARDATA!") DO CALL :PORT "%%A" "%%B"
)
IF /I "!VARNAME!" == "ZOOKEEPER" (
ECHO ## !DATE! !TIME! 等待Zookeeper: !VARDATA!
FOR /F "tokens=1,* delims=:" %%A IN ("!VARDATA!") DO CALL :ZOOKEEPER "%%A" "%%B"
)
IF /I "!VARNAME!" == "SLEEP" (
ECHO ## !DATE! !TIME! 等待: !VARDATA!
CALL Common.CMD SLEEP !VARDATA!
)
IF /I "!VARNAME!" == "ECHO" (
ECHO.!VARDATA!
)
)

::执行结束
ECHO ========== 执行结束 !DATE! !TIME! ==========
GOTO :EOF

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

::执行命令
:: 参数1: 命令
:RUNWAIT
CALL Common.CMD GET_PATH "%~1"
START "!VARDATA!" /WAIT /D "!$!" %*
GOTO :EOF

::执行命令并等待
:: 参数1: 命令
:RUN
CALL Common.CMD GET_PATH "%~1"
START "!VARDATA!" /D "!$!" %*
GOTO :EOF

::等待网址
:: 参数1: 网址
:URL
FOR /F "tokens=*" %%I IN ('curl -I -m 10 -o NUL -s -w %%{http_code} "%~1"') DO (
IF NOT "%%I" == "200" IF NOT "%%I" == "302" (
CALL Common.CMD SLEEP 3
GOTO :URL
)
)
GOTO :EOF

::等待端口
:: 参数1: 地址
:: 参数2: 端口
:PORT
nc.exe -z "%~1" "%~2" 1>NUL 2>&1
IF "%ERRORLEVEL%" == "0" GOTO :EOF
CHOICE /T 3 /D Y 1>NUL 2>&1
GOTO :PORT

::等待Zookeeper
:: 参数1: 地址
:: 参数2: 端口
:ZOOKEEPER
ECHO stat | nc.exe "%~1" "%~2" | find /I "Zookeeper" 1>NUL 2>&1
IF "%ERRORLEVEL%" == "0" GOTO :EOF
CHOICE /T 3 /D Y 1>NUL 2>&1
GOTO :ZOOKEEPER

14 changes: 9 additions & 5 deletions SequentialStartup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@
## 注意:注释符前不能有空格

# 运行命令
## RUN=xxx.cmd
## Run=xxx.cmd
# 运行等待
## RUNWAIT=xxx.cmd
## RunWait=xxx.cmd
# 检查并等待URL可以访问
## URL=https://www.baidu.com
## Url=https://www.baidu.com
# 检查并等待端口可以访问
## Port=localhost:8443
# 检查并等待Zookeeper可以访问
## Zookeeper=10.0.253.5:2181
# 等待时间(秒)
## SLEEP=12
## Sleep=12
# 显示信息
## ECHO=正在载入...
## Echo=正在载入...
#
# 脚本顺序解析配置文件
# 当遇到以上某一个元素时,执行对应操作
Expand Down
68 changes: 68 additions & 0 deletions script/Common.CMD
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
::通用子程序
::@author FB
::@version 0.1

@ECHO OFF
CALL :%*
GOTO :EOF

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

::去首尾空格
:: 参数*: 目标字符串
:: 返回$: 处理后的字符串
:TRIM
SET "$=%*"
GOTO :EOF

::获取完整路径
:: 参数1: 目标路径
:: 返回$: 完整路径
:FULL_PATH
SET "$=%~f1"
GOTO :EOF

::截取文件路径
:: 参数1: 文件路径
:: 返回$: 所在路径
:GET_PATH
SET "$=%~dp1"
GOTO :EOF


::获取字符串长度
:: 参数1: 字符串
:: 返回$: 长度
:GET_SIZE
SET "$STR=%~1#"
SET "$=0"
FOR %%I IN (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) DO (
IF NOT "!$STR:~%%I,1!" == "" (
SET /A "$+=%%I"
SET "$STR=!$STR:~%%I!"
)
)
SET "$STR="
GOTO :EOF

::判断字符串是否以指定字符串结尾
:: 参数1: 目标字符串
:: 参数2: 指定字符串
:: 返回$: TRUE/FALSE
:END_WITH
SET "$STR=%~1"
CALL :GET_SIZE "%~2"
IF /I "!$STR:~-%$%!" == "%~2" (
SET "$=TRUE"
) ELSE (
SET "$=FALSE"
)
SET "$STR="
GOTO :EOF

::延迟
:: 参数1: 时间(秒)
:SLEEP
SET /A "$=%~1 + 0"
CHOICE /T "%$%" /D "Y" 1>NUL 2>NUL
GOTO :EOF
7 changes: 0 additions & 7 deletions script/WaitPort.cmd

This file was deleted.

7 changes: 0 additions & 7 deletions script/WaitZookeeper.cmd

This file was deleted.

0 comments on commit 3d3baf9

Please sign in to comment.