shell编程

一:数组的声明

    declare -x varname 设置环境变量
    declare -i 声明整数
    declare -g 设置函数为全局函数
    declare -xf 设置环境函数
    declare -a 数组名 声明索引数组
    declare -A 数组名 声明关联数组
    declare -a 查看所有索引数组
    declare -A 查看所有关系数组


二:数组的赋值:

    title[0]=boss
    title[1]=ceo
    title[2]=coo
    title[3]=cto
[root@centos7 ~]# name=(liubei guanyu zhangfei zhaoyun)
[root@centos7 ~]# course=([0]=linux [2]=python [5]=oracle)
图片.png[root@centos7 ~]# read -a menu
huimian lamian yanroutang

script=(/root/t*.sh)
digit=({1..20})

同样支持把执行结果作为数组的输入。图片.png


三:数组的引用
[root@centos7 ~]# echo $digit  $数组名  仅引用第一个值
[root@centos7 ~]# echo ${digit[1]}  指定下标以引用指定值

引用所有值
[root@centos7 ~]# echo ${digit[*]}
[root@centos7 ~]# echo ${digit[@]}

打印数量
[root@centos7 ~]# echo ${#digit[@]}

引用最后一个值
[root@centos7 ~]# echo ${digit[$[${#digit[@]}-1]]}
图片.png练习:分别显示每一个值,每个值换一行。

[root@centos7 ~]# for n in $(seq 0 $[${#testshuzu[*]}-1]);do
> echo testshuzu[$n] is ${testshuzu[$n]}
> done
testshuzu[0] is liubei
testshuzu[1] is guanyu
testshuzu[2] is zhaofei
testshuzu[3] is huangzhong
testshuzu[4] is zhaoyun

在索引数组中加值,加到最后
[root@centos7 ~]# testshuzu[${#testshuzu[*]}]=machao


四:数组的删除

删除数组中的某个值
unset 数组名[下标值]

删除整个数组
unset 数组名


五:数组的切片
offset: 要跳过的元素个数
number: 要取出的元素个数
取偏移量之后的所有元素
[root@centos7 ~]# echo ${testshuzu[*]:1:3}
[root@centos7 ~]# echo ${testshuzu[*]:1}   跳过一个值,取到最后

图片.png


六:关联数组
两种方式:
一:先声明,再赋值
declare -A testshuzu
testshuzu=([zhugong]=liubei [erdi]=guanyu [sandi]=zhangfei)

二:声明,赋值一步到位
declare -A weiguo=([chengxiang]=caocao [junshi]=simayi [jiangju]=xiahoudun )  
图片.png

练习:写一个脚本会生成一个名为digit的数组包含10个随机数,显示数组的所有值,再显示最大值,最小值。
$RANDOM 0-32767
./digit.sh
All digit are .....
Max is
Min is

方法1:

fun1() {
for (( i=0;i<10;i++ ));do
        a=$(echo $RANDOM)
        digit[$i]=$a
        echo ${digit[$i]}
done
}
                fun1 |sort -nr > /app/shuzul
cat /app/shuzul
m=$( cat /app/shuzul |head -1)
n=$( cat /app/shuzul |tail -1)
echo max=$m
echo min=$n
rm /app/shuzul -rf

方法2:

#!/bin/bash
declare -i min max
declare -a digit
for ((i=0;i<10;i++));do
        digit[$i]=$RANDOM
        [ $i -eq 0 ] && min=${digit[0]} && max=${digit[0]} && continue
        [ ${digit[$i]} -gt $max ] && max=${digit[$i]} && continue
        [ ${digit[$i]} -lt $min ] && min=${digit[$i]} && continue
done
echo All digit are ${digit[*]}
echo Max is $max
echo Min is $min



练习:编写脚本,定义一个数组,数组中的元素是/var/log目录下所有以.log结尾的文件;统计出其下标为偶数的文件中的行数之和

方法1:

