“type” Command Not Working As Expected on Git Bash

三世轮回 提交于 2019-12-11 16:35:29

问题


The type command, in Linux, returns the location, on the filesystem, of the given file, if it is in the current folder or the $PATH. This functionality is also available on Windows with the Git Bash command line program. However, I have run into what seems like a strange corner case where the file exists on the $PATH but doesn't get returned using the command.

I am thinking of buying a new computer soon, so I looked up the method of transferring the license key from one computer to another, in preparation for actually doing this. The method I found mentioned the files slmgr.vbs and slui.exe, both of which reside in the C:/Windows\System32 folder, which is in my $PATH, as usual for a Windows computer. However, these two files aren't showing up when I use the type command. Also, slmgr.vbs gets executed when I call it in Git Bash, but not slui.exe. (slmgr.vbs can't execute correctly with Git Bash, but I can call it with the cmd CLI from Git Bash.) Finally, slmgr.vbs is shown when listing the folder's contents in Git Bash, as well, but slui.exe isn't.

I thought this might have to do with permissions, and, indeed, both files have very restrictive permissions, as you can see in the pictures below, but they both have the same permissions, which wouldn't explain why one gets executed and the other doesn't when called directly, nor why one file is listed on command line but the other isn't.

C:\Windows\System32 folder, proving the files exist:

File permissions for the Users and Administrators groups for the two files (they are identical):

And the folder:

type command and its output in Git Bash for the 2 files, plus listing the files in the folder (using grep to filter as the folder is huge), as well as listing part of the $PATH (keep in mind, while reading, that Git Bash changes the paths as they are displayed):

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

What's happening? Why aren't these files listed with the type command? Is this issue because of weird Windows permissions, or something even weirder? If permissions, why do they seem to have the same permissions, yet both are not handled in the same way?

EDIT I tried @RossRidge's answer, putting the invisible, virtual C:\Windows\Sysnative folder in my $PATH. My results are below:

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*

As you can see, the Sysnative is not listed when listing the contents of the /c/Windows (again, filtering for brevity), but listing the contents of /c/Windows/Sysnative works. Executing both slmgr.vbs and slui.exe without specifying a path to them also works (slmgr still needs arguments I'm intentionally not giving it). The really strange part, however, is that the type command now lists slui.exe as being in the /c/Windows/Sysnative folder, but slmgr.vbs is still not shown. Why am I able to execute it, yet it still does not show up with the type command?


回答1:


It looks like you've run into the WOW64 redirector. The file you want is in c:\windows\system32 but as the shell and other utilities are 32-bit programs they get redirected to c:\windows\syswow64. The simple solution to your problem is to run the command you want from the normal (64-bit) Windows command line.

If you really want to run these commands from the MSYS environment you can either run the command /c/windows/sysnative/slui or add /c/windows/sysnative to your path. You may need to use the cscript command to run the VBScript file.



来源:https://stackoverflow.com/questions/25435576/type-command-not-working-as-expected-on-git-bash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!