Windows上的Bash-exe文件的别名 [英] Bash on Windows - alias for exe files

查看:135
本文介绍了Windows上的Bash-exe文件的别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows上的Ubuntu上使用Bash,在Windows 10上运行bash的方式.我安装了Creators更新,而Ubuntu版本是16.04.

I am using the Bash on Ubuntu on Windows, the way to run bash on Windows 10. I have the Creators update installed and the Ubuntu version is 16.04.

我最近在玩npm,node.js和Docker,对于docker,我发现可以将其安装并在Windows中运行,而仅使用bash的客户端部分,直接从bash中调用docker.exe文件Windows的Program Files文件文件夹.我只是更新了path变量,将docker的路径包含为PATH=$PATH:~/mnt/e/Program\ Files/Docker/(放在.bashrc中),然后可以通过bash调用docker.exe来运行docker.

I was playing recently with things as npm, node.js and Docker and for docker I found it is possible to install it and run it in windows and just use the client part from bash, calling directly the docker.exe file from Windows's Program Files files folder. I just update my path variable to include the path to docker as PATH=$PATH:~/mnt/e/Program\ Files/Docker/ (put in .bashrc) and then I am able to run docker from bash calling docker.exe.

但是,我不想在命令(程序)末尾写.exe.我可以简单地添加一个别名alias docker="docker.exe",但是然后我想使用一个叫docker-compose的别名,我必须添加另一个别名.我当时正在考虑向.bashrc中添加一个脚本,该脚本将遍历路径变量并在该路径变量中指定的每个路径中搜索.exe文件,并为每次出现的事件添加一个别名,但这似乎不是一个很干净的解决方案(但我想它会很好地达到其目的).

But hey this bash and I dont want to write .exe at the end of the commands (programs). I can simply add an alias alias docker="docker.exe", but then I want to use lets say docker-compose and I have to add another one. I was thinking about adding a script to .bashrc that would go over path variable and search for .exe files in every path specified in the path variable and add an alias for every occurance, but it does not seem to be a very clean solution (but I guess it would serve its purpose quite well).

是否有简单且干净的解决方案来实现这一目标?

Is there a simple and clean solution to achieve this?

推荐答案

在尝试使用有很多现有的shell脚本,它们可以在Linux上很好地运行,并且大多数情况下也可以在WSL下运行,直到由于docker: command not found而失败.在任何地方将docker更改为docker.exe都将非常麻烦且不可移植.

Had plenty of existing shell scripts that run fine under Linux and mostly under WSL too until failing due to docker: command not found. Changing everywhere docker to docker.exe would be too cumbersome and non-portable.

使用~/.bashrc中的别名尝试了变通方法首先在此处:

Tried workaround with aliases in ~/.bashrc as here at first:

shopt -s expand_aliases
alias docker=docker.exe
alias docker-compose=docker-compose.exe

但是它要求每个脚本都必须在在反引号内不起作用无需修改脚本.

But it requires every script to be run in interactive mode and still doesn't work within backticks without script modification.

然后尝试导出 bash

Then tried exported bash functions in ~/.bashrc:

docker() { docker.exe "$@"; }
export -f docker
docker-compose() { docker-compose.exe "$@"; }
export -f docker-compose

这有效.但是添加每个所需的exe仍然很繁琐.

This works. But it's still too tedious to add every needed exe.

最后得到了一个更简单的 wslshim 自定义帮助程序脚本.

Finally ended up with easier symlinks approach and a modified wslshim custom helper script.

只需将一次添加到~/.local/bin/wslshim:

#!/bin/bash -x
cd ~/.local/bin && ln -s "`which $1.exe`" "$1" || ln -s "`which $1.ps1`" "$1" || ln -s "`which $1.cmd`" "$1" || ln -s "`which $1.bat`" "$1"

使其可执行:chmod +x ~/.local/bin/wslshim

然后添加任何别名"变得像键入两个单词一样容易:

Then adding any "alias" becomes as easy as typing two words:

$ wslshim docker
+ cd ~/.local/bin
++ which docker.exe
+ ln -s '/mnt/c/Program Files/Docker/Docker/resources/bin/docker.exe' docker

$ wslshim winrm
+ cd ~/.local/bin
++ which winrm.exe
+ ln -s '' winrm
ln: failed to create symbolic link 'winrm' -> '': No such file or directory
++ which winrm.ps1
+ ln -s '' winrm
ln: failed to create symbolic link 'winrm' -> '': No such file or directory
++ which winrm.cmd
+ ln -s /mnt/c/Windows/System32/winrm.cmd winrm

脚本自动选择绝对路径到$ PATH中的任何Windows可执行文件,并将其无符号扩展名链接到

The script auto picks up an absolute path to any windows executable in $PATH and symlinks it without extension into ~/.local/bin which also resides in $PATH on WSL.

可以根据需要轻松扩展此方法,以自动链接给定目录中的任何exe.但是将整个$ PATH链接起来将是一个过大的选择. )

This approach can be easily extended further to auto link any exe in a given directory if needed. But linking the whole $PATH would be an overkill. )

这篇关于Windows上的Bash-exe文件的别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