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



我曾尝试制作一个类似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%文件夹中的一个空文件夹(如果它不存在,我会创建它)
  • 我担心命令注入,所以在进行内部测试之前,我会验证命令只包含字母
  • (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

最新更新