shell脚本(二)

目录

1函数 function

1.1函数介绍

1.2管理函数

1.3 定义函数

1.4查看函数

1.5删除函数

1.6函数调用

1.7函数返回值

1.8环境函数

1.9函数参数

2.0函数变量

2.1函数递归

2 其它脚本相关工具

1 信号捕捉 trap

2 创建临时文件 mktemp

3 安装复制文件 install

4 交互式转化批处理工具 expect

3 数组 array

1 数组介绍

2 声明数组

3 数组赋值

4 显示所有数组

5 引用数组

6 删除数组

7 数组数据处理

8 关联数组

9 举例

4字符串处理

1基于偏移量取字符串

2 基于模式取子串

3 查找替换

4 查找并删除

5字符大小写转换

6变量扩展

5高级变量

1 高级变量赋值

2 高级变量用法-有类型变量

3 变量间接引用


1函数 function

1.1函数介绍

函数function是由若干条shell命令组成的语句块,实现代码重用和模块化编程
它与shell程序形式上是相似的,不同的是它不是一个单独的进程,不能独立运行,而是shell程序的一部分
函数和shell程序区别:
        Shell程序在子Shell中运行
        函数在当前Shell中运行。因此在当前Shell中,函数可对shell中变量进行修改

1.2管理函数

函数由两部分组成:函数名和函数体
帮助参看:help function

1.3 定义函数

#语法一:
func_name (){
    ...函数体...
}

#语法二:
function func_name {
    ...函数体...
}

#语法三:
function func_name () {
    ...函数体...
}

1.4查看函数

#查看当前已定义的函数名
declare -F
#查看当前已定义的函数定义
declare -f
#查看指定当前已定义的函数名
declare -f func_name
#查看当前已定义的函数名定义
declare -F func_name

1.5删除函数

#格式:
 unset func_name

1.6函数调用

函数的调用方式

可在交互式环境下定义函数
可将函数放在脚本文件中作为它的一部分
可放在只包含函数的单独文件中
调用:函数只有被调用才会执行,通过给定函数名调用函数,函数名出现的地方,会被自动替换为函数代码
函数的生命周期:被调用时创建,返回时终止

1.6.1 交互式环境调用函数
交互式环境下定义和使用函数

#例:
[root@centos8 ~]#dir() {
> ls -l
> }
[root@centos8 ~]#dir
total 4
-rw-------. 1 root root 1559 Nov  7 19:33 anaconda-ks.cfg 


#例:实现判断CentOS的主版本
[root@centos8 ~]#centos_version() {
> sed -rn 's#^.* +([0-9]+)\..*#\1#p' /etc/redhat-release
> }
[root@centos8 ~]#centos_version
8

1.6.2在脚本中定义及使用函数

函数在使用前必须定义,因此应将函数定义放在脚本开始部分,直至shell首次发现它后才能使用,调用函数仅使用其函数名即可

[root@centos8 ~]#cat func1.sh
#!/bin/bash
#name:func1
hello(){
echo "Hello there today's date is `date +%F`"
}
echo "now going to the function hello"
hello
echo "back from the function"
[root@centos8 ~]#./func1.sh
now going to the function hello
Hello there today's date is 2019-12-18
back from the function


#例:
#!/bin/bash
disable_selinux(){
sed -i.bak 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
echo "SElinux已禁用,重新启动后才可生效"
}
disable_firewall(){
systemctl disable --now firewalld &> /dev/null
echo "防火墙已禁用"
}
set_ps1() {
echo "PS1='\[\e[1;35m\][\u@\h \W]\\$\[\e[0m\]'" > /etc/profile.d/reset.sh
echo "提示符已修改成功,请重新登录生效"
}
set_eth(){
sed -i.bak  '/GRUB_CMDLINE_LINUX=/s#"$# net.ifnames=0"#' /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg &> /dev/null
echo "网络名称已修改成功,请重新启动才能生效"
}
PS3="请选择相应的编号(1-6): "
MENU='
禁用SELinux
关防火墙
修改提示符
修改网卡名
以上全实现
退出
'
select M in $MENU ;do
case $REPLY in
1)
disable_selinux
;;
2)
disable_firewall
;;
3)
set_ps1
;;
4)
set_eth
;;
5)
 disable_selinux
disable_firewall
set_ps1
set_eth
;;
6)
break
;;
*)
echo "请输入正确的数字"
esac
done

1.6.3 使用函数文件

可以将经常使用的函数存入一个单独的函数文件,然后将函数文件载入shell,再进行调用函数
函数文件名可任意选取,但最好与相关任务有某种联系,例如:functions
一旦函数文件载入shell,就可以在命令行或脚本中调用函数。可以使用delcare -f 或set 命令查看所有
定义的函数,其输出列表包括已经载入shell的所有函数
若要改动函数,首先用unset命令从shell中删除函数。改动完毕后,再重新载入此文件
实现函数文件的过程:
1. 创建函数文件,只存放函数的定义
2. 在shell脚本或交互式shell中调用函数文件

