1、启用 WSL,参考:执行到第 5 步骤
3、现有 test.sh 文件,其代码如下
#!/bin/bash
echo Hello World!
4、以管理员身份运行 Windows Terminal ,执行命令:bash test.sh 。成功运行。如图1

PS E:\wwwroot\object> bash test.sh
Hello World!
5、现有 themes.sh 文件,其代码如下
#!/bin/sh
for i in $(seq 1 200);
do
echo "Install theme $i times"
php artisan theme-store-db:theme:install C:/Users/Lenovo/Downloads/theme "theme$i" blade --force
done
6、运行时报错:syntax error near unexpected token `$’\r”。如图2

PS E:\wwwroot\object> bash themes.sh
themes.sh: line 2: $'\r': command not found
themes.sh: line 3: syntax error near unexpected token `$'\r''
'hemes.sh: line 3: `for i in $(seq 1 200);
PS E:\wwwroot\object>
7、在 IDE 中调整换行符,从 CRLF – Windows (\r\n) 调整为 LF – Unix 和 macOS (\n)。如图3

8、再次运行,报错:LogicException : Please make sure the PHP Redis extension is installed and enabled.。 此报错为文件中执行的内部命令报错。但是,将内部命令单独执行,可执行成功。如图4

PS E:\wwwroot\object> bash themes.sh
Install theme 1 times
LogicException : Please make sure the PHP Redis extension is installed and enabled.
at /mnt/e/wwwroot/object/vendor/laravel/framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php:77
73| protected function createClient(array $config)
74| {
75| return tap(new Redis, function ($client) use ($config) {
76| if ($client instanceof RedisFacade) {
> 77| throw new LogicException(
78| extension_loaded('redis')
79| ? 'Please remove or rename the Redis facade alias in your "app" configuration file in order to avoid collision with the PHP Redis extension.'
80| : 'Please make sure the PHP Redis extension is installed and enabled.'
81| );
Exception trace:
1 Illuminate\Redis\Connectors\PhpRedisConnector::Illuminate\Redis\Connectors\{closure}()
/mnt/e/wwwroot/object/vendor/laravel/framework/src/Illuminate/Support/helpers.php:422
2 tap()
/mnt/e/wwwroot/object/vendor/laravel/framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php:105
Please use the argument -v to see more details.
PS E:\wwwroot\object> php artisan theme-store-db:theme:install C:/Users/Lenovo/Downloads/theme theme blade --force
安装主题到数据仓库
主题theme安装成功
PS E:\wwwroot\object>
9、最终决定放弃在 WSL 中运行 .sh 或 Shell 脚本文件。参考:在 Windows 10 上使用 Ubuntu 执行 .sh 或 Shell 脚本文件。当在 Ubuntu 中 PHP 的 Redis 扩展后,再次执行命令:sh themes.sh,报错:RedisException : Connection refused。由此得出结论,.sh 脚本的执行所依赖的环境配置必须在 Ubuntu 中全部准备妥当才行。所连接的 Redis 已经不再是 Windows 中的 Redis。
PS E:\wwwroot\object> bash themes.sh
Install theme 1 times
RedisException : Connection refused
at /mnt/e/wwwroot/object/vendor/laravel/framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php:137
133| $parameters[] = $context;
134| }
135| }
136|
> 137| $client->{($persistent ? 'pconnect' : 'connect')}(...$parameters);
138| }
139|
140| /**
141| * Create a new redis cluster instance.
Exception trace:
1 Redis::pconnect()
/mnt/e/wwwroot/object/vendor/laravel/framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php:137
2 Illuminate\Redis\Connectors\PhpRedisConnector::establishConnection()
/mnt/e/wwwroot/object/vendor/laravel/framework/src/Illuminate/Redis/Connectors/PhpRedisConnector.php:84
Please use the argument -v to see more details.
Install theme 2 times
^C
Linux 服务器运维、部署与线上故障排查
如果你的网站或后端服务部署在 Linux 服务器上,遇到访问异常、Nginx 配置问题、MySQL / Redis 异常、Docker 服务不可用、磁盘占满、CPU / 内存过高等问题,可以联系我做一次远程排查。
适合以下场景:
✅ 网站打不开或访问不稳定
✅ Nginx / PHP-FPM 配置异常
✅ MySQL / Redis 性能或连接问题
✅ Docker 服务部署与维护
✅ 服务器迁移与环境配置
✅ CPU / 内存 / 磁盘异常排查
服务内容:
✅ Linux 环境检查
✅ 网站部署与迁移
✅ Nginx / PHP-FPM / MySQL / Redis 排查
✅ Docker 配置与维护
✅ 服务器性能分析
✅ 长期远程运维支持
如需咨询,请联系我,并注明:Linux 运维咨询。
联系方式:
Telegram:@shuijingwan
微信:13980074657
邮箱:shuijingwanwq@gmail.com

评论
2 条对“在 Windows 10 中使用 WSL 运行 .sh 或 Shell 脚本文件”的回复
[…] 上一篇 在 Windows 10 中使用 WSL 运行 .sh 或 Shell 脚本文件 […]
[…] Reference: Running .sh or Shell script files using WSL in Windows 10 . Ultimately […]