gpt4 book ai didi

windows - 在批处理脚本中执行 'which'命令

转载 作者:行者123 更新时间:2023-12-02 11:45:05 30 4
gpt4 key购买 nike

我尝试制作一个像 which 一样工作的脚本。所以我想检查这个命令是内部命令还是外部命令或两者兼而有之。我尝试执行程序,但是当程序(例如 .exe)不正确时,我的程序会因错误而退出,因此我尝试为该程序调用 help 。但现在,如果我尝试检查例如 echo 并且当前目录中有 echo.exe,我会发现它是外部命令,但不是内部命令。所以我的问题是如何正确地做到这一点。如何检查该程序是内部程序还是外部程序。谢谢。这是我的代码:

@echo off
setlocal enabledelayedexpansion
if "%1"=="" goto help

:start
if not "%1"=="" (
shift
if "%1"=="/?" goto help
goto :start
)
set arg=%*

for %%a in ("%pathext:;=" "%") do (
echo %arg%|findstr /E /I %%a >nul
rem If file was given with extension
if not ERRORLEVEL 1 goto with_rashr
)

set ext=0
for %%a in ("%pathext:;=" "%") do (
if EXIST "%CD%\!arg!%%~a" (
echo This is an external command: %CD%\!arg!%%~a
set ext=1
goto :internal
)
)

for %%G in ("%path:;=" "%") do (
for %%a in ("%pathext:;=" "%") do (
if EXIST "%%~G\!arg!%%~a" (
echo This is an external command: %%~G\!arg!%%~a
set ext=1
goto :internal
)
)
)
goto :internal

:with_rashr
echo Command with extension was given
if EXIST "%CD%\!arg!" (
echo This is an external command: %CD%\!arg!
set ext=1
goto :internal
)
for %%G in ("%path:;=" "%") do (
if EXIST "%%~G\!arg!" (
echo This is an external command: %%~G\!arg!
set ext=1
goto :internal
)
)

:internal
set PATH=
rem set PATH=%PATH%;%CD%
help %arg% >nul 2>&1
rem set error_check=%ERRORLEVEL%
rem echo %error_check%
rem %arg% /?
rem ERRORLEVEL 9009 when a batch attempts to execute a program that is not found.
rem echo %ext%
if ERRORLEVEL 9009 (
echo We couldn't execute command
if "%ext%"=="0" (
echo This is not a command
)
endlocal
goto :EOF
)
if "%ext%"=="1" (
if ERRORLEVEL 0 (
echo This is also an internal command
)
endlocal
goto :EOF
)
echo This is internal command

endlocal
goto :EOF

:help
echo Like which
echo Shows if this command external or internal

最佳答案

好问题,但解决起来出奇的困难。

我认为我有一个很好的工作版本 http://ss64.org/viewtopic.php?pid=5752#p5752 ,其中我回应了另一个人尝试做基本上相同的事情。但是在读完你的问题后,我意识到我的旧“解决方案”也遇到了同样的问题 - 如果路径中的某处碰巧存在具有相同根名称的 exe,它会错误地将内部命令报告为外部命令。

我首先检查是否可以找到外部命令,如果没有,那么我假设 HELP 知道的任何命令都是内部命令。

我想我现在有了一个工作版本。

我首先使用与您尝试过的类似技术检查该命令是否是内部命令。我添加了一些曲折。

  • 我依赖于 %temp% 文件夹中的一个空文件夹(如果它不存在,我会创建它)
  • 我担心命令注入(inject),因此在进行内部测试之前,我会验证命令是否只包含字母。
  • (CALL ) 命令是清除 ERRORLEVEL 的快速方法

据我所知,如果给出 /? 参数,每个内部命令都会打印出帮助信息。如果该命令不是内部命令,则它将无法执行并将 ERRORLEVEL 设置为 9009,因为 PATH 为空且当前目录为空。

::WHICH  CommandName  [ReturnVar]
::
:: Determines the full path of the file that would execute if
:: CommandName were executed.
::
:: The result is stored in variable ReturnVar, or else it is
:: echoed to stdout if ReturnVar is not specified.
::
:: If no file is found, then an error message is echoed to stderr.
::
:: The ERRORLEVEL is set to one of the following values
:: 0 - Success: A matching file was found
:: 1 - CommandName is an internal command
:: 2 - No file was found and CommandName is not an internal command
:: 3 - Improper syntax - no CommandName specified
::
@echo off
setlocal disableDelayedExpansion

set "file=%~1"
setlocal enableDelayedExpansion

if not defined file (
>&2 echo Syntax error: No CommandName specified
exit /b 3
)


:: test for internal command
echo(!file!|findstr /i "[^abcdefghijklmnopqrstuvwxyz]" >nul || (
set "empty=!temp!\emptyFolder"
md "!empty!" 2>nul
del /q "!empty!\*" 2>nul >nul
setlocal
pushd "!empty!"
set path=
(call )
!file! /? >nul 2>nul
if not errorlevel 9009 (
>&2 echo "!file!" is an internal command
popd
exit /b 1
)
popd
endlocal
)


:: test for external command
set "noExt="
if "%~x1" neq "" if "!PATHEXT:%~x1=!" neq "!PATHEXT!" set noExt="";
set "modpath=.\;!PATH!"
@for %%E in (%noExt%%PATHEXT%) do @for %%F in ("!file!%%~E") do (
setlocal disableDelayedExpansion
if not "%%~$modpath:F"=="" if not exist "%%~$modpath:F\" (
endlocal & endlocal & endlocal
if "%~2"=="" (echo %%~$modpath:F) else set "%~2=%%~$modpath:F"
exit /b 0
)
endlocal
)
endlocal


>&2 echo "%~1" is not a valid command
exit /b 2

关于windows - 在批处理脚本中执行 'which'命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21142715/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com