Run .sh or shell script files using WSL in Windows 10

运行时报错:syntax error near unexpected token `$'\r''
1. Enable WSL, refer to: https://www.shuijingwanwq.com/en/2021/12/23/9949/ , execute to step 5 3, the existing test.sh file, the code is as follows


#!/bin/bash
echo Hello World!


4. Run Windows Terminal as an administrator and execute the command: bash test.sh . run successfully. as shown in Figure 1
以管理员身份运行 Windows Terminal ,执行命令:bash test.sh 。成功运行
Figure 1


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


5. The existing Themes.sh file, the code is as follows


#!/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. Error at runtime: syntax error near unexpected token `$r. as shown in Figure 2
运行时报错:syntax error near unexpected token `$'r''
Figure 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. Adjust the line break in the IDE, from CRLF – Windows (rn) to LF – UNIX and macOS (n). as shown in Figure 3
在 IDE 中调整换行符,从 CRLF - Windows (rn) 调整为 LF - Unix 和 macOS (n)
Figure 3
8. Run again, and report an error: LogicException : please make sure the php redis extension is installed and enabled. This error is an internal command executed in the file. However, executing the internal command separately, the execution is successful. as shown in Figure 4
再次运行,报错:LogicException  : Please make sure the PHP Redis extension is installed and enabled.。 此报错为文件中执行的内部命令报错。但是,将内部命令单独执行,可执行成功
Figure 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. The final decision is to give up running the .sh or shell script files in the WSL. Reference: Use Ubuntu to execute .sh or shell script files on Windows 10. When the redis extension of php in ubuntu, execute the command again: sh themes.sh, and report an error: redisException : connection refused. From this, it is concluded that the environment configuration on which the execution of the .sh script depends must be fully prepared in Ubuntu. The connected Redis is no longer the Redis in Windows.  


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


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.