shell編程常見知識點

shell數組實踐

#!/bin/bash

#常規操作打印數組內容
#array=(1 2 3 4 5)
#array=(`ls /tmp`)

array=(
192.168.56.10
192.168.56.12
192.168.56.13
192.168.56.14
192.168.56.15
)

for n in ${array[*]}
do
        echo $n
done

echo =========================

#c語言的方式來循環打印數組內容
#i爲數組下標
for ((i=0;i<${#array[*]};i++))
do
        echo ${array[i]}
done

shell編程常見知識點

數組和shell循環實現文本內容匹配
應用場景:
通過服務的路徑找到服務的名字,或者名字來對應找到路徑,可用於當服務名字和路徑不統一的時候,用於自動化的部署腳本。

#!/bin/bash

array=(
`awk -F '[ ]+' '{print  $2}'  a.txt`
)

for dir in ${array[*]}
do
        if [ $dir == "/root" ]
        then
              echo  當前的路徑是:$dir    對應的服務名字是: `grep ${dir} a.txt|awk '{print $1}'`
        fi
done
統一管理的文本示例
[root@linux-node1 scripts]# cat a.txt 
servicename   dir
aaa            /root
bbb            /data
ccc            /tmp
ddd            /opt 

[root@linux-node1 scripts]# sh deploy.sh 
“當前的路徑是:/root  對應的服務名字是:aaa
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章