#格式:
. filename
source  filename 


#例:
[root@centos8 ~]#cat functions
#!/bin/bash
#functions
hello(){
  echo Run hello Function
}
hello2(){
  echo Run hello2 Function
}
[root@centos8 ~]#. functions
[root@centos8 ~]#hello
Run hello Function
[root@centos8 ~]#hello2
Run hello2 Function
[root@centos8 ~]#declare -f hello hello2
hello ()
{
  echo Run hello Function
}
hello2 ()
{
  echo Run hello2 Function
}




#例:
[root@centos8 script40]#cat reset.sh
#!/bin/bash
. /etc/init.d/functions
disable_selinux(){
sed -i.bak 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
action "SElinux已禁用,重新启动后才可生效"
}
disable_firewall(){
systemctl disable --now firewalld &> /dev/null
action "防火墙已禁用"
}
set_ps1() {
echo "PS1='\[\e[1;35m\][\u@\h \W]\\$\[\e[0m\]'" > /etc/profile.d/reset.sh
action "提示符已修改成功,请重新登录生效"
}
set_eth(){
sed -i.bak  '/GRUB_CMDLINE_LINUX=/s#"$# net.ifnames=0"#' /etc/default/grub
grub2-mkconfig -o /boot/grub2/grub.cfg &> /dev/null
action "网络名称已修改成功,请重新启动才能生效"
}
PS3="请选择相应的编号(1-6): "
MENU='
禁用SELinux
关防火墙
修改提示符
修改网卡名
以上全实现
退出
'
select M in $MENU ;do
case $REPLY in
1)
disable_selinux
;;
2)
disable_firewall
;;
3)
set_ps1
;;
4)
set_eth
;;
5)
 disable_selinux
disable_firewall
set_ps1
set_eth
;;
6)
break
;;
*)
echo "请输入正确的数字"
esac
done

1.7函数返回值

函数的执行结果返回值:
        使用echo等命令进行输出
        函数体中调用命令的输出结果
函数的退出状态码:
        默认取决于函数中执行的最后一条命令的退出状态码
        自定义退出状态码

#格式:
return 从函数中返回,用最后状态命令决定返回值
return 0 无错误返回
return 1-255 有错误返回

1.8环境函数

类拟于环境变量,也可以定义环境函数,使子进程也可使用父进程定义的函数

#定义环境函数:
export -f function_name
declare -xf function_name


#查看环境函数:
export -f
declare -xf

1.9函数参数

函数可以接受参数:
传递参数给函数:在函数名后面以空白分隔给定参数列表即可,如:testfunc arg1 arg2 ...
在函数体中当中,可使用$1, $2, ...调用这些参数;还可以使用$@, $*, $#等特殊变量

#例:实现进度条功能
[root@centos8 ~]#cat progress_chart.sh
#!/bin/bash
function print_chars()
{
  # 传入的第一个参数指定要打印的字符串
 local char="$1"
  # 传入的第二个参数指定要打印多少次指定的字符串
 local number="$2"
 local c
  for ((c = 0; c < number; ++c)); do
   printf "$char"
  done
}
COLOR=32
declare -i end=50
for ((i = 1; i <= end; ++i)); do
 printf "\e[1;${COLOR}m\e[80D["
 print_chars "#" $i
 print_chars " " $((end - i))
 printf "] %3d%%\e[0m" $((i * 2))
  sleep 0.1s
done
echo

2.0函数变量

变量作用域:
        普通变量:只在当前shell进程有效,为执行脚本会启动专用子shell进程;因此,本地变量的作用
        范围是当前shell脚本程序文件,包括脚本中的函数
        环境变量:当前shell和子shell有效
        本地变量:函数的生命周期;函数结束时变量被自动销毁

注意:
        如果函数中定义了普通变量,且名称和局部变量相同,则使用本地变量
        由于普通变量和局部变量会冲突,建议在函数中只使用本地变量

在函数中定义本地变量的方法

local NAME=VALUE

2.1函数递归

函数递归:函数直接或间接调用自身,注意递归层数,可能会陷入死循环
递归特点:

        函数内部自已调用自已
        必须有结束函数的出口语句,防止死循环

#例: 无出口的递归函数调用
[root@centos8 ~]#func () { echo $i;echo "run func";let i++; func; }
[root@centos8 ~]#func



