没有不值得去解决的问题,也没有不值得去学习的技术!

Run .sh or Shell Scripts in Windows 10 With WSL

运行时报错:syntax error near unexpected token `$'\r''

1. Enable WSL, refer to: Execute up to step 5

3. There is an existing test.sh file, its code is as follows

ShellScript

#!/bin/bash
echo Hello World!

4. Run Windows Terminal as an administrator, and execute the command: bash test.sh. It runs successfully. As shown in Figure 1

Run Windows Terminal as an administrator, and execute the command: bash test.sh. It runs successfully
Figure 1
Plaintext

PS E:\wwwroot\object> bash test.sh
Hello World!

5. There is an existing themes.sh file, its code is as follows

ShellScript

#!/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. An error occurs at runtime: syntax error near unexpected token `$’\r”. As shown in Figure 2

Runtime error: syntax error near unexpected token `$'\r''
Figure 2
Plaintext

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. Adjust the line endings in the IDE from CRLF – Windows (\r\n) to LF – Unix and macOS (\n). As shown in Figure 3

Adjust line endings in the IDE from CRLF - Windows (\r\n) to LF - Unix and macOS (\n)
Figure 3

8. Run it again, and an error occurs: LogicException : Please make sure the PHP Redis extension is installed and enabled. This error is triggered by an internal command executed within the file. However, executing the internal command separately succeeds. As shown in Figure 4

Run it again, and an error occurs: LogicException : Please make sure the PHP Redis extension is installed and enabled. This error is triggered by an internal command executed within the file. However, executing the internal command separately succeeds
Figure 4
Plaintext
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. Finally, I decided to give up running .sh or Shell script files in WSL. Reference: Running .sh or Shell script files using Ubuntu on Windows 10. After installing the PHP Redis extension in Ubuntu, executing the command sh themes.sh again results in an error: RedisException : Connection refused. This leads to the conclusion that the environment configurations required for executing .sh scripts must be fully prepared in Ubuntu. The Redis being connected to is no longer the Redis in Windows.

 

Plaintext

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

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系