一:數組的聲明
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)
[root@centos7 ~]# read -a menu
huimian lamian yanroutang
script=(/root/t*.sh)
digit=({1..20})
同樣支持把執行結果作爲數組的輸入。
三:數組的引用
[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]]}
練習:分別顯示每一個值,每個值換一行。
[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} 跳過一個值,取到最後
六:關聯數組
兩種方式:
一:先聲明,再賦值
declare -A testshuzu
testshuzu=([zhugong]=liubei [erdi]=guanyu [sandi]=zhangfei)
二:聲明,賦值一步到位
declare -A weiguo=([chengxiang]=caocao [junshi]=simayi [jiangju]=xiahoudun )
練習:寫一個腳本會生成一個名爲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]}
顯示變量或數組的字符數
[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個字符
從左至右查找
[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
從右邊至左邊查找
[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/
[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替換之
查找並刪除
${var/pattern}:刪除var所表示的字符串中第一次被
pattern所匹配到的字符串
${var//pattern}:刪除var所表示的字符串中所有被
pattern所匹配到的字符串
${var/#pattern}:刪除var所表示的字符串中所有以
pattern爲行首所匹配到的字符串
${var/%pattern}:刪除var所表示的字符串中所有以
pattern爲行尾所匹配到的字符串
大小寫轉換
[root@centos7 ~]# name="Zhangfei LiuBeI"
[root@centos7 ~]# echo ${name,,}
zhangfei liubei
[root@centos7 ~]# echo ${name^^}
ZHANGFEI LIUBEI
Shell變量一般是無類型的,但是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
[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
[root@centos7 ~]# echo $mage
linux
[root@centos7 ~]# echo $linux
centos
[root@centos7 ~]# test=${!mage}
[root@centos7 ~]# echo $test
centos
九: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
十: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
十一: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