Article directory
Sometimes in order to make it easier for others to use, we will choose to write various command line scripts: writing .bat
cmd batch scripts for Windows users, and writing .sh
bash shell scripts for macOS and Linux users.
Of course, Chinese should be the first choice as the script display language for domestic users. If it also supports overseas users, it would be best to provide international multi-language ( i18n
) support. It would be easier to only provide the English version; the concept of i18n is too big. Well, this article records how to implement it simply: according to the user's language environment, the script can be automatically displayed in Chinese or English. Of course, if you are willing, you can also support more languages at the same time.
Command line script reference - bat
@echo off
::请保存成gbk编码.bat文件,为了防止乱码吞掉关键字符,所有多字节字符文本后面均多放了几个空格
::取消下面这行注释可以切换成英文代码页cmd窗口
::chcp 437
::识别当前语言,0英文,1中文,2...更多支持的语言,中文识别简单粗暴
set CurrentLang=0
ver | find "版本%qjkTTT%" > nul && set CurrentLang=1
::封装echo输出函数,通过2个或更多参数提供不同语言的文字,只显示当前语言文字
goto func__echo2
:echo2
if "%CurrentLang%"=="1" echo %~1
if "%CurrentLang%"=="0" echo %~2
goto:eof
:func__echo2
::测试
call:echo2 "显示语言:简体中文 " "Language: English"
call:echo2 "脚本运行啦 " "The script is running"
pause
Command line scripting reference - bash
#!/usr/bin/env bash
# 请保存成utf-8编码.sh文件,将文件设为允许执行,然后到终端中执行即可测试
# 识别当前语言,0英文,1中文,2...更多支持的语言
CurrentLang=0
if [ $(echo ${
LANG/_/-} | grep -Ei "\\b(zh|cn)\\b") ]; then CurrentLang=1; fi
# 封装echo输出函数,通过2个或更多参数提供不同语言的文字,只显示当前语言文字
function echo2(){
if [ $CurrentLang == 1 ]; then
echo $1; #显示中文
else
echo $2; #显示英文
fi
}
#测试
echo2 "显示语言:简体中文" "Language: English"
echo2 "脚本运行啦" "The script is running"
read -n1 #按任意键退出
The above bat and bash script codes are referenced from the GitHub open source library: https://github.com/xiangyuecn/RSA-csharp , Test-Build-Run.bat
and Test-Build-Run.sh
scripts. There is no need for IDE to directly call .NET to compile and run c#
the code. It supports PEM in .NET Core and .NET Framework environments. (PKCS#1, PKCS#8) format RSA key generation, import, export, multiple common RSA encryption, signature padding algorithms supported.
Like https://github.com/xiangyuecn/RSA-java , the same sh and bat scripts do not require IDE to directly call JDK to compile and run java
the code. It is convenient to use bash and bat script codes and convenient for reference.
Knowledge points worth learning
1. Identify the language used by the terminal
The trickier way is to directly check whether the text displayed in the version number of the terminal contains the word "version" to determine whether it is a Chinese environment or an English environment. It is supported in bash and bat cmd, and it is mainly very simple (not excluding some cases). The result judged using this method may not be consistent with the actual situation). $LANG
In addition, you can use environment variables to judge in bash , which is also easy to use.
// bat中
> ver //输出:Microsoft Windows [版本 10.0.****.***]
//bash中
> bash -version //输出:GNU bash,版本 5.1.16 ......
2. Writing functions
Bash itself supports the writing of functions. It is relatively simple. You can function echo2(){ ... }
define the function through and obtain the parameters echo2
within the function $1 $2 $3 ...
. When calling, just write the function name and add parameters directly to complete the function call: echo2 "args1" args2 args3
, the parameters can be wrapped in quotation marks (spaces in the parameters must be wrapped), if there are no parameters, just write a function name directly to complete the call: echo2
.
There is no syntax specifically defining functions in the bat script of cmd, but it supports tags and goto jumps. You can execute it wherever you want. It is very flexible, and tags support call:lable
calling. With special tags, eof
you can return to call
the calling point.
Therefore, in the bat script, we can :echo2
define the function entry position through, goto:eof
define the function end position through, and %1 %2 %3 ...
obtain the parameters within the function. %1
This method will not remove the first and last quotation marks of the parameters. Using it %~1
will remove the first and last quotation marks.
Although :echo2
the function is defined through the label echo2
, when the bat script is executed, the function content after the label will be executed line by line, so we :echo2
have to goto
skip the function body by passing it before, so we put a :func__echo2
label at the end of the function (the label name is arbitrary), the function Put one at the beginning goto func__echo2
so that the function body will be skipped during execution.
For the final function call, use call:echo2 "args1" args2 args3
to call the function. The parameters can be wrapped in quotation marks (spaces in the parameters must be wrapped). If there are no parameters, just write one directly call:echo2
to complete the call.
3. Get user input
In bash, read
input is obtained through commands, and in bat, set /p
input is obtained through commands.
//bat中
set text=&set /p text=^>
//bash中
read -rp "> " text
The above code will be displayed in different environments "> "
, and then wait for user input. After pressing Enter, the text content will be stored text
in the variable; it is worth noting that if there is no input content and you press Enter directly, the variable will not be The assignment is empty. This setting is quite strange, so you need to set the variable to empty in advance before inputting, or you || set text=
can use it to set it to empty after inputting.
4. What should I do if the bat file is always garbled?
Hey, the bat script follows the system's default encoding. If the file is saved in UTF-8, the Chinese characters will be garbled. You have to use the force switch chcp 65001
to the UTF-8 code page at the beginning of the file. However, the script will not be garbled, but it will call many programs. The content output by the command becomes garbled, and gbk encoding is still used.
Notice that all multi-byte character texts (Chinese) in the bat file are deliberately written with a few extra spaces after them. This is to prevent newlines, quotation marks and other symbols from being swallowed by the garbled characters (whether it has any effect needs to be verified), even if The bat file is garbled, but it can be executed correctly and the English content inside is output. There is one in the above code "版本%qjkTTT%"
. This is also a special treatment to prevent the ending quotation marks from being swallowed when the code is garbled. Kun Jin Kao · Hot, Hot, Hot finally resisted everything.
【over】