用了很久的bash,但是没有很深入,详细的看下文档做下笔记吧
如果你想让你的bash脚本国际化,输出的语音可以根据主机地域翻译为不同的语言可以按照下列流程操作
加入有一个脚本test.sh
1 |
|
运行bash --dump-po-strings test.sh > domain.pot
通常这个domain.pot(模板文件)文件放在./po文件夹下面
讲mydomain.pot复制为多个要翻译的翻译文件比如zh_CN.po
创建文件夹用于分类存储各个版本的翻译语言mkdir -p locale/zh_CN/LC_MESSAGES
编译翻译配置文件为二进制文件msgfmt po/zh_CN.po -o locale/zh_CN/LC_MESSAGES
注意需要在脚本中配置TEXTDOMAIN 以及 TEXTDOMAINDIR
也可以将编译后的文件放在系统指定的位置
1 | TEXTDOMAIN=example |
1 |
|
最终运行脚本发现文本就会被翻译为指定的语言。
A pipeline
is a sequence of one or more commands separated by one of the control operators ‘|’ or ‘|&’.
1 | [time [-p]] [!] command1 [ | or |& command2 ] … |
其中time可以输出命令执行的时间
|可以将标准输出传递给command2
|&可以将标准输出以及标准错误输出传递给command2
A list
is a sequence of one or more pipelines separated by one of the operators ‘;’, ‘&’, ‘&&’, or ‘||’, and optionally terminated by one of ‘;’, ‘&’, or a newline
.
其中&& 与 ‘||’具有相同的优先级 &与|具有相同的优先级
‘;’用于分隔语句‘;’前后的语句顺序执行
1 | echo "Hello"; echo "World" |
‘&’用于后台执行命令 ‘&’前后的命令会异步执行就是同步的运行
1 | time sleep 5 & #便会在后台执行5秒钟然后返回给前端 |
&&
表示逻辑“与”:只有前面的命令执行成功(返回状态为 0),才会执行后面的命令。||
表示逻辑“或”:只有前面的命令执行失败(返回状态非 0),才会执行后面的命令。until test-commands; do consequent-commands; done
until直到判断为真的时候终止循环
1 | i=1 |
while test-commands; do consequent-commands; done
直到判断为假的时候终止循环
for
1 | for name [ [in [words …] ] ; ] do commands; done |
1 | if test-commands; then |
1 | case word in |
每条语句都必须以’’;;’, ‘;&’, ‘;;&’结尾
‘;;’以这个结尾的当匹配到本条语句就会终止跳过后面的
‘;&’以这个结尾的当匹配到本语句还会执行下一条语句,不进行再次的匹配
‘;;&’以这个结尾的还会尝试匹配后面的语句
1 | #!/bin/bash |
1 | select name [in words …]; do commands; done |
改表达式会将列表中的每一项标号输出,然后接受用户的标准输入
如果输入语某一个标号相同则会将name赋值为该序号对应的word, 该序号会被保存在REPLY变量中
数学表达式,表达式中的双引号会被忽略,如果表达式计算的结果是0那么该语句的退出码为1表示不成功
繁殖表达式计算结果为1那么退出码的结果为0表示成功
1 | if (( x > 5 )); then |
增强型的条件判断
双括号里面的语句不会进行 word splitting 和 filename expansion
但是依然会正常进行波浪号展开,参数变量展开,算数展开,命令替换,进程替换,引号去除
虽然会进行引号去除Conditional operators such as ‘-f’ must be unquoted to be recognized as primaries.
因为引号去除是发生在条件操作符识别之后进行的。
When used with [[
, the ‘<’ and ‘>’ operators sort lexicographically using the current locale.
此法拆分是指在Bash执行命令之前,根据特殊字符(主要是空格,制表符合换行符)将命令中的字符串分割为多个单词
或参数的过程。整个过程通常发生在变量替换之后。例如,如果你有一个变量 var = “hello world”, 如果你不加引号直接使用
echo $var 是 bash会进行词法拆分,将其视为两个独立的单词:hello和world 。为了避免这种情况,你可以使用双引号包围变量
如echo “$var”
Bash provides two ways to group a list of commands to be executed as unit
Placing a list of commands between parentheses force the shell to create a subshell, and each of the commands
In list is executed in the subshell enivorment.
Placing a list of commands between curly braces causes the list to be executed in the current shell context. No subshell is created. The semicolon (or newline) following list is required.
The braces are reserved words, so they must be separated from the list by blank or other matecharacter.
the syntex is
1 | coproc [NAME] command [redirections] |
Syntex
1 | fname () compound-command [ redirections ] |
when executed, the exit status of a function is the exit status of the latest command executed in the function.
In the most common usage the curly braces that surround the body of the function must be separated by balnks
or newline. This is because the braces are reserved words.(they are only recognized as such when they are separated
from the command list by whitespace or other shell metacharacter)
Also, when using the brace, the list must be terminated by semicolon, a ‘&’, or a newline.
the arguement of the function become the positional parameters during its executed.
the special parameter “#” is used to reflect the number of posotional parameters.
the first element of the FUNCNAME variable is set to the name of the function
you can use $(number) to use positional parameters.
Special parameter 0
is unchanged, it used to express the name of the shell name.
1 | func () { |
if set the numeric value greater than 0, defines a maximum function nesting level.
Function invocations that exceed the limit cause the entire command to abort.
If the builtin command return
is executed in a function, the function completes and execution resumes with the next command after the function call
Variables local to the function may be declared with the local
builtin (local variables). Ordinarily, variables and their values are shared between a function and its caller. These variables are visible only to the function and the commands it invokes.
a local variable declared in a function hides a global variable of the same name:
(局部变量)
the parameter is an entity that storags value. It can be a name, a number, or one of the special characters listed below
1 | name=[value] |
if value is not given, the variable is assigned the null string.
All values undergo tilde expansion, parameter expansion, variable expansion, command substitution, arithemetic expansion,
and quote removal.
integer attribute
1 | # 使用整数属性可以在变量赋值的时候自动的对变量表达式进行计算然后赋值给变量 |
使用declear -n进行声明类似指针
1 | var="hello world" |
在函数中
使用declear -n 对 $1 positional parameter进行操作时可以间接的对全局变量进行赋值
1 | var="hello world" |