#递归示例:
阶乘是基斯顿·卡曼于 1808 年发明的运算符号,是数学术语
一个正整数的阶乘(factorial)是所有小于及等于该数的正整数的积,并且0和1的阶乘为1,自然数n的
阶乘写作n!
n!=1×2×3×...×n
阶乘亦可以递归方式定义:0!=1,n!=(n-1)!×n
n!=n(n-1)(n-2)...1
n(n-1)! = n(n-1)(n-2)!



#cat fact.sh
#!/bin/bash
#
fact() {
  if [ $1 -eq 0 -o $1 -eq 1 ]; then
echo 1
  else
echo $[$1*$(fact $[$1-1])]
  fi
}
fact $1


#例: 测试递归的嵌套深度
[root@centos8 ~]#test(){
let i++
echo i=$i
test
}
[root@centos8 ~]#test



#fork 炸弹是一种恶意程序,它的内部是一个不断在 fork 进程的无限循环,实质是一个简单的递归程
序。由于程序是递归的,如果没有任何限制,这会导致这个简单的程序迅速耗尽系统里面的所有资源
参考:https://en.wikipedia.org/wiki/Fork_bomb

#函数实现
:(){ :|:& };:
bomb() { bomb | bomb & }; bomb


#脚本实现
cat Bomb.sh
#!/bin/bash
./$0|./$0&

2 其它脚本相关工具

1 信号捕捉 trap

trap 命令可以捕捉信号,修改信号原来的功能,实现自定义功能

#进程收到系统发出的指定信号后,将执行自定义指令,而不会执行原操作
trap '触发指令' 信号

#忽略信号的操作
trap '' 信号

#恢复原信号的操作
trap '-' 信号

#列出自定义信号操作
trap -p

#当脚本退出时,执行finish函数
trap finish EXIT


#例:
#!/bin/bash
trap "echo 'Press ctrl+c or ctrl+\ '" int quit
trap -p
for((i=0;i<=10;i++))
do
    sleep 1
    echo $i
done
trap '' int
trap -p
for((i=11;i<=20;i++))
do
    sleep 1
    echo $i
done
trap '-' int
trap -p
for((i=21;i<=30;i++))
do
    sleep 1
    echo $i
done


#例: 当脚本正常或异常退出时,也会执行finish函数
[root@centos8 ~]#cat trap_exit.sh
#!/bin/bash
finish(){
    echo finish| tee -a /root/finish.log
}
trap finish exit

while true ;do
    echo running
    sleep 1
done

2 创建临时文件 mktemp

mktemp 命令用于创建并显示临时文件,可避免冲突

#格式:
mktemp [OPTION]... [TEMPLATE]
说明:TEMPLATE: filenameXXX,X至少要出现三个


#常见选项:
-d     #创建临时目录
-p     DIR或--tmpdir=DIR  #指明临时文件所存放目录位置


#例:
[root@centos8 ~]#mktemp
/tmp/tmp.UogGCumh8C
[root@centos8 ~]#mktemp /tmp/testXXX
[root@centos8 ~]#tmpdir=`mktemp -d /tmp/testdirXXX`
[root@centos8 ~]#mktemp --tmpdir=/testdir testXXXXXX


#例:实现文件垃圾箱
#方法1:脚本实现
[root@centos8 ~]#cat /data/scripts/rm.sh
#!/bin/bash
DIR=`mktemp -d /tmp/trash-$(date +%F_%H-%M-%S)XXXXXX`
mv $* $DIR
echo $* is move to $DIR
[root@centos8 ~]#alias rm=/data/scripts/rm.sh


#方法2:函数实现
[root@centos8 ~]#function rm () { local trash=`mktemp -d /tmp/trashXXXX`;mv $*
$trash; }

3 安装复制文件 install

install 功能相当于cp,chmod,chown,chgrp ,mkdir 等相关工具的集合

#格式:
install [OPTION]... [-T] SOURCE DEST 单文件
install [OPTION]... SOURCE... DIRECTORY
install [OPTION]... -t DIRECTORY SOURCE...
install [OPTION]... -d DIRECTORY... #创建空目录


#常用选项:
-m MODE,默认755
-o OWNER
-g GROUP
-d DIRNAME 目录


#例:
[root@centos8 ~]#install -m 700 -o xin -g admins srcfile desfile
[root@centos8 ~]#install -m 770 -d /testdir/installdir
[root@centos8 ~]#install -m 600 -o xin -g bin anaconda-ks.cfg /data/a.cfg
[root@centos8 ~]#ll /data/a.cfg
-rw------- 1 xin bin 1550 Jun 23 20:28 /data/a.cfg
[root@centos8 ~]#install -m 700 -o xinxin -g daemon -d /data/testdir
[root@centos8 ~]#ll -d /data/testdir
drwx------ 2 xinxin daemon 6 Apr 29 15:09 /data/testdir

4 交互式转化批处理工具 expect

