shell: bash -- string and array
一、源文件
1 [wit@ubuntu:null]$ cat ./string_and_array
2 #!/usr/bin/env bash
3
4
5
6
7 # file_name = string_and_array
8
9
10
11
12 # parameter
13 user_https='https://www.baidu.com/123.html'
14
15
16 echo -e "\n\n"
17
18
19 echo -e "\n---- string cut: delete left part of string ----\n"
20
21 echo -e "\tuser_https := " ${user_https}
22
23 echo -e "\t" '${user_https#*/} := ' ${user_https#*/}
24
25 echo -e "\t" '${user_https##*/} := ' ${user_https##*/}
26
27
28
29
30 echo -e "\n---- string cut: delete right part of string ----\n"
31
32 echo -e "\tuser_https := " ${user_https}
33
34 echo -e "\t" '${user_https%/*} := ' ${user_https%/*}
35
36 echo -e "\t" '${user_https%%/*} := ' ${user_https%%/*}
37
38
39
40
41 # array
42
43
44 four="4"
45 user_array=('one' 'two' 'three' $four 'five')
46
47
48 # output all elements
49 echo
50 echo -e "\n\n---- all elements of array ----\n"
51 echo -e "\t" '${user_array[@]} := ' ${user_array[@]}
52 echo -e "\t" '${user_array[*]} := ' ${user_array[*]}
53
54
55 echo
56 echo -e "\n\n---- count_elements_of_array ----\n"
57 # length(array()) = ${#user_array[@]}
58 # length(array()) = ${#user_array[*]}
59 echo -e "\t" 'length(user_array[@]) := ' ${#user_array[@]}
60 echo -e "\t" 'length(user_array[*]) := ' ${#user_array[*]}
61
62
63 echo
64 echo -e "\n\n---- output element of array ----\n"
65 # output specified element; ${user_array[1]} = user_array[1]
66 echo -e "\t" '${user_array[1]} := ' ${user_array[1]}
67 echo -e "\t" '${user_array[2]} := ' ${user_array[2]}
68
69
70 echo
71 echo -e "\n\n---- length_of_string ----\n"
72 # length(string) = length(${user_array[1]})= ${#user_array[1]}
73 echo -e "\t" "length(user_array[1]=${user_array[1]}) := " ${#user_array[1]}
74 echo -e "\t" "length(user_array[3]=${user_array[2]}) := " ${#user_array[2]}
75 echo -e "\n\n"
76
77
78 [wit@ubuntu:null]$
79 [wit@ubuntu:null]$
二、运行实例
1 [wit@ubuntu:null]$ ./string_and_array
2
3
4
5
6 ---- string cut: delete left part of string ----
7
8 user_https := https://www.baidu.com/123.html
9 ${user_https#*/} := /www.baidu.com/123.html
10 ${user_https##*/} := 123.html
11
12 ---- string cut: delete right part of string ----
13
14 user_https := https://www.baidu.com/123.html
15 ${user_https%/*} := https://www.baidu.com
16 ${user_https%%/*} := https:
17
18
19
20 ---- all elements of array ----
21
22 ${user_array[@]} := one two three 4 five
23 ${user_array[*]} := one two three 4 five
24
25
26
27 ---- count_elements_of_array ----
28
29 length(user_array[@]) := 5
30 length(user_array[*]) := 5
31
32
33
34 ---- output element of array ----
35
36 ${user_array[1]} := two
37 ${user_array[2]} := three
38
39
40
41 ---- length_of_string ----
42
43 length(user_array[1]=two) := 3
44 length(user_array[3]=three) := 5
45
46
47
48 [wit@ubuntu:null]$
49 [wit@ubuntu:null]$
三、参考文档
1、shell 变量 | 菜鸟教程 https://www.runoob.com/linux/linux-shell-variable.html
本文由 lnlidawei 原创、整理、转载,本文来自于【博客园】; 整理和转载的文章的版权归属于【原创作者】; 转载或引用时请【保留文章的来源信息】:https://www.cnblogs.com/lnlidawei/p/17848018.html