linux - "type"命令在 Git Bash 上未按预期工作

标签 linux windows windows-8 windows-8.1 git-bash

在 Linux 中,type 命令返回给定文件在文件系统中的位置,如果它在当前文件夹或 $PATH 中。此功能也可以在 Windows 上通过 Git Bash 命令行程序使用。但是,我遇到了一个看起来很奇怪的极端情况,文件存在于 $PATH 中,但没有使用命令返回。

最近想买一台新电脑,于是查了一下将license key从一台电脑转移到另一台电脑的方法,为实际操作做准备。 The method I found提到了文件 slmgr.vbsslui.exe,它们都位于 C:/Windows\System32 文件夹中,该文件夹位于我的$PATH,与 Windows 计算机一样。但是,当我使用 type 命令时,这两个文件没有出现。此外,当我在 Git Bash 中调用它时,slmgr.vbs 会被执行,而不是 slui.exe。 (slmgr.vbs 无法使用 Git Bash 执行正确,但我可以使用 cmd CLI 调用它来自 Git Bash。)最后,slmgr.vbsGit Bash 中列出文件夹的内容时也会显示,但是 slui .exe 不是。

我认为这可能与权限有关,事实上,这两个文件都具有非常严格的权限,如下图所示,但它们具有相同的权限,这无法解释为什么一个文件会被执行而另一个在直接调用时没有,也不是为什么一个文件在命令行上列出而另一个没有。

C:\Windows\System32 文件夹,证明文件存在:

enter image description here

两个文件的 UsersAdministrators 组的文件权限(它们是相同的):

enter image description here

和文件夹:

enter image description here

type 命令及其在 Git Bash 中的 2 个文件的输出,加上列出文件夹中的文件(使用 grep 过滤为文件夹很大),以及列出 $PATH 的一部分(请记住,在阅读时,Git Bash 会在路径显示时更改它们):

Sean@MYPC ~
$ type -a slmgr.vbs
sh.exe": type: slmgr.vbs: not found

Sean@MYPC ~
$ type -a slui.exe
sh.exe": type: slui.exe: not found

Sean@MYPC ~
$ slmgr.vbs
/c/WINDOWS/system32/slmgr.vbs: line 2: syntax error near unexpected token `('
/c/WINDOWS/system32/slmgr.vbs: line 2: `' Copyright (c) Microsoft Corporation. A
ll rights reserved.'

Sean@MYPC ~
$ slui.exe
sh.exe": slui.exe: command not found

Sean@MYPC ~
$ ls /c/Windows/System32/slui.exe /c/Windows/System32/slmgr.vbs
ls: /c/Windows/System32/slui.exe: No such file or directory
/c/Windows/System32/slmgr.vbs

Sean@MYPC ~
$ echo $PATH
/c/Users/Sean/bin:.:/usr/local/bin:/mingw/bin:/bin:/cmd:/c/Python33/:/c/Program
Files (x86)/Intel/iCLS Client/:/c/Program Files/Intel/iCLS Client/:/c/WINDOWS/sy
stem32:/c/WINDOWS:/c/WINDOWS/System32/Wbem:/c/WINDOWS/System32/WindowsPowerShell
/v1.0/:/c/Program Files/Intel/Intel(R) Management Engine Components/DAL:/c/Progr
am Files/Intel/Intel(R) Management Engine Components/IPT:/c/Program Files (x86)/
Intel/Intel(R) Management Engine Components/DAL:/c/Program Files (x86)/Intel/Int
el(R) Management Engine Components/IPT:/c/Program Files/Intel/WiFi/bin/:/c/Progr
am Files/Common Files/Intel/WirelessCommon/:/c/strawberry/c/bin:/c/strawberry/pe
rl/site/bin:/c/strawberry/perl/bin:/c/Program Files (x86)/Microsoft ASP.NET/ASP.
NET Web Pages/v1.0/:/c/Program Files/Microsoft SQL Server/110/Tools/Binn/:/c/Pro
gram Files (x86)/Microsoft SQL Server/90/Tools/binn/:/c/Program Files (x86)/Open
AFS/Common:/c/HashiCorp/Vagrant/bin:/c/Program Files (x86)/Windows Kits/8.1/Wind
ows Performance Toolkit/:/c/Program Files/nodejs/:/c/Program Files (x86)/Git/cmd
:/c/Program Files (x86)/Git/bin:/c/Program Files/Microsoft/Web Platform Installe
r/:/c/Ruby200-x64/bin:/c/Users/Sean/AppData/Local/Box/Box Edit/:/c/Program Files
 (x86)/SSH Communications Security/SSH Secure Shell:/c/Users/Sean/Documents/Lisp
