我希望能够file
从内部调用命令cmd
。
这可行,但是输入起来很麻烦:
bash -c "file filename.ext"
这要容易得多,并且可以按预期工作......
DOSKEY file=bash -c "file $*"
file filename.ext
...除非文件名包含空格:
C:\> file "the filename.ext"
the: cannot open `the' (No such file or directory)
单引号没有帮助:
DOSKEY file=bash -c "file '$*'"
file "the filename.ext"
结果:
filename.ext': -c: line 0: unexpected EOF while looking for matching `''
filename.ext': -c: line 1: syntax error: unexpected end of file
引号也不会用引号转义:
DOSKEY file=bash -c "file ""$*"""
file "the filename.ext"
the: cannot open `the' (No such file or directory)
filename.ext: cannot open `filename.ext' (No such file or directory)
或者反斜杠:
DOSKEY file=bash -c "file \"$*\""
file "the filename.ext"
filename.ext": -c: line 0: unexpected EOF while looking for matching `"'
filename.ext": -c: line 1: syntax error: unexpected end of file
答案1
DOSKEY file=bash -c "file '$*'"
使用示例:
“通常”文件名:
file NoExtensionJpg
NoExtensionJpg: JPEG image data, Exif standard: [TIFF image data, big-endian, direntries=11, manufacturer=Apple, model=iPhone 6s, orientation=upper-left, xresolution=162, yresolution=170, resolutionunit=2, software=11.4, atetime=2019:06:01 11:20:54, GPS-Data], baseline, precision 8, 4032x3024, components 3
文件名包含空格:
file export - Copy.txt
export - Copy.txt: Windows Registry little-endian text (Win2K or above)
文件名包含cmd
-poisonous 字符:
file NUL&pause&exit
NUL&pause&exit: Non-ISO extended-ASCII text, with CRLF line terminators
答案2
如果您使用的是 cygwin 或 MSYS,那么file.exe
您可以直接从 cmd 调用。一些终端(如 MobaXterm 或 git bash)也在后台使用 MSYS 或 cygwin,因此您实际上不需要从 bash 调用
如果你正在使用 WSL,那么你需要将 Windows 路径转换为 WSL 路径,因为 Linux 子系统不理解 Windows 路径
DOSKEY file=bash -c 'file^ ^"$(wslpath^ ^"$*^")^"'
但使用批处理文件会简单得多。只需将下面的行保存到文件中,并将其放在 PATH 中的某个位置即可
@bash -c "file \"$(wslpath \"%~1\")\""