PowerShell의 *Nix 'which' 명령어와 동등합니까?
PowerShell에 문의하려면 어떻게 해야 하나요?
예를 들어 "which notepad"를 입력하면 메모장이 있는 디렉토리가 반환됩니다.exe는 현재 경로에 따라 실행됩니다.
PowerShell에서 프로파일을 커스터마이즈하기 시작한 후 처음 만든 별칭은 'which'였습니다.
New-Alias which get-command
이것을 프로파일에 추가하려면 , 다음과 같이 입력합니다.
"`nNew-Alias which get-command" | add-content $profile
마지막 줄의 선두에 있는n은 새 줄에서 시작됨을 확인하는 것입니다.
다음은 실제 *nix 등가입니다. 즉, *nix 스타일의 출력을 제공합니다.
Get-Command <your command> | Select-Object -ExpandProperty Definition
원하는 것으로 대체하세요.
PS C:\> Get-Command notepad.exe | Select-Object -ExpandProperty Definition
C:\Windows\system32\notepad.exe
파이프와 함께 별칭을 사용할 수 없으므로 프로필에 추가할 때 별칭이 아닌 함수를 사용할 수 있습니다.
function which($name)
{
Get-Command $name | Select-Object -ExpandProperty Definition
}
프로파일을 새로고침하면 다음과 같이 할 수 있습니다.
PS C:\> which notepad
C:\Windows\system32\notepad.exe
보통 타이핑만 합니다.
gcm notepad
또는
gcm note*
gcm는 Get-Command의 기본 별칭입니다.
시스템에서 gcm note* 출력:
[27] » gcm note*
CommandType Name Definition
----------- ---- ----------
Application notepad.exe C:\WINDOWS\notepad.exe
Application notepad.exe C:\WINDOWS\system32\notepad.exe
Application Notepad2.exe C:\Utils\Notepad2.exe
Application Notepad2.ini C:\Utils\Notepad2.ini
디렉토리와 원하는 명령어가 표시됩니다.
다음의 예를 시험해 보겠습니다.
(Get-Command notepad.exe).Path
Which 함수에 대한 나의 제안:
function which($cmd) { get-command $cmd | % { $_.Path } }
PS C:\> which devcon
C:\local\code\bin\devcon.exe
와의 퀵 앤 which
하고 있다
New-Alias which where.exe
그러나 여러 행이 존재하는 경우 반환되므로 다음과 같이 됩니다.
function which {where.exe command | select -first 1}
는 아요를 좋아한다.Get-Command | Format-List
줄여서 에 대해서만 사용합니다.powershell.exe
:
gcm powershell | fl
다음과 같은 에일리어스를 찾을 수 있습니다.
alias -definition Format-List
은 하다, 하다, 하다, 하다와 할 수 있습니다.gcm
.
탭에 모든 옵션이 한 번에 나열되도록 하려면:
set-psreadlineoption -editmode emacs
이것은 당신이 원하는 것을 할 수 있을 것 같습니다(http://huddledmasses.org/powershell-find-path/)에서 찾았습니다).
Function Find-Path($Path, [switch]$All = $false, [Microsoft.PowerShell.Commands.TestPathType]$type = "Any")
## You could comment out the function stuff and use it as a script instead, with this line:
#param($Path, [switch]$All = $false, [Microsoft.PowerShell.Commands.TestPathType]$type = "Any")
if($(Test-Path $Path -Type $type)) {
return $path
} else {
[string[]]$paths = @($pwd);
$paths += "$pwd;$env:path".split(";")
$paths = Join-Path $paths $(Split-Path $Path -leaf) | ? { Test-Path $_ -Type $type }
if($paths.Length -gt 0) {
if($All) {
return $paths;
} else {
return $paths[0]
}
}
}
throw "Couldn't find a matching path of type $type"
}
Set-Alias find Find-Path
이 PowerShell을 확인해 주세요.
여기에 제공된 코드는 다음을 나타냅니다.
($Env:Path).Split(";") | Get-ChildItem -filter notepad.exe
Windows 2003 이후(또는 Resource Kit 를 인스톨 하고 있는 경우는 Windows 2000/XP)에서, 커맨드를 시험합니다.
그런데 다른 질문에서 더 많은 답변을 받았습니다.
Windows에 'which'와 동등한 기능이 있습니까?
™ PowerShell과 한 PowerShellwhich
★★★★★★★★★★★★★★★★★?
파이프라인으로부터의 입력 또는 파라미터로서 양쪽 모두의 입력을 받아들이는 경우는, 다음의 조작을 실시할 필요가 있습니다.
function which($name) {
if ($name) { $input = $name }
Get-Command $input | Select-Object -ExpandProperty Path
}
profile('copy-commands')notepad $profile
를 참조해 주세요.
예:
❯ echo clang.exe | which
C:\Program Files\LLVM\bin\clang.exe
❯ which clang.exe
C:\Program Files\LLVM\bin\clang.exe
게 요.which
PowerShell:
function which {
<#
.SYNOPSIS
Identifies the source of a PowerShell command.
.DESCRIPTION
Identifies the source of a PowerShell command. External commands (Applications) are identified by the path to the executable
(which must be in the system PATH); cmdlets and functions are identified as such and the name of the module they are defined in
provided; aliases are expanded and the source of the alias definition is returned.
.INPUTS
No inputs; you cannot pipe data to this function.
.OUTPUTS
.PARAMETER Name
The name of the command to be identified.
.EXAMPLE
PS C:\Users\Smith\Documents> which Get-Command
Get-Command: Cmdlet in module Microsoft.PowerShell.Core
(Identifies type and source of command)
.EXAMPLE
PS C:\Users\Smith\Documents> which notepad
C:\WINDOWS\SYSTEM32\notepad.exe
(Indicates the full path of the executable)
#>
param(
[String]$name
)
$cmd = Get-Command $name
$redirect = $null
switch ($cmd.CommandType) {
"Alias" { "{0}: Alias for ({1})" -f $cmd.Name, (. { which $cmd.Definition } ) }
"Application" { $cmd.Source }
"Cmdlet" { "{0}: {1} {2}" -f $cmd.Name, $cmd.CommandType, (. { if ($cmd.Source.Length) { "in module {0}" -f $cmd.Source} else { "from unspecified source" } } ) }
"Function" { "{0}: {1} {2}" -f $cmd.Name, $cmd.CommandType, (. { if ($cmd.Source.Length) { "in module {0}" -f $cmd.Source} else { "from unspecified source" } } ) }
"Workflow" { "{0}: {1} {2}" -f $cmd.Name, $cmd.CommandType, (. { if ($cmd.Source.Length) { "in module {0}" -f $cmd.Source} else { "from unspecified source" } } ) }
"ExternalScript" { $cmd.Source }
default { $cmd }
}
}
용도:
function Which([string] $cmd) {
$path = (($Env:Path).Split(";") | Select -uniq | Where { $_.Length } | Where { Test-Path $_ } | Get-ChildItem -filter $cmd).FullName
if ($path) { $path.ToString() }
}
# Check if Chocolatey is installed
if (Which('cinst.bat')) {
Write-Host "yes"
} else {
Write-Host "no"
}
또는 원래 where 명령어를 호출하는 이 버전입니다.
또한 이 버전은 bat 파일에만 국한되지 않기 때문에 더 잘 작동합니다.
function which([string] $cmd) {
$where = iex $(Join-Path $env:SystemRoot "System32\where.exe $cmd 2>&1")
$first = $($where -split '[\r\n]')
if ($first.getType().BaseType.Name -eq 'Array') {
$first = $first[0]
}
if (Test-Path $first) {
$first
}
}
# Check if Curl is installed
if (which('curl')) {
echo 'yes'
} else {
echo 'no'
}
할 수 .which
다른 모든 UNIX 명령어와 함께 https://goprogram.co.uk/software/commands, 명령어를 사용합니다.
scoop이 있는 경우 다음과 같은 직접 클론을 설치할 수 있습니다.
scoop install which
which notepad
또, Windows powershell 로부터 액세스 하는 방법에는, 실제로 3개의 방법이 있습니다.첫 번째(꼭 최선은 아닙니다) wsl - e 명령어(이것에 의해, Linux 용 Windows 서브 시스템을 인스톨 해, distro 를 실행할 필요가 있습니다).B. gnuwin32는 .exe 형식의 여러 gnu 바이너리 포트로 standle one 번들 lanunchers 옵션 3을 사용하여 msys2(크로스 컴파일러 플랫폼)를 설치합니다.그것이 /usr/bin에 설치되어 있는 장소에 가면, 보다 최신의 많은 gnu 유틸리티가 있습니다.대부분은 독립 실행형 exe로 작동하며 bin 폴더에서 홈 드라이브로 복사하여 PATH에 추가할 수 있습니다.
또, Windows powershell 로부터 액세스 할 수 있는 방법은, 3가지가 있습니다.
- 첫 번째 (최선은 아니지만)는 wsl(linux용 Windows 서브시스템)입니다.
wsl -e which command
이를 위해서는 Linux용 Windows 서브시스템을 설치하고 distro를 실행해야 합니다.
다음으로 gnuwin32는 스탠드얼론 번들랜처로서 .exe 형식의 여러 gnu 바이너리 포트입니다
셋째, msys2(크로스 컴파일러 플랫폼)를 /usr/bin에 설치한 장소에 가면 보다 최신의 많은 gnu 유틸리티가 있습니다.대부분의 유틸리티는 독립 실행형 exe로 동작하며 bin 폴더에서 홈드라이브로 복사하여 PATH에 추가할 수 있습니다.
언급URL : https://stackoverflow.com/questions/63805/equivalent-of-nix-which-command-in-powershell
'programing' 카테고리의 다른 글
Excel로의 JSON Import (0) | 2023.04.14 |
---|---|
변수를 소스 없이 Bash 스크립트에서 환경으로 내보낼 수 있습니까? (0) | 2023.04.14 |
여러 하위 프로세스가 완료될 때까지 bash에서 기다렸다가 하위 프로세스가 코드 !=0으로 끝날 때 종료 코드 !=0을 반환하는 방법? (0) | 2023.04.14 |
UITextField 암호 숨기기 (0) | 2023.04.14 |
'git pull'과 'git fetch'의 차이점은 무엇입니까? (0) | 2023.04.09 |