:/c/Program Files/GCL-2.6.1/lib/gcl-2.6.1/unixport:/c/Chocolatey/bin:/c/Users/Se
an/AppData/Roaming/npm:/c/wamp/bin/mysql/mysql5.6.12/bin:/c/Program Files/Oracle
/VirtualBox:/c/Program Files/Java/jdk1.7.0_51/bin:/c/Program Files/Node-Growl:/c
/chocolatey/bin:/c/Program Files/eclipse:/c/MongoDB/bin:/c/Program Files/7-Zip:/
c/Program Files (x86)/Google/Chrome/Application:/c/Program Files (x86)/LibreOffi
ce 4/program:/c/Program Files (x86)/OpenOffice 4/program

这是怎么回事?为什么不使用 type 命令列出这些文件?这个问题是因为奇怪的 Windows 权限,还是更奇怪的原因?如果是权限,为什么它们看起来具有相同的权限,但两者的处理方式却不同?

编辑 我尝试了@RossRidge 的回答,将不可见的虚拟C:\Windows\Sysnative 文件夹放在我的$PATH 中。我的结果如下:

Sean@MYPC ~
$ type -a slmgr.vbs
sh.exe": type: slmgr.vbs: not found

Sean@MYPC ~
$ type -a slui.exe
slui.exe is /c/WINDOWS/Sysnative/slui.exe

Sean@MYPC ~
$ slmgr.vbs
/c/WINDOWS/system32/slmgr.vbs: line 2: syntax error near unexpected token `('
/c/WINDOWS/system32/slmgr.vbs: line 2: `' Copyright (c) Microsoft Corporation. A
ll rights reserved.'

Sean@MYPC ~
$ slui.exe

Sean@MYPC ~
$ ls /c/Windows | grep sysnative

Sean@MYPC ~
$ ls /c/Windows/sysnative | grep pla
AutoWorkplace.exe*
AutoWorkplace.exe.config
AutoWorkplaceN.dll*
DeviceDisplayStatusManager.dll*
Display.dll*
DisplaySwitch.exe*

如您所见,在列出 /c/Windows 的内容时并没有列出 Sysnative(同样,为简洁起见进行过滤),而是列出了/c/Windows/Sysnative 有效。在不指定路径的情况下执行 slmgr.vbsslui.exe 也可以(slmgr 仍然需要参数我故意不给它).然而,真正奇怪的部分是 type 命令现在将 slui.exe 列为位于 /c/Windows/Sysnative 文件夹中,但是 slmgr.vbs 仍然没有显示。为什么我可以执行它,但它仍然没有显示 type 命令?

最佳答案

您似乎遇到了 WOW64 重定向器。您需要的文件在 c:\windows\system32 中,但由于 shell 和其他实用程序是 32 位程序,它们会被重定向到 c:\windows\syswow64。解决您的问题的简单方法是从普通(64 位)Windows 命令行运行您想要的命令。

如果你真的想从 MSYS 环境运行这些命令,你可以运行命令 /c/windows/sysnative/slui 或添加 /c/windows/sysnative 到你的路径。您可能需要使用 cscript 命令来运行 VBScript 文件。

关于linux - "type"命令在 Git Bash 上未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25435576/

相关文章:

python - E : Package 'python-pip' has no installation candidate

linux - 追加文件的文本

linux - 如何从终端使用正则表达式替换

windows-8 - 数据绑定(bind)到 Page.BottomAppBar 中的 ElementName

通过 PuTTy 运行 Nvim-R : setting up r_term_cdm

windows - Tensorflow 导入错误 (Windows10) (python3.5.3) (tensorflow-gpu #243 nightly binary)

windows - 在 x64 中第二次 RichEdit 初始化后崩溃

windows - 玩耍/学习——QEMU(适用于 ARM)、Angstrom Linux(或 Debian)

javascript - 在后台播放音频 (Windows 8)

visual-c++ - 为什么在 WIN8 下使用 Touch Injection API 时只能注入(inject)一次触摸?