Closed LussacZheng closed 5 years ago
Keywords: line endings, git, batch, gitattributes
Why the console window just flashes and disappears when I run the batch?
To save a .bat
file from GitHub, you turn to the page of that file.
.bat
file in a new tab of browser, just like opening a .txt
file with browser, instead of downloading it.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.
To make sure that users always get the right batch with line endings CRLF
, there are several methods:
.bat
file downloaded directly from Release page is as it is before uploading.LF
with CRLF
by themselves. ( How to replace line endings )But all these methods seem to be complex. Why not use a start.bat
batch or lite start.exe
program to do that?
install.bat
, replace all LF
with CRLF
in the batch file, and run the new install.bat
.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
&
or &&
to connect all the commands.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.
There are two easy methods to replace LF
with CRLF
on Windows.
Method 1 - Generally applicable
win + R
, input wordpad
and press Enter
.install.bat
.Method 2
intall.bat
, such as Notepad++, Visual Studio Code, Sublime Text, etc.CRLF
, UNIX style means LF
). Do the setting or replacement.
Why the console window just flashes and disappears when I run the batch? 为什么运行脚本后,命令行窗口一闪而过?