加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows – 在`shift`之后使用`%*`

发布时间:2020-12-14 01:54:09 所属栏目:Windows 来源:网络整理
导读:据我所知,在 Windows批处理文件中,%*扩展为所有命令行参数,并且该移位会移动编号的命令行参数%1,%2等,但它不会更改%*的内容. 如果我想要一个反映移位效果的%*版本,我该怎么办?我明白我可以说移位后%1%2%3%4%5%6%7%8%9但是这似乎很愚蠢且有潜
据我所知,在 Windows批处理文件中,%*扩展为所有命令行参数,并且该移位会移动编号的命令行参数%1,%2等,但它不会更改%*的内容.

如果我想要一个反映移位效果的%*版本,我该怎么办?我明白我可以说移位后%1%2%3%4%5%6%7%8%9但是这似乎很愚蠢且有潜在危险,这限制了我固定数量的参数.

虽然这不是特定于python的问题,但我可能有必要理解我想要这种行为的原因是我必须编写一个批处理文件SelectPython.bat来预先配置某些环境变量,以便导航babel我有不同的Python发行版(你必须以某种方式设置%PYTHONHOME%,%PYTHONPATH%和%PATH%才能调用Python二进制文件并确信你会获得正确的发行版).我当前的脚本适用于设置这些变量,但我希望能够在一行中调用它和Python – 例如:

SelectPython C:Python35  pythonw.exe myscript.py arg1 arg2 arg3 ...

理想情况下,我希望我的批处理文件使用shift来“吃掉”第一个参数,相应地处理它并设置环境,然后自动链式执行由其余参数形成的字符串.原理类似于env在posix系统中包装命令的方式:

env FOO=1 echo $FOO     # wrap the `echo` command to be executed in the context of specified environment settings

到目前为止我有这个 – 最后一行是问题所在:

@echo off
set "LOC=%CD%
if not "%~1" == "" set "LOC=%~1
if exist "%LOC%python.exe" goto :Success

echo "python.exe not found in %LOC%"
goto :eof

:Success
:: Canonicalize the resulting path:
pushd %LOC%
set "LOC=%CD%
popd

:: Let Python know where its own files are:
set "PYTHONHOME=%LOC%
set "PYTHONPATH=%LOC%;%LOC%Libsite-packages

:: Put Python's location at the beginning of the system path if it's not there already:
echo "%PATH%" | findstr /i /b /c:"%PYTHONHOME%" > nul || set "PATH=%PYTHONHOME%;%PYTHONHOME%Scripts;%PATH%

:: Now execute the rest:
shift
if "%~1" == "" goto :eof
%1 %2 %3 %4 %5 %6 %7 %8 %9
:: This is unsatsifactory - what if there are more than 9 arguments?

更新:感谢Stephan我的工作解决方案现在有以下更改的结束部分:

:: Now execute the rest of the arguments,if any:
shift
if @%1 == @ goto :eof
set command=
:BuildCommand
if @%1 == @ goto :CommandFinished
set "command=%command% %1"
shift
goto :BuildCommand
:CommandFinished
%command%

解决方法

建立你自己的“%*”(我把它命名为%params%):

set "params="
:build
if @%1==@ goto :cont
shift
set "params=%params% %1"
goto :build
:cont
echo params are %params%

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读