expect 是由Don Libes基于 Tcl( Tool Command Language )语言开发的,主要应用于自动化交互式操作的场景,借助 expect 处理交互的命令,可以将交互过程如:ssh登录,ftp登录等写在一个脚本上,使之自动化完成。尤其适用于需要对多台服务器执行相同操作的环境中,可以大大提高系统管理人员的工作效率

#例: 安装expect 及mkpasswd 工具
[root@centos8 ~]#yum -y install expect
[root@centos8 ~]#rpm -ql expect|head
/usr/bin/autoexpect
/usr/bin/dislocate
/usr/bin/expect
/usr/bin/ftp-rfc
/usr/bin/kibitz
/usr/bin/lpunlock
/usr/bin/mkpasswd
/usr/bin/passmass
/usr/bin/rftp
/usr/bin/rlogin-cwd
[root@centos8 ~]#mkpasswd
mghe5J&9A
[root@centos8 ~]#mkpasswd
XhyQ67uo=
[root@centos8 ~]#mkpasswd -l 15 -d 3 -C 5
9T{htJmcA7pgCJ2



#语法:expect [选项] [ -c cmds ] [ [ -[f|b] ] cmdfile ] [ args ]


#常用选项:
-c:从命令行执行expect脚本,默认expect是交互地执行的
-d:可以调试信息


#例:
expect  -c 'expect "\n" {send "pressed enter\n"}'
expect  -d ssh.exp


#expect中相关命令
    spawn 启动新的进程
    expect 从进程接收字符串
    send 用于向进程发送字符串
    interact 允许用户交互
    exp_continue 匹配多个字符串在执行动作后加此命令
expect最常用的语法(tcl语言:模式-动作)


#单一分支模式语法:
[root@centos8 test]#expect
expect1.1> expect "hi" {send "You said hi\n"}
hahahixixi
You said hi
匹配到hi后,会输出“you said hi”,并换行


#多分支模式语法:
[root@centos8 ~]#expect
expect1.1> expect "hi" { send "You said hi\n" } "hehe" { send "Hehe yourself\n"
} "bye" { send "Good bye\n" }
hehe
Hehe yourself
expect1.2> expect "hi" { send "You said hi\n" } "hehe" { send "Hehe yourself\n"
} "bye" { send "Good bye\n" }
bye
Good bye
expect1.3> expect "hi" { send "You said hi\n" } "hehe" { send "Hehe yourself\n"
} "bye" { send "Good bye\n" }
hi
You said hi
expect1.4>

#匹配hi,hello,bye任意字符串时,执行相应输出。等同如下:  
expect {
    "hi" { send "You said hi\n"}
    "hehe" { send "Hehe yourself\n"}
    "bye" { send  " Good bye\n"}
}

[root@centos8 ~]#expect
expect1.1> expect {
+> "hi" { send "You said hi\n"}
+> "hehe" { send "Hehe yourself\n"}
+> "bye" { send  " Good bye\n"}
+> }
bye
  Good bye
expect1.2> 



#例:非交互式复制文件
#!/usr/bin/expect
spawn scp /etc/redhat-release 10.0.0.7:/data
expect {
    "yes/no" { send "yes\n";exp_continue }
    "password" { send  "xinxin\n" }
}
expect eof



#例:自动登录
#!/usr/bin/expect
spawn ssh 10.0.0.7
expect {
    "yes/no" { send "yes\n";exp_continue }
    "password" { send  "xinxin\n" }
}
interact



#例:expect 变量
#!/usr/bin/expect
set ip 10.0.0.7
set user root
set password xinxin
set timeout 10
spawn ssh $user@$ip
expect {
    "yes/no" { send "yes\n";exp_continue }
    "password" { send "$password\n" }
}
interact


#例:expect 位置参数
[root@centos8 ~]#cat expect4
#!/usr/bin/expect
set ip [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
spawn ssh $user@$ip
expect {
    "yes/no" { send "yes\n";exp_continue }
    "password" { send "$password\n" }
}
interact
[root@centos8 ~]#./expect4 10.0.0.7 root xinxin
spawn ssh root@10.0.0.7
root@10.0.0.7's password:
Last login: Wed Apr 29 15:34:14 2020 from 10.0.0.8
[root@centos7 ~]#exit
logout
Connection to 10.0.0.7 closed.


#例:expect 执行多个命令
#!/usr/bin/expect
set ip [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]
set timeout 10
spawn ssh $user@$ip
expect {
    "yes/no" { send "yes\n";exp_continue }
    "password" { send "$password\n" }
}
expect "]#" { send "useradd haha\n" }
expect "]#" { send "echo xinxin |passwd --stdin haha\n" }
send "exit\n"
expect eof
#./ssh4.exp 10.0.0.7 root xinxin 


#例:shell脚本调用 expect
#!/bin/bash
ip=$1
user=$2
password=$3
expect <<EOF
set timeout 20
spawn ssh $user@$ip
expect {
    "yes/no" { send "yes\n";exp_continue }
    "password" { send "$password\n" }
}
expect "]#" { send "useradd hehe\n" }
expect "]#" { send "echo xinxin |passwd --stdin hehe\n" }
expect "]#" { send "exit\n" }
expect eof
EOF
#./ssh5.sh 192.168.8.10 root xinxin 



#例:shell脚本利用循环调用expect在CentOS和Ubuntu上批量创建用户
#!/bin/bash
NET=10.0.0
user=root
password=xinxin
IPLIST="
7
18
101
"
for ID in $IPLIST;do
ip=$NET.$ID
expect <<EOF
set timeout 20
spawn ssh $user@$ip
expect {
    "yes/no" { send "yes\n";exp_continue }
    "password" { send "$password\n" }
}
expect "#" { send "useradd test\n" }
expect "#" { send "exit\n" }
expect eof
EOF
done


#例:
[root@centos8 scripts]#cat expect8.sh
#!/bin/bash
NET=10.0.0
user=root
password=xinxin
IPLIST="
7
18
"
for ID in $IPLIST ;do
ip=$NET.$ID
expect <<EOF
set timeout 20
spawn ssh $user@$ip
expect {
    "yes/no" { send "yes\n";exp_continue }
    "password" { send "$password\n" }
}
expect "#" { send "sed -i 's/^SELINUX=enforcing/SELINUX=disabled/'
/etc/selinux/config\n" }
expect "#" { send "setenforce 0\n" }
expect "#" { send "exit\n" }
expect eof
EOF
done

3 数组 array

1 数组介绍

变量:存储单个元素的内存空间
数组:存储多个元素的连续的内存空间,相当于多个变量的集合
数组名和索引
        索引的编号从0开始,属于数值索引
        索引可支持使用自定义的格式,而不仅是数值格式,即为关联索引,bash 4.0版本之后开始支持
        bash的数组支持稀疏格式(索引不连续)

2 声明数组

#普通数组可以不事先声明,直接使用
declare -a ARRAY_NAME
#关联数组必须先声明,再使用
declare -A ARRAY_NAME

#注意:两者不可相互转换

3 数组赋值

数组元素的赋值

(1) 一次只赋值一个元素

ARRAY_NAME[INDEX]=VALUE


#例:
weekdays[0]="Sunday"
weekdays[4]="Thursday"

(2) 一次赋值全部元素

ARRAY_NAME=("VAL1" "VAL2" "VAL3" ...)


#例:
title=("ceo" "coo" "cto")
num=({0..10})
alpha=({a..g})
file=( *.sh )

(3) 只赋值特定元素

ARRAY_NAME=([0]="VAL1" [3]="VAL2" ...)

(4) 交互式数组值对赋值

read -a ARRAY
#例:
[root@centos8 ~]#declare -A course
[root@centos8 ~]#declare -a course
-bash: declare: course: cannot convert associative to indexed array
[root@centos8 ~]#file=( *.sh )
[root@centos8 ~]#declare -A file
-bash: declare: file: cannot convert indexed to associative array


#例:
[root@ubuntu1804 ~]#i=a
[root@ubuntu1804 ~]#j=1
[root@ubuntu1804 ~]#declare -A arr
[root@ubuntu1804 ~]#arr[$i$j]=xinxin
[root@ubuntu1804 ~]#j=2
[root@ubuntu1804 ~]#arr[$i$j]=xin
[root@ubuntu1804 ~]#echo ${arr[*]}
xin xinxin
[root@ubuntu1804 ~]#echo ${arr[a1]}
xinxin
[root@ubuntu1804 ~]#echo ${arr[a2]}
xin 

4 显示所有数组

#显示所有数组:
declare -a


#例:
[root@centos8 ~]#declare -a
declare -a BASH_ARGC=()
declare -a BASH_ARGV=()
declare -a BASH_COMPLETION_VERSINFO=([0]="2" [1]="7")
declare -a BASH_LINENO=()
declare -ar BASH_REMATCH=()
declare -a BASH_SOURCE=()
declare -ar BASH_VERSINFO=([0]="4" [1]="4" [2]="19" [3]="1" [4]="release"
[5]="x86_64-redhat-linux-gnu")
declare -a DIRSTACK=()
declare -a FUNCNAME
declare -a GROUPS=()
declare -a PIPESTATUS=([0]="0")

5 引用数组

引用特定的数组元素

${ARRAY_NAME[INDEX]}
#如果省略[INDEX]表示引用下标为0的元素


#例:
[root@centos8 ~]#declare -a title=([0]="ceo" [1]="coo" [2]="cto")
[root@centos8 ~]#echo ${title[1]}
coo
[root@centos8 ~]#echo ${title}
ceo
[root@centos8 ~]#echo ${title[2]}
cto
[root@centos8 ~]#echo ${title[3]}

引用数组所有元素

${ARRAY_NAME[*]}
${ARRAY_NAME[@]}


#例:
[root@centos8 ~]#echo ${title[@]}
ceo coo cto
[root@centos8 ~]#echo ${title[*]}
ceo coo cto

数组的长度,即数组中元素的个数

${#ARRAY_NAME[*]}
${#ARRAY_NAME[@]}



#例:
[root@centos8 ~]#echo ${#title[*]}
3

数组的所有下标

${!ARRAY_NAME[*]}
${!ARRAY_NAME[@]}


#例:
[root@centos8 ~]#declare -a title=([0]="ceo" [1]="coo" [2]="cto")
[root@centos8 ~]#echo ${!title[@]}
0 1 2
[root@centos8 ~]#echo ${!title[*]}
0 1 2

6 删除数组

#删除数组中的某元素,会导致稀疏格式
unset ARRAY[INDEX]

#例:
[root@centos8 ~]#echo ${title[*]}
ceo coo cto
[root@centos8 ~]#unset title[1]
[root@centos8 ~]#echo ${title[*]}
ceo cto



#删除整个数组
unset ARRAY

#例:
[root@centos8 ~]#unset title
[root@centos8 ~]#echo ${title[*]}
[root@centos8 ~]#

7 数组数据处理

#数组切片:
${ARRAY[@]:offset:number}
${ARRAY[*]:offset:number}
offset #要跳过的元素个数
number #要取出的元素个数
#取偏移量之后的所有元素
{ARRAY[@]:offset}
{ARRAY[*]:offset}



#例:
[root@centos8 ~]#num=({0..10})
[root@centos8 ~]#echo ${num[*]:2:3}
2 3 4
[root@centos8 ~]#echo ${num[*]:6}
6 7 8 9 10


#向数组中追加元素:
ARRAY[${#ARRAY[*]}]=value
ARRAY[${#ARRAY[@]}]=value


#例:
[root@centos8 ~]#num[${#num[@]}]=11
[root@centos8 ~]#echo ${#num[@]}
12
[root@centos8 ~]#echo ${num[@]}
0 1 2 3 4 5 6 7 8 9 10 11

8 关联数组

注意:关联数组必须先声明再调用

declare -A ARRAY_NAME
ARRAY_NAME=([idx_name1]='val1' [idx_name2]='val2‘...)


#例:关联数组
[root@centos8 ~]#declare -A student
[root@centos8 ~]#student[name1]=lijun
[root@centos8 ~]#student[name2]=ziqing
[root@centos8 ~]#student[age1]=18
[root@centos8 ~]#student[age2]=16
[root@centos8 ~]#student[gender1]=m
[root@centos8 ~]#student[city1]=nanjing
[root@centos8 ~]#student[gender2]=f
[root@centos8 ~]#student[city2]=anhui
[root@centos8 ~]#student[gender2]=m
[root@centos8 ~]#student[name50]=alice
[root@centos8 ~]#student[name3]=tom
[root@centos8 ~]#for i in {1..50};do echo student[name$i]=${student[name$i]};
done


#例: 显示关联数组的所有下标
[root@centos8 ~]#declare -A title=([ceo]=xinxin [coo]=zhang [cto]=xin)
[root@centos8 ~]#echo ${!title[@]}
ceo cto coo

9 举例

#例:生成10个随机数保存于数组中,并找出其最大值和最小值
#!/bin/bash
declare -i min max
declare -a nums
for ((i=0;i<10;i++));do
nums[$i]=$RANDOM
[ $i -eq 0 ] && min=${nums[0]} &&  max=${nums[0]}&& continue
[ ${nums[$i]} -gt $max ] && max=${nums[$i]} && continue
[ ${nums[$i]} -lt $min ] && min=${nums[$i]}
done
echo "All numbers are ${nums[*]}"
echo Max is $max
echo Min is $min



#例:编写脚本,定义一个数组,数组中的元素对应的值是/var/log目录下所有以.log结尾的文件;统计
出其下标为偶数的文件中的行数之和
#!/bin/bash
#
declare -a files
files=(/var/log/*.log)
declare -i lines=0
for i in $(seq 0 $[${#files[*]}-1]); do
  if [ $[$i%2] -eq 0 ];then
let lines+=$(wc -l ${files[$i]} | cut -d' ' -f1)
  fi
done
echo "Lines: $lines"

4字符串处理

1基于偏移量取字符串

#返回字符串变量var的字符的长度,一个汉字算一个字符
${#var}

#返回字符串变量var中从第offset个字符后(不包括第offset个字符)的字符开始,到最后的部分,
offset的取值在0 到 ${#var}-1 之间(bash4.2后,允许为负值)
${var:offset}

#返回字符串变量var中从第offset个字符后(不包括第offset个字符)的字符开始,长度为number的部分
${var:offset:number}

#取字符串的最右侧几个字符,取字符串的最右侧几个字符, 注意:冒号后必须有一空白字符
${var: -length}

#从最左侧跳过offset字符,一直向右取到距离最右侧lengh个字符之前的内容,即:掐头去尾
${var:offset:-length}

#先从最右侧向左取到length个字符开始,再向右取到距离最右侧offset个字符之间的内容,注意:-
length前空格,并且length必须大于offset
${var: -length:-offset}


#例:
[root@centos8 script40]#str=abcdef我你他
[root@centos8 script40]#echo ${#str}
9
[root@centos8 script40]#echo ${str:2}
cdef我你他
[root@centos8 script40]#echo ${str:2:3}
cde
[root@centos8 script40]#echo ${str:-3}
abcdef我你他
[root@centos8 script40]#echo ${str: -3}
我你他
[root@centos8 script40]#echo ${str:2:-3}
cdef
[root@centos8 script40]#echo ${str: -2:-3}
-bash: -3: substring expression < 0
[root@centos8 script40]#echo ${str: -3:-2}
我
[root@centos8 script40]#echo ${str:-3:-2}
abcdef我你他
[root@centos8 script40]#echo ${str: -3:-2}
我
[root@centos8 script40]#echo ${str: -5:-2}
ef我

2 基于模式取子串

##其中word可以是指定的任意字符,自左而右,查找var变量所存储的字符串中,第一次出现的word, 删除字
符串开头至第一次出现word字符串(含)之间的所有字符,即懒惰模式,以第一个word为界删左留右
${var#*word}

#从var变量的值中删除以word开头的部分
${var#word}

#同上,贪婪模式,不同的是,删除的是字符串开头至最后一次由word指定的字符之间的所有内容,即贪婪模
式,以最后一个word为界删左留右
${var##*word}
${var##word}


#例:
[root@centos8 ~]#file="var/log/messages"
[root@centos8 ~]#echo ${file#*/}
log/messages
[root@centos8 ~]#echo ${file##*/}
messages




#其中word可以是指定的任意字符,功能:自右而左,查找var变量所存储的字符串中,第一次出现的word,
删除字符串最后一个字符向左至第一次出现word字符串(含)之间的所有字符,即懒惰模式,以从右向左的第
一个word为界删右留左
${var%word*}
${var%word}
#同上,只不过删除字符串最右侧的字符向左至最后一次出现word字符之间的所有字符,即贪婪模式,以从右向
左的最后一个word为界删右留左
${var%%word*}
${var%%word}



#例:
[root@centos8 ~]#file="var/log/messages"
[root@centos8 ~]#echo ${file%/*}
var/log
[root@centos8 ~]#echo ${file%%/*}
var


#例:
[root@centos8 ~]#url=http://www.baidu.com:8080
[root@centos8 ~]#echo ${url##*:}
8080
[root@centos8 ~]#echo ${url%%:*}
http

3 查找替换

#查找var所表示的字符串中,第一次被pattern所匹配到的字符串,以substr替换之
${var/pattern/substr}

#查找var所表示的字符串中,所有能被pattern所匹配到的字符串,以substr替换之
${var//pattern/substr}

#查找var所表示的字符串中,行首被pattern所匹配到的字符串,以substr替换之
${var/#pattern/substr}

#查找var所表示的字符串中,行尾被pattern所匹配到的字符串,以substr替换之
${var/%pattern/substr}

4 查找并删除

#删除var表示的字符串中第一次被pattern匹配到的字符串
${var/pattern}

#删除var表示的字符串中所有被pattern匹配到的字符串
${var//pattern}

#删除var表示的字符串中所有以pattern为行首匹配到的字符串
${var/#pattern}

#删除var所表示的字符串中所有以pattern为行尾所匹配到的字符串
${var/%pattern}

5字符大小写转换

#把var中的所有小写字母转换为大写
${var^^}

#把var中的所有大写字母转换为小写
${var,,}

6变量扩展

#扩展以所有prefix开头的变量
${!prefix*}
${!prefix@}


#例:
[root@centos8 ~]#file1=a.txt
[root@centos8 ~]#file2=b.txt
[root@centos8 ~]#file3=c.txt
[root@centos8 ~]#echo ${!file*}
file1 file2 file3
[root@centos8 ~]#echo ${!file@}
file1 file2 file3

5高级变量

1 高级变量赋值

#例:
[root@centos8 ~]#title=ceo
[root@centos8 ~]#name=${title-xin}
[root@centos8 ~]#echo $name
ceo
[root@centos8 ~]#title=
[root@centos8 ~]#name=${title-xin}
[root@centos8 ~]#echo $name
[root@centos8 ~]#unset title
[root@centos8 ~]#name=${title-xin}
[root@centos8 ~]#echo $name
xin




#例:
[root@centos8 ~]#title=ceo
[root@centos8 ~]#name=${title:-xin}
[root@centos8 ~]#echo $name
ceo
[root@centos8 ~]#title=
[root@centos8 ~]#name=${title:-xin}
[root@centos8 ~]#echo $name
xin
[root@centos8 ~]#unset title
[root@centos8 ~]#name=${title:-xin}
[root@centos8 ~]#echo $name
xin

2 高级变量用法-有类型变量

Shell变量一般是无类型的,但是bash Shell提供了declare和typeset两个命令用于指定变量的类型,两个命令是等价的

declare [选项] 变量名
选项:
-r 声明或显示只读变量
-i 将变量定义为整型数
-a 将变量定义为数组
-A 将变量定义为关联数组
-f 显示已定义的所有函数名及其内容
-F 仅显示已定义的所有函数名
-x 声明或显示环境变量和函数,相当于export
-l 声明变量为小写字母 declare -l var=UPPER
-u 声明变量为大写字母 declare -u var=lower
-n  make NAME a reference to the variable named by its value

3 变量间接引用

1 eval命令
eval命令将会首先扫描命令行进行所有的置换,然后再执行该命令。该命令适用于那些一次扫描无法实现其功能的变量,该命令对变量进行两次扫描

[root@centos8 ~]# CMD=whoami
[root@centos8 ~]# echo $CMD
whoami
[root@centos8 ~]# eval $CMD
root
[root@centos8 ~]# n=10    
[root@centos8 ~]# echo {0..$n}  
{0..10}
[root@centos8 ~]# eval echo {0..$n}
0 1 2 3 4 5 6 7 8 9 10
[root@centos8 ~]#for i in `eval echo {1..$n}` ;do echo i=$i ;done
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
i=10
[root@centos8 ~]#i=a
[root@centos8 ~]#j=1
[root@centos8 ~]#$i$j=hello
-bash: a1=hello: command not found
[root@centos8 ~]#eval $i$j=hello
[root@centos8 ~]#echo $i$j
a1
[root@centos8 ~]#echo $a1
hello

2 间接变量引用

如果第一个变量的值是第二个变量的名字,从第一个变量引用第二个变量的值就称为间接变量引用
variable1的值是variable2,而variable2又是变量名,variable2的值为value,间接变量引用是指通过variable1获得变量值value的行为

variable1=variable2
variable2=value
#示例:
i=1
$1=xin



#bash Shell提供了两种格式实现间接变量引用
#方法1
#变量赋值
eval tempvar=\$$variable1
#显示值
eval echo \$$variable1
eval echo '$'$variable1
#方法2
#变量赋值
tempvar=${!variable1}
#显示值
echo ${!variable1}


#例:
[root@centos8 ~]#ceo=name
[root@centos8 ~]#name=xinxin
[root@centos8 ~]#echo $ceo
name
[root@centos8 ~]#echo $$ceo
33722ceo
[root@centos8 ~]#echo $$
33722
[root@centos8 ~]#echo \$$ceo
$name
[root@centos8 ~]#eval echo \$$ceo
xinxin
[root@centos8 ~]#eval tmp=\$$ceo
[root@centos8 ~]#echo $tmp
xinxin
[root@centos8 ~]#echo ${!ceo}
xinxin


#例:
[root@server ~]# N1=N2
[root@server ~]# N2=xin
[root@server ~]# eval NAME=\$$N1
[root@server ~]# echo $NAME
xin
[root@server ~]# NAME=${!N1}
[root@server ~]# echo $NAME
xin


#例: 生成位置变量
[root@centos7 ~]#cat test.sh
#!/bin/bash
for i in {1..3};do
  echo ${!i}
  #eval echo \$$i
done
[root@centos7 ~]#bash test.sh a b c
a
b
c


#例: 批量创建用户
[root@centos8 ~]#cat create_user.sh
#!/bin/bash
n=$#
[ $n -eq 0 ] && { echo "Usage: `basename $0` username..." ; exit 2; }
for i in `eval echo {1..$n}`;do
  user=${!i}
 id $user &> /dev/null && echo $user is exist || { useradd $user; echo $user
is created; }
done
[root@centos8 ~]#bash create_user.sh hehe xixi haha
hehe is created
xixi is created
haha is created

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值