bash_notes

用了很久的bash,但是没有很深入,详细的看下文档做下笔记吧

bash_doc

bash 国际化

如果你想让你的bash脚本国际化,输出的语音可以根据主机地域翻译为不同的语言可以按照下列流程操作

  1. 加入有一个脚本test.sh

    1
    2
    3
    4
    #!/bin/bash
    #需要翻译的文字引号前面需要有$符号
    echo $"Hello World"
    echo $"today is nice day"
  2. 运行bash --dump-po-strings test.sh > domain.pot 通常这个domain.pot(模板文件)文件放在./po文件夹下面

  3. 讲mydomain.pot复制为多个要翻译的翻译文件比如zh_CN.po

  4. 创建文件夹用于分类存储各个版本的翻译语言mkdir -p locale/zh_CN/LC_MESSAGES

  5. 编译翻译配置文件为二进制文件msgfmt po/zh_CN.po -o locale/zh_CN/LC_MESSAGES

  6. 注意需要在脚本中配置TEXTDOMAIN 以及 TEXTDOMAINDIR

    也可以将编译后的文件放在系统指定的位置

    1
    2
    TEXTDOMAIN=example
    TEXTDOMAINDIR=/usr/local/share/locale
    1
    2
    3
    4
    5
    6
    7
    #!/bin/bash
    #指定环境变量 翻译域
    TEXTDOMAIN=tpo
    TEXTDOMAINDIR=./locale
    #需要翻译的文字引号前面需要有$符号
    echo $"Hello World"
    echo $"today is nice day"
  7. 最终运行脚本发现文本就会被翻译为指定的语言。

Piplines

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

Lists of Commands

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),才会执行后面的命令。

Looping Constructs

  1. until test-commands; do consequent-commands; done

    until直到判断为真的时候终止循环

    1
    2
    3
    4
    5
    i=1
    until [ $i -gt 5 ]; do
    echo "until: \$i = $i"
    i=$((i + 1))
    done
  2. while test-commands; do consequent-commands; done

    直到判断为假的时候终止循环

  3. for

    1
    2
    3
    for name [ [in [words …] ] ; ] do commands; done
    #当省略in words的时候bash会默认使用位置参数 ‘in "$@"’
    for (( expr1 ; expr2 ; expr3 )) ; do commands ; done

Conditional Constructs

if

1
2
3
4
5
6
if test-commands; then
consequent-commands;
[elif more-test-commands; then
more-consequents;]
[else alternate-consequents;]
fi

case

1
2
3
case word in
[ [(] pattern [| pattern]…) command-list ;;]…
esac

每条语句都必须以’’;;’, ‘;&’, ‘;;&’结尾

‘;;’以这个结尾的当匹配到本条语句就会终止跳过后面的

‘;&’以这个结尾的当匹配到本语句还会执行下一条语句,不进行再次的匹配

‘;;&’以这个结尾的还会尝试匹配后面的语句

1
2
3
4
5
6
7
#!/bin/bash
read name
case $name in
admin | adm | ad) echo "i'm is admin";;
endiexz | endiex | endie) echo "i'm is endiexz";;
*) echo "i'm loser";;
esac

select

1
select name [in words …]; do commands; done

改表达式会将列表中的每一项标号输出,然后接受用户的标准输入
如果输入语某一个标号相同则会将name赋值为该序号对应的word, 该序号会被保存在REPLY变量中

((…))

数学表达式,表达式中的双引号会被忽略,如果表达式计算的结果是0那么该语句的退出码为1表示不成功
繁殖表达式计算结果为1那么退出码的结果为0表示成功

1
2
3
if (( x > 5 )); then
echo "x is greater than 5"
fi

[[]]

增强型的条件判断
双括号里面的语句不会进行 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.

word splitting

此法拆分是指在Bash执行命令之前,根据特殊字符(主要是空格,制表符合换行符)将命令中的字符串分割为多个单词
或参数的过程。整个过程通常发生在变量替换之后。例如,如果你有一个变量 var = “hello world”, 如果你不加引号直接使用
echo $var 是 bash会进行词法拆分,将其视为两个独立的单词:hello和world 。为了避免这种情况,你可以使用双引号包围变量
如echo “$var”

Grouping Commands

Bash provides two ways to group a list of commands to be executed as unit

( List )

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.

{ list; }

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.

CPROC

the syntex is

1
2
3
coproc [NAME] command [redirections]
# the recommended form
coproc NAME { command; }

function

Syntex

1
2
3
fname () compound-command [ redirections ]
# or
function 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
2
3
4
5
6
7
8
func () {
echo $#
echo $0
echo $1
echo $2
echo ${FUNCNAME[0]}
}
func a b

FUNCNEST

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:
(局部变量)

Shell Parameters

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
2
3
4
5
6
7
# 使用整数属性可以在变量赋值的时候自动的对变量表达式进行计算然后赋值给变量
declear -i var
var="2 + 3 + 4"
echo $var

#显示结果为
9

nameref

使用declear -n进行声明类似指针

1
2
3
4
5
6
7
8
var="hello world"
declear -n ref=var
echo $ref
ref="endiexz"

#结果为
hello world
#对ref的操作最终都会作用在var身上,ref输出的结果其实都是var

在函数中
使用declear -n 对 $1 positional parameter进行操作时可以间接的对全局变量进行赋值

1
2
3
4
5
6
7
8
var="hello world"
func() {
declear -n ref="$1"
echo $ref
ref="endiexz"
}
func var
echo $var

Positional Parameters