bash-learn

0. 自定义

vim /etc/profile.d/ cust_PS1.sh

1
2
3
4
5
6
7
8
# 修改提示语格式
PS1="[\u@47.97.104.205 \w #\#]\$ "

# 修改别名
alias list="declare -a"
alias int="declare -i"
alias map="declare -A"
alias final="declare -r"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[nginx@47.97.104.205 ~ #10]$ list a=(1 2)
[nginx@47.97.104.205 ~ #11]$ echo ${a[@]}
1 2
[nginx@47.97.104.205 ~ #12]$ for it in ${a[@]}; do echo it:$it; done
it:1
it:2

[nginx@47.97.104.205 ~ #13]$ int a=1+2+3
[nginx@47.97.104.205 ~ #14]$ echo $a
6

[nginx@47.97.104.205 ~ #15]$ final a=3
[nginx@47.97.104.205 ~ #16]$ echo $a
3
[nginx@47.97.104.205 ~ #17]$ a=4
-bash: a: readonly variable
[nginx@47.97.104.205 ~ #18]$ unset a
-bash: unset: a: cannot unset: readonly variable

[nginx@47.97.104.205 ~ #20]$ map b=(["a"]=1 ["b"]=2)
[nginx@47.97.104.205 ~ #25]$ for it in ${!b[@]}; do echo "k=$it, v=${b[$it]}" ; done
k=a, v=1
k=b, v=2

变量内容的删除, 取代和替换

1. 函数

1.1 新建函数

1
2
3
4
5
6
7
8
9
10
11
12
print_time1() {
echo -n "Today is: "
date +"%Y-%m-%d %H:%M:%S"
return 0
}

# 两个等价
function print_time2() {
echo -n "Today is: "
date +"%Y-%m-%d %H:%M:%S"
return 0
}

1.2 删除函数

1
2
# unset -f fun_name
unset -f print_time1

1.3 查询函数

1
2
3
4
5
6
# 查询所有定义的函数(包括函数体)
declare -f
# 查询所有定义的函数名称
declare -F
# 查询函数定义
declare -f fun_name

1.4 参数变量

  • $1~`$9`:函数的第一个到第9个的参数。
  • $0:函数所在的脚本名。
  • $#:函数的参数总数。
  • $@:函数的全部参数,参数之间使用空格分隔。
  • $*:函数的全部参数,参数之间使用变量$IFS值的第一个字符分隔,默认为空格,但是可以自定义。

如果函数的参数多于9个,那么第10个参数可以用${10}的形式引用,以此类推。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function print_time() {
echo "接受到$#个参数:"
local ind=1
for _arg in "$@";
do
echo "${ind}: ${_arg}"
((ind=ind+1))
done
local name="$1"
local format="$2"
echo -n "hello, ${name}. Today is: "
date +"${format}"
}
print_time lds "%Y-%m-%d %H:%M:%S"

2. 数组

其实感觉是map, 为空的位置又访问不到?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 也可以先申明 declare -a arr
arr=(hello world "hi lds")
ind=0
for it in ${arr[@]}
do
((ind++))
echo "$ind: $it"
done

# ${arr[@]} 放不放在双引号中, 结果不太一样
[git@iZbp1igl2202gsfyu9g5u3Z ~]$ ind=0;for it in ${arr[@]}; do ((ind++)); echo "$ind: $it"; done
1: hello
2: world
3: hi
4: lds

# "${arr[@]}" 这样才是期望的
[git@iZbp1igl2202gsfyu9g5u3Z ~]$ ind=0;for it in "${arr[@]}"; do ((ind++)); echo "$ind: $it"; done
1: hello
2: world
3: hi lds

# ${arr[*]}
[git@iZbp1igl2202gsfyu9g5u3Z ~]$ ind=0;for it in ${arr[*]}; do ((ind++)); echo "$ind: $it"; done
1: hello
2: world
3: hi
4: lds

# "${arr[*]}"
[git@iZbp1igl2202gsfyu9g5u3Z ~]$ ind=0;for it in "${arr[*]}"; do ((ind++)); echo "$ind: $it"; done
1: hello world hi lds
1
2
3
4
5
6
7
8
# 数组长度
$ a[100]=foo

$ echo ${#a[*]}
1

$ echo ${#a[@]}
1

${!array[@]}${!array[*]},可以返回数组的成员序号,即哪些位置是有值的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
$ arr=([5]=a [9]=b [23]=c)
$ echo ${!arr[@]}
5 9 23
$ echo ${!arr[*]}
5 9 23

# 遍历数组
arr=(a b c d)
for i in ${!arr[@]};do
echo ${arr[i]}
done


# 默认取第一个元素
[git@iZbp1igl2202gsfyu9g5u3Z ~]$ echo $arr
a

[git@iZbp1igl2202gsfyu9g5u3Z ~]$ echo ${arr[@]}
a b c

[git@iZbp1igl2202gsfyu9g5u3Z ~]$ echo ${!arr[@]}
0 9 23

[git@iZbp1igl2202gsfyu9g5u3Z ~]$ ind=0;for it in "${arr[@]}"; do ((ind++)); echo "$ind: $it"; done
1: a
2: b
3: c

# 可以进行切片
[git@iZbp1igl2202gsfyu9g5u3Z ~]$ echo ${arr[@]:0:2}
a b

# 可以进去索引获取
[git@iZbp1igl2202gsfyu9g5u3Z ~]$ echo ${arr[9]}
b

[git@iZbp1igl2202gsfyu9g5u3Z ~]$ echo ${arr[2]}

[git@iZbp1igl2202gsfyu9g5u3Z ~]$ echo ${arr[1]}

[git@iZbp1igl2202gsfyu9g5u3Z ~]$ echo ${arr[0]}
a

[git@iZbp1igl2202gsfyu9g5u3Z ~]$ echo ${arr[80]}

1
2
3
4
5
6
7
8
9
# 可以追加成员
[git@iZbp1igl2202gsfyu9g5u3Z ~]$ arr+=(append extra)
[git@iZbp1igl2202gsfyu9g5u3Z ~]$ echo ${arr[@]}
a b c append extra
[git@iZbp1igl2202gsfyu9g5u3Z ~]$

$ foo+=(d e f)
$ echo ${foo[@]}
a b c d e f
1
2
3
4
5
6
# 删除数组定义
[git@iZbp1igl2202gsfyu9g5u3Z ~]$ unset arr
[git@iZbp1igl2202gsfyu9g5u3Z ~]$ echo ${arr[@]}

[git@iZbp1igl2202gsfyu9g5u3Z ~]$

关联数组

Bash 的新版本支持关联数组。关联数组使用字符串而不是整数作为数组索引。

很像 Map<String, Object> ?

declare -A可以声明关联数组。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
declare -A colors
colors["red"]="#ff0000"
colors["green"]="#00ff00"
colors["blue"]="#0000ff"

# 或者
[git@iZbp1igl2202gsfyu9g5u3Z ~]$ declare -A code2msg=(["200"]="success" ["301"]="redirect" ["404"]="Not Found"); echo ${code2msg[@]}
Not Found success redirect
# 像map 一样?
[git@iZbp1igl2202gsfyu9g5u3Z ~]$ for key in ${!code2msg[@]}; do echo "key=$key, val=${code2msg[$key]}"; done
key=404, val=Not Found
key=200, val=success
key=301, val=redirect


关联数组必须用带有-A选项的declare命令声明创建。相比之下,整数索引的数组,可以直接使用变量名创建数组,关联数组就不行。

访问关联数组成员的方式,几乎与整数索引数组相同。

1
echo ${colors["blue"]}

declare [-aAfFgilnrtux] [-p] [name[=value] …]
typeset [-aAfFgilnrtux] [-p] [name[=value] …]
Declare variables and/or give them attributes. If no names are given then display the values of variables. The -p option will display the attributes and values of each name. When -p is used with name arguments, additional options, other than -f and -F, are ignored. When -p is supplied without name arguments, it will display the attributes and values of all variables having the attributes specified by the additional options. If no other options are supplied with -p, declare will display the attributes and values of all shell variables. The -f option will restrict the display to shell functions. The -F option inhibits the display of function definitions; only the function name and attributes are printed. If the extdebug shell option is enabled using shopt, the source file name and line number where each name is defined are displayed as well. The -F option implies -f. The -g option forces variables to be created or modified at the global scope, even when declare is executed in a shell function. It is ignored in all other cases. The following options can be used to restrict output to variables with the specified attribute or to give variables attributes:
-a Each name is an indexed array variable (see Arrays above).
-A Each name is an associative array variable (see Arrays above).
-f Use function names only.
-i The variable is treated as an integer; arithmetic evaluation (see ARITHMETIC EVALUATION above) is performed when the variable is assigned a value.
-l When the variable is assigned a value, all upper-case characters are converted to lower-case. The upper-case attribute is disabled.
-n Give each name the nameref attribute, making it a name reference to another variable. That other variable is defined by the value of name. All references, assignments, and attribute modifications to name, except those using or changing the -n attribute itself, are performed on the variable referenced by name’s value. The nameref attribute cannot be applied to array variables.
-r Make names readonly. These names cannot then be assigned values by subsequent assignment statements or unset.
-t Give each name the trace attribute. Traced functions inherit the DEBUG and RETURN traps from the calling shell. The trace attribute has no special meaning for variables.
-u When the variable is assigned a value, all lower-case characters are converted to upper-case. The lower-case attribute is disabled.
-x Mark names for export to subsequent commands via the environment.
Using +' instead of -‘ turns off the attribute instead, with the exceptions that +a may not be used to destroy an array variable and +r
will not remove the readonly attribute. When used in a function, declare and typeset make each name local, as with the local command,
unless the -g option is supplied. If a variable name is followed by =value, the value of the variable is set to value. When using -a or
-A and the compound assignment syntax to create array variables, additional attributes do not take effect until subsequent assignments.
The return value is 0 unless an invalid option is encountered, an attempt is made to define a function using ``-f foo=bar’’, an attempt is
made to assign a value to a readonly variable, an attempt is made to assign a value to an array variable without using the compound assign‐
ment syntax (see Arrays above), one of the names is not a valid shell variable name, an attempt is made to turn off readonly status for a
readonly variable, an attempt is made to turn off array status for an array variable, or an attempt is made to display a non-existent func‐
tion with -f.


bash-learn
http://47.97.104.205/2024-04-23/BashLearn/
作者
Rojer Di
发布于
2024年4月23日
许可协议