#!/bin/bash
Sum=0
File=(/var/log/*.log)
for ((i=0;i<=$[${#File[*]}-1];i++));do
        if [ $[$i%2] -eq 0 ];then
                Line=$(cat ${File[$i]} | wc -l)
                Sum=$[$Sum+$Line]
        fi
done
echo $Sum



方法2:

declare -a logfiles=(/var/log/*.log)
echo All logfile name ${logfiles[*]}
i=0
linesum=0
while [ $i -lt ${#logfiles[*]} ];do
        [ $[i%2] -eq 0 ]&& fileline=$(cat ${logfiles[$i]} |wc -l ) && let linesum+=$fileline
        let i++
done
echo All lines are $linesum


    


                    
七:字符串处理
echo ${#name} echo ${#name[1]}
    显示变量或数组的字符数
图片.png


[root@centos7 ~]# echo ${name}
[root@centos7 ~]# echo ${name:2:3}  从左边跳过2个字符,取3个字符
[root@centos7 ~]# echo ${name:2}   从左边跳过2个字符,取到最后
[root@centos7 ~]# echo ${name: -2}  从右向左取2个字符,注意冒号后至少打一个空格
[root@centos7 ~]# echo ${name: -3: 1}  从右向左取3个字符,再从左向右取1个字符
[root@centos7 ~]# echo ${name: -3: -1} 从右向左取3个字符,再从右向左去掉1个字符
[root@centos7 ~]# echo ${name:3:-1} 从左边跳过3个字符,从右边跳过1个字符

图片.png


从左至右查找
[root@centos7 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@centos7 ~]# echo ${PATH#*local}
/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@centos7 ~]# echo ${PATH##*local}   贪婪模式
/bin:/usr/sbin:/usr/bin:/root/bin

图片.png

从右边至左边查找
[root@centos7 ~]# echo ${PATH}
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@centos7 ~]# echo ${PATH%local*}
/usr/local/sbin:/usr/
[root@centos7 ~]# echo ${PATH%%local*}
/usr/
图片.png[root@centos7 ~]# url=http://www.magedu.com:8080
[root@centos7 ~]# echo ${url%%:*}
http
[root@centos7 ~]# echo ${url##*:}
8080

查找替换
${var/pattern/substr}:查找var所表示的字符串中,第
一次被pattern所匹配到的字符串,以substr替换之
${var//pattern/substr}: 查找var所表示的字符串中,
所有能被pattern所匹配到的字符串,以substr替换之
${var/#pattern/substr}:查找var所表示的字符串中,
行首被pattern所匹配到的字符串,以substr替换之
${var/%pattern/substr}:查找var所表示的字符串中,
行尾被pattern所匹配到的字符串,以substr替换之
图片.png
查找并删除
${var/pattern}:删除var所表示的字符串中第一次被
pattern所匹配到的字符串
${var//pattern}:删除var所表示的字符串中所有被
pattern所匹配到的字符串
${var/#pattern}:删除var所表示的字符串中所有以
pattern为行首所匹配到的字符串
${var/%pattern}:删除var所表示的字符串中所有以
pattern为行尾所匹配到的字符串
图片.png大小写转换
[root@centos7 ~]# name="Zhangfei LiuBeI"
[root@centos7 ~]# echo ${name,,}        
zhangfei liubei
[root@centos7 ~]# echo ${name^^}
ZHANGFEI LIUBEI
图片.pngShell变量一般是无类型的,但是bash Shell提供了declare和typeset两个命令用于指定变量的类型,两个命令是等价的
     declare [选项] 变量名
        -r 声明或显示只读变量
        -i 将变量定义为整型数
        -a 将变量定义为数组
        -A 将变量定义为关联数组
        -f 显示已定义的所有函数名及其内容
        -F 仅显示已定义的所有函数名
        -x 声明或显示环境变量和函数
        -l 声明变量为小写字母 declare –l var=UPPER
        -u 声明变量为大写字母 declare –u var=lower


八:eval
[root@centos7 ~]# mage=linux
[root@centos7 ~]# linux=centos
[root@centos7 ~]# eval echo \$$mage                    
centos
[root@centos7 ~]# centos=redhat
[root@centos7 ~]# eval echo \$$(eval echo \$$mage)     
redhat
图片.png
[root@centos7 ~]# n=10
[root@centos7 ~]# echo {1..$n}
{1..10}
[root@centos7 ~]# eval echo {1..$n}
1 2 3 4 5 6 7 8 9 10
图片.png[root@centos7 ~]# echo $mage
linux
[root@centos7 ~]# echo $linux
centos
[root@centos7 ~]# test=${!mage}
[root@centos7 ~]# echo $test
centos
图片.png


九:mktemp

mktemp命令:创建并显示临时文件,可避免冲突
    mktemp [OPTION]... [TEMPLATE]
     OPTION:
-d: 创建临时目录
-p DIR或--tmpdir=DIR:指明临时文件所存放目录位置
     示例:
mktemp /tmp/test.XXX
tmpdir=`mktemp –d /tmp/testdir.XXX`
mktemp --tmpdir=/testdir test.XXXXXX
图片.png



十:install命令:
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
图片.png

十一:expect
expect用于交互式转化为非交互式的过程中。是自动化运维的基础,这是一条很重要的命令!

#!/usr/bin/expect
spawn ssh [email protected]
expect {
        "yes/no" { send "yes\n" ;exp_continue } #捕获到yes/no 就将yes输入到交互式终端中
        "password" { send "centos\n" }
}
interact  #重新切换回交互式
#expect eof #expect 命令结束
#!/usr/bin/expect
set user root
set remotehost 172.18.253.51
set password centos
spawn ssh $user@$remotehost
expect {
        "yes/no" { send "yes\n" ;exp_continue }
        "password" { send "$password\n" }
}
interact
#expect eof

执行登录后的后续操作

#!/usr/bin/expect
set user [lindex $argv 0]
set remotehost [lindex $argv 1]
set password [lindex $argv 2]
spawn ssh $user@$remotehost
expect {
        "yes/no" { send "yes\n" ;exp_continue }
        "password" { send "$password\n" }
}
expect "]#" { send "useradd liubei\n" }
expect "]#" { send "echo redhat|passwd --stdin liubei\n" }
send "exit\n"
expect eof

 使用shell脚本格式

#!/bin/bash
user=$1
remotehost=$2
password=$3
expect << EOF
spawn ssh $user@$remotehost
expect {
        "yes/no" { send "yes\n" ;exp_continue }
        "password" { send "$password\n" }
}
expect "]#" { send "useradd liubei\n" }
expect "]#" { send "echo redhat|passwd --stdin liubei\n" }
send "exit\n"
expect eof
EOF


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章