在 Windows 10 中使用 WSL 运行 .sh 或 Shell 脚本文件
1、启用 WSL,参考:https://www.shuijingwanwq.com/2021/12/23/5559/ ,执行到第 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
1 条回复
[…] 上一篇 在 Windows 10 中使用 WSL 运行 .sh 或 Shell 脚本文件 […]