无法从 WSL 'bash script.sh' 命令行运行 ruby/bundle 脚本,但可以从 bash 解释器 'sh script.sh' 运行

问题描述 投票:0回答:1

我已使用以下命令在 Windows 11 上的 WSL2 环境中成功安装了 ruby:

$ sudo apt install gnupg2 build-essential dh-autoreconf
$ gpg --keyserver keyserver.ubuntu.com --recv-keys ...
$ \curl -sSL https://get.rvm.io | bash
$ rvm install ruby
$ gem update
$ gem install jekyll
$ gem install bundler

之后,安装的ruby版本是3.1.3p185(这是我运行时得到的

ruby --version

现在考虑我的 WSL 主目录中的以下

test.sh
脚本:

#!/bin/bash
bundle exec jekyll build

当我打开 bash 命令提示符时(使用 Win+R 然后输入 bash),我可以成功运行:

$ sh test.sh

在这种情况下,Jekyll 构建运行良好。

现在,如果从 Powershell(或 Windows CMD 提示符)我输入:

$ bash test.sh

我收到以下错误:

/usr/local/bin/bundle: /usr/bin/ruby2.7: bad interpreter: No such file or directory

为什么?我还有许多其他 .sh 脚本,可以使用

bash script.sh
语法正常运行。

我发现了以下问题

`$bundle install`抛出错误:bash:/usr/local/bin/bundle:/usr/bin/ruby2.7:错误的解释器:没有这样的文件或目录

但不幸的是,建议的修复不起作用(我已经安装了

bundler
)。

编辑:使用

wsl -e bash test.sh
而不是
bash test.sh
会产生相同的错误。另外,如果
test.sh
只包含
ruby --version
,我会得到
ruby: command not found

ruby bash jekyll windows-subsystem-for-linux bundler
1个回答
0
投票

问题来自于 ruby,其 PATH 是在

.bashrc
中设置的,实际上
.bash_profile
也是如此。解决方案是将 bash 作为登录 shell 运行 (
bash -l
)。

© www.soinside.com 2019 - 2024. All rights reserved.