在批处理脚本中实现“which”命令

Dan*_*nov 4 windows command-line cmd batch-file

我试图制作一个像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
Run Code Online (Sandbox Code Playgroud)

dbe*_*ham 5

好问题,而且出乎意料地难以解决。

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

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

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

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

  • 我依赖于 %temp% 文件夹中的一个空文件夹(如果它不存在,我会创建它)
  • 我担心命令注入,所以在我进行内部测试之前,我确认命令只包含字母。
  • (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
Run Code Online (Sandbox Code Playgroud)