Chapter 4 Arrays, Associative Arrays, and Alias Usage

Chapter 4 Arrays, Associative Arrays, and Alias ​​Usage

array

Glossary

As a special data structure, arrays have their place in any programming language, and arrays are also a very important part of shell scripts, which store multiple independent data as a character with the help of indexes.

grammar

Ordinary arrays can only use integers as array index values.

define array

格式:
arrary[key]=value
array=(value value value ...)

array method

One row and one column of values:

array_n=(1 2 3 4)

打印数组第一个值:
echo ${array_n[0]}  # 0 代表数组里边的索引,从左往右从0开始;
echo ${array_n}     # 默认不加索引,只打印第一个值。

打印数组所有值:
echo ${array_n[*]}

A set of index values:

[root@ceshi ~]# array_num[0]="text1"
[root@ceshi ~]# array_num[1]="text2"
[root@ceshi ~]# array_num[2]="text3"
[root@ceshi ~]# array_num[3]="text4"

[root@ceshi ~]# echo ${array_num[0]}    # 打印数组中第一个值
text1

[root@ceshi ~]# index=2
[root@ceshi ~]# echo ${array_num[$index]}   #也可以通过变量调用索引
text3

[root@ceshi ~]# echo ${array_num[*]}     # 打印数组中的所有值
text1 text2 text3 text4

Print the length of the array (that is, the number of elements in the array)

[root@ceshi ~]# echo ${#array_n[*]}     # 也就是打印所有元素的基础上加#
4   

delete array

删除数组中一个值
[root@ceshi ~]# echo ${array_num[*]}
text1 text2 text3 text4
[root@ceshi ~]# unset array_num[1]      #删除数组中一个值
[root@ceshi ~]# echo ${array_num[*]}
text1 text3 text4
[root@ceshi ~]# echo ${array_num[0]}
text1
[root@ceshi ~]# echo ${array_num[1]}    #元素删除了,索引也跟着删除了。

[root@ceshi ~]# echo ${array_num[2]}
text3

删除整个数组
[root@ceshi ~]# unset array_num

slice of array

is to intercept a piece of data

array=( [0]=one [1]=two [2]=three [3]=four )

# 截取所有元素
[root@ceshi ~]# echo ${array[@]:0}  #等同于echo ${array[*]} ;@也代表所有
one two three four

#截取除了第一个元素 之后的所有数据
[root@ceshi ~]# echo ${array[*]:1} 
two three four

#截取前两个元素
[root@ceshi ~]# echo ${array[*]:0:2}  
one two

#截取第2个和第3个元素
[root@ceshi ~]# echo ${array[*]:1:2}  
two three

substring deletion

(Actually, it matches the elements of the array, deletes the matched ones, and prints the remaining elements to the terminal; but does not actually modify the array)

[root@ceshi ~]# echo ${array[@]:0}   #查看数组所有元素
one two three four

#从左开始最短的匹配:"t*e" ,这代表会匹配到“thre”,程序就结束
[root@ceshi ~]# echo ${array[@]#t*e}
one two e four

#从左开始最长的匹配:"t*e" ,这代表会匹配到“three”,程序就结束
[root@ceshi ~]# echo ${array[@]##t*e}
one two four

#从字符串的结尾开始最短的匹配
[root@ceshi ~]# array[1]=ttwo   #先修改two 为ttwo
[root@ceshi ~]# echo ${array[@]}    #打印数组所有的元素
one ttwo three four
[root@ceshi ~]# echo ${array[@]%t*o}    #从右开始向左最短匹配
one t three four
[root@ceshi ~]# echo ${array[@]%%t*o}   #从右开始向左最长匹配
one three four

Substring replacement

#修改字符串o变成m
[root@ceshi ~]# echo ${array[@]/o/m}    
mne ttwm three fmur

#不指定替换子串,则删除匹配到的字符
[root@ceshi ~]# echo ${array[@]/o/}
ne ttw three fur
[root@ceshi ~]# echo ${array[@]//o/}
ne ttw three fur

#替换字符串前面的子串
[root@ceshi ~]# echo ${array[@]/#o/A}  
Ane ttwo three four

#替换字符串后面的子串
[root@ceshi ~]# echo ${array[@]/%o/A}
one ttwA three four

#当然也可以指定数组元素 进行替换
[root@ceshi ~]# echo ${array[3]/o/A} 
fAur

associative array

Associative arrays have been introduced since bash 4.0, and the index value of associative arrays can use arbitrary text (in fact, the index is replaced by an arbitrary string). Associative arrays are useful in many operations.

array_var=([one]=one-1 [two]=two-2 [three]=three-3)
#这里把索引 0 1 2 分别替换成了one two three

#当然也可以写成如下
array_var[one]=one-1    

The printing method of an associative array is the same as that of a normal array.

[root@ceshi ~]# echo ${array_var[one]}
one-1

List array index values

[root@ceshi ~]# echo ${!array_var[*]}
one two three

注意:该方法也可以用于普通数组。

alias

Aliases are all about providing a convenient way to do something with long strings of commands. Save unnecessary trouble and improve efficiency. Generally, it can be implemented as a function or an alias command.

alias example

alias nginxrestart='/usr/local/nginx/sbin/nginx -s reload'

这样就nginxrestart就代替了nginx重启的命令,重启之后才能生效。
注意:马上要使用,需要把这条命令放入~/.bashrc文件中,退出系统 重新登录一下就可以使用了。
echo 'alias nginxrestart="/usr/local/nginx/sbin/nginx -s reload"' >> ~/.bashrc

View system-defined aliases

[root@ceshi ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

Alias ​​escape

Some commands do not always want to use aliases, you can enter a backslash \ before the command to ignore the defined aliases.

格式:[root@ceshi ~]# \command
[root@ceshi ~]# alias       #查看已经定义的别名
alias pwd='pwd | cat -n'

[root@ceshi ~]# pwd         #查看别名后的命令的结果
     1  /root
[root@ceshi ~]# \pwd        #忽略别名后 pwd的结果,忽略了打印行号的功能。
/root

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326217838&siteId=291194637