将 Windows 10 上的 linux shell (.sh) 脚本与 Bash 或 WSL 相关联

Ale*_*Sim 6 windows bash sh windows-10 windows-subsystem-for-linux

如果您曾尝试通过双击在 Windows 10 上运行 .sh linux 脚本,在默认情况下将其分配给 bash.exe(Windows 10 的 Ubuntu Bash)后,您很可能注意到它不会打开。

为什么它不打开,我该如何解决这个问题?

Ale*_*Sim 9

编辑:下面的注册表项和信息已过时,请参阅此处提供的新版本:Can I drag and drop files to an .sh script using Bash on Ubuntu on Windows or Windows Subsystem for Linux (WSL)?

这可以通过编辑单个注册表项(或更多以获得额外功能)来完成。

我当然会链接一个 .reg 文件(实际上是三个压缩的 reg 文件)以快速安装解决方法(以及图标关联和(可选)nano 编辑启用程序),而无需自己编辑注册表,如果您下载并安装它,您必须只遵循第 1 步;

文件(只需按照步骤1,然后下载安装):http : //www.mediafire.com/file/r5uxk9c0hhucvt9/ShellBashScriptOpen_v3.rar

但是,为了满足您对健康的不信任,您可以通过以下方式自行解决:

.

1) 将 bash.exe 设置为 .sh 文件的默认程序

只需右键单击一个 sh 文件,转到属性,找到“打开方式”,更改 > more_apps > find_an_app_in_this_PC 并选择 C:\Windows\System32\bash.exe;

申请就可以了。

.

2) 实际双击开关键

使用 regedit 创建以下注册表项

HKEY_CLASSES_ROOT\Applications\bash.exe\shell\open\command
Run Code Online (Sandbox Code Playgroud)

a) 如果你想在脚本完成后保持 bash 打开

将(默认)值设置为

"C:\Windows\System32\bash.exe" -c " \"./$(grep -oE '[^\\]+$' <<< '%L')\"; cd ~; bash;"
Run Code Online (Sandbox Code Playgroud)

b) 如果你想在脚本完成后关闭 bash

将(默认)值设置为

"C:\Windows\System32\bash.exe" -c " \"./$(grep -oE '[^\\]+$' <<< '%L')\";"
Run Code Online (Sandbox Code Playgroud)

.

3)以管理员身份打开

使用 regedit 创建以下注册表项

HKEY_CLASSES_ROOT\Applications\bash.exe\shell\runas\command
Run Code Online (Sandbox Code Playgroud)

a) 如果你想在脚本完成后保持 bash 打开

将(默认)值设置为

"C:\Windows\System32\bash.exe" -c " sudo bash \"$(grep -oE '[^\\]+$' <<< '%L')\"; cd ~; bash"
Run Code Online (Sandbox Code Playgroud)

b) 如果你想在脚本完成后关闭 bash

将(默认)值设置为

"C:\Windows\System32\bash.exe" -c " sudo bash \"$(grep -oE '[^\\]+$' <<< '%L')\";"
Run Code Online (Sandbox Code Playgroud)

.

4)分配默认图标键(可选[但更酷])

创建以下注册表项

HKEY_CLASSES_ROOT\Applications\bash.exe\DefaultIcon
Run Code Online (Sandbox Code Playgroud)

并将(默认)值设置为

%USERPROFILE%\AppData\Local\lxss\bash.ico,0
Run Code Online (Sandbox Code Playgroud)

.

5)添加右键>编辑选项(带nano编辑器)(可选)

创建以下注册表项

HKEY_CLASSES_ROOT\Applications\bash.exe\shell\edit\command
Run Code Online (Sandbox Code Playgroud)

并将(默认)值设置为

"C:\Windows\System32\bash.exe" -c "nano -miST 4 \"$(grep -oE '[^\\]+$' <<< '%L')\""
Run Code Online (Sandbox Code Playgroud)