LussacZheng / video-downloader-deploy

Video Downloaders (you-get, youtube-dl, lux) One-Click Deployment Batch. || 视频下载器 (you-get, youtube-dl, lux) 一键配置脚本。
549 stars 99 forks source link

Console window just flashes and disappears / 命令行窗口一闪而过 #2

Closed LussacZheng closed 5 years ago

LussacZheng commented 5 years ago

Why the console window just flashes and disappears when I run the batch? 为什么运行脚本后,命令行窗口一闪而过?

LussacZheng commented 5 years ago

A record of fixing issue #2

Keywords: line endings, git, batch, gitattributes


Issue

Why the console window just flashes and disappears when I run the batch?


Cause

To save a .bat file from GitHub, you turn to the page of that file.

However, when you "save as" text files from GitHub, the files will be saved with the line endings LF. Even the batch files in the master.zip downloaded also use LF as line endings. This is where the problem lies.
Generally, a Windows batch should use CRLF as line endings. When you run a batch file with line endings LF, especially the batch file containing Chinese character/string, the cmd window will probably flash and close itself.


Seek for Solution

To make sure that users always get the right batch with line endings CRLF, there are several methods:

  1. Create a Release for each commit of batch updating. The .bat file downloaded directly from Release page is as it is before uploading.
  2. Tell users how to replace LF with CRLF by themselves. ( How to replace line endings )
  3. Upload and share the latest batch in cloud storage.
  4. etc. ...

But all these methods seem to be complex. Why not use a start.bat batch or lite start.exe program to do that?

  1. This batch should only has one line, so that it has nothing to do with line endings.
  2. What the batch should do is to download the install.bat, replace all LF with CRLF in the batch file, and run the new install.bat.
  3. To write such a batch, realize the basic function firstly.

    :: Download install.bat
    powershell (New-Object Net.WebClient).DownloadFile('https://raw.githubusercontent.com/LussacZheng/you-get_install_win/master/install_zh.bat', 'install_temp.bat')
    
    :: Replace 'LF' with 'CRLF'
    (for /f "delims=" %%i in (install_temp.bat) do @(set /p a=%%i<nul&echo.))>>install_zh-latest.bat
    
    :: Run the new batch
    install_zh-latest.bat
  4. Add some extra commands to avoid repetition or special situation.
  5. Then use & or && to connect all the commands.

Final Solution

Actually, there is a easiest solution. Using a .gitattributes file.

Write the following content into the .gitattributes file.

*.bat -crlf

This tells Git that never replace CRLF with LF for .bat files when they are written to the Git object database. From now on, when you "save as" batch files from this repository, the files will be saved with the line endings CRLF, which are suitable line endings for Windows batch files.


Additional Information

How to replace line endings

There are two easy methods to replace LF with CRLF on Windows.

Method 1 - Generally applicable

  1. Press win + R, input wordpad and press Enter.
  2. Use Wordpad to open install.bat.
  3. Directly Save the file.
  4. Close Wordpad, and try to run the batch again. At present, the batch should run normally, instead of flashing and disappearing.

Method 2

  1. Use a code editor to open intall.bat, such as Notepad++, Visual Studio Code, Sublime Text, etc.
  2. Code editors offer the function to set the line endings ( Windows style means CRLF, UNIX style means LF ). Do the setting or replacement.
  3. Save the changes and try to run the batch again.

Reference