如何解决Windows批处理脚本中的嵌套条件
我编写了一个Windows批处理脚本py.bat,该脚本应该运行Python脚本,如有必要,在脚本名称中添加.py扩展名,并传递0、1或2个命令行参数。令人惊讶的是,零参数和单参数情况被正确处理,而二参数情况却没有被正确处理。如有任何建议,我将不胜感激。
@ECHO OFF
REM This script runs a Python script,adding the .py extension to the script name if necessary,REM and passing 0,1,or 2 command-line arguments.
:Step1
If EXIST "%1%" (
set file=%1%
GOTO Step2
)
If EXIST "%1%.py" (
set file=%1%.py
GOTO Step2
)
echo Script '%1%' not found!
GOTO End
:Step2
if "%2" EQU "" (
call python %file%
) else (
if "%3%" EQU "" (
call python %file% %2%
) else (
call python %file% %2% %3%
)
)
:End
我有一个名为count_args.py的简单Python脚本,它报告命令行参数的数量,并以两种方式运行它-直接和通过py.bat。以下I / O显示批处理脚本不会传递多个命令行参数。
(py38) C:\Phillip\Computer\Python\Code3>python count_args.py
Total number of command-line arguments: 0
(py38) C:\Phillip\Computer\Python\Code3>python count_args.py a
Total number of command-line arguments: 1
(py38) C:\Phillip\Computer\Python\Code3>python count_args.py a b
Total number of command-line arguments: 2
(py38) C:\Phillip\Computer\Python\Code3>py count_args
Total number of command-line arguments: 0
(py38) C:\Phillip\Computer\Python\Code3>py count_args a
Total number of command-line arguments: 1
(py38) C:\Phillip\Computer\Python\Code3>py count_args a b
Total number of command-line arguments: 1
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。