什么是cmd/powershell相当于bash上的`which`?

bas*_*ibe 36 powershell cmd which

我想找出CMD shell使用哪个版本的可执行文件.在任何unix shell中,我都会用which它来找到它.

其中一个Windows shell中是否有等效命令?

Joe*_*oey 69

多种.

  1. where 是直接的等价物:

    C:\Users\Joey>where cmd
    C:\Windows\System32\cmd.exe
    
    Run Code Online (Sandbox Code Playgroud)

    请注意,PowerShell where本身是别名Where-Object,因此您需要where.exe在PowerShell中使用.

  2. cmd你也可以使用for:

    C:\Users\Joey>for %x in (powershell.exe) do @echo %~$PATH:x
    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
    
    Run Code Online (Sandbox Code Playgroud)
  3. 在PowerShell 中,如果传递参数,则它具有相同的Get-Command别名gcm(但也适用于PowerShell中的别名,cmdlet和函数):

    PS C:\Users\Joey> Get-Command where
    
    CommandType     Name          Definition
    -----------     ----          ----------
    Alias           where         Where-Object
    Application     where.exe     C:\Windows\system32\where.exe
    
    Run Code Online (Sandbox Code Playgroud)

    第一个返回的命令是将要执行的命令.

  • 由于某种原因,`where`根本对我不起作用(什么都不打印然后立即退出),但是`gcm`效果很好。 (3认同)
  • @Hassan:正如您可以从PowerShell上的Get-Command where的示例输出中收集到的那样,where实际上是Where-Object的别名,它优先于where.exe。要运行“ where.exe”,必须输入“ where.exe”。至少在PowerShell中。 (2认同)

dbe*_*ham 5

WHERE命令与 unix 不太一样,which因为它列出了当前目录或 PATH 中找到的所有匹配文件。正如乔伊所说,列出的第一个是执行的那个。创建一个只返回找到的第一个脚本的批处理脚本很简单。

@echo off
for /f "delims=" %%F in ('where %1') do (
  echo %%F
  exit /b
)
Run Code Online (Sandbox Code Playgroud)

WHERE速度比较慢。

下面是一个 WHICH.BAT 脚本,它速度更快,功能更多。它使用广泛的延迟扩展切换,因为: 1) 如果存在未加引号的特殊字符,则扩展 %PATH% 是不可靠的。2) 在启用延迟扩展的情况下扩展 FOR 变量会损坏包含!.

::WHICH.BAT  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)

更新

我必须对上面的脚本进行重大修改,因为如果 PATH 中的某处碰巧存在具有相同根名称的 exe 文件,它会错误地将内部命令列为外部命令。