Fourth. Arrays, associative arrays, and aliases using array noun explanations
arrays, as a special kind of data structure, have their place in any programming language, and arrays are a very important part of the shell script, which uses indexes to store multiple independent data as a single role.
Grammar
An ordinary array can use integers only as an index value for an array.
Defining arrays
格式:arrary[key]=valuearray=(value value value ...)
Array method
Single row one column value:
array_n=(1 2 3 4)打印数组第一个值:echo ${array_n[0]} # 0 代表数组里边的索引,从左往右从0开始;echo ${array_n} # 默认不加索引,只打印第一个值。打印数组所有值:echo ${array_n[*]}
A set of index values:
[[email protected] ~]# array_num[0]="text1"[[email protected] ~]# array_num[1]="text2"[[email protected] ~]# array_num[2]="text3"[[email protected] ~]# array_num[3]="text4"[[email protected] ~]# echo ${array_num[0]} # 打印数组中第一个值text1[[email protected] ~]# index=2[[email protected] ~]# echo ${array_num[$index]} #也可以通过变量调用索引text3[[email protected] ~]# echo ${array_num[*]} # 打印数组中的所有值text1 text2 text3 text4
Prints the array length (that is, the number of elements in the array)
[[email protected] ~]# echo ${#array_n[*]} # 也就是打印所有元素的基础上加#4
Delete an array
删除数组中一个值[[email protected] ~]# echo ${array_num[*]}text1 text2 text3 text4[[email protected] ~]# unset array_num[1] #删除数组中一个值[[email protected] ~]# echo ${array_num[*]}text1 text3 text4[[email protected] ~]# echo ${array_num[0]}text1[[email protected] ~]# echo ${array_num[1]} #元素删除了,索引也跟着删除了。[[email protected] ~]# echo ${array_num[2]}text3删除整个数组[[email protected] ~]# unset array_num
Slices of an array
is to intercept a piece of data
array=( [0]=one [1]=two [2]=three [3]=four )# 截取所有元素[[email protected] ~]# echo ${array[@]:0} #等同于echo ${array[*]} ;@也代表所有one two three four#截取除了第一个元素 之后的所有数据[[email protected] ~]# echo ${array[*]:1} two three four#截取前两个元素[[email protected] ~]# echo ${array[*]:0:2} one two#截取第2个和第3个元素[[email protected] ~]# echo ${array[*]:1:2} two three
Sub-string deletion
(It is actually an array of elements to match, to match to the deletion, and to print the remaining elements to the terminal; but not to actually modify the arrays)
[[email protected] ~]# echo ${array[@]:0} #查看数组所有元素one two three four#从左开始最短的匹配:"t*e" ,这代表会匹配到“thre”,程序就结束[[email protected] ~]# echo ${array[@]#t*e}one two e four#从左开始最长的匹配:"t*e" ,这代表会匹配到“three”,程序就结束[[email protected] ~]# echo ${array[@]##t*e}one two four#从字符串的结尾开始最短的匹配[[email protected] ~]# array[1]=ttwo #先修改two 为ttwo[[email protected] ~]# echo ${array[@]} #打印数组所有的元素one ttwo three four[[email protected] ~]# echo ${array[@]%t*o} #从右开始向左最短匹配one t three four[[email protected] ~]# echo ${array[@]%%t*o} #从右开始向左最长匹配one three four
SUBSTRING substitution
#修改字符串o变成m[[email protected] ~]# echo ${array[@]/o/m} mne ttwm three fmur#不指定替换子串,则删除匹配到的字符[[email protected] ~]# echo ${array[@]/o/}ne ttw three fur[[email protected] ~]# echo ${array[@]//o/}ne ttw three fur#替换字符串前面的子串[[email protected] ~]# echo ${array[@]/#o/A} Ane ttwo three four#替换字符串后面的子串[[email protected] ~]# echo ${array[@]/%o/A}one ttwA three four#当然也可以指定数组元素 进行替换[[email protected] ~]# echo ${array[3]/o/A} fAur
Associative arrays
Associative arrays are introduced from bash4.0, and indexed values of associative arrays can use arbitrary text (which is actually replacing the index with arbitrary strings). 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
Associative arrays are printed in the same way as normal array usages
[[email protected] ~]# echo ${array_var[one]}one-1
List array index values
[[email protected] ~]# echo ${!array_var[*]}one two three注意:该方法也可以用于普通数组。
Alias
Aliases provide a convenient way to do certain long-string commands. Eliminate unnecessary hassles and improve efficiency. This can usually be implemented by a function or 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 aliases already defined by the system
[[email protected] ~]# aliasalias 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, and you can enter backslashes before the command to ignore the defined aliases.
格式:[[email protected] ~]# \command
[[email protected] ~]# alias #查看已经定义的别名alias pwd=‘pwd | cat -n‘[[email protected] ~]# pwd #查看别名后的命令的结果 1 /root[[email protected] ~]# \pwd #忽略别名后 pwd的结果,忽略了打印行号的功能。/root
Fourth. Array, associative array, and alias use