導語
- Linux 有時候需要
統計多臺主機
上面的數據,比如合并N多主機的日志,然后進行下一步的分析。這個時候如果直接把所有主機IP寫死到腳本
中的話,下次新增一臺主機,就需要再去修改腳本,如果涉及到多個腳本的話,這樣的操作將會大大浪費我們的時間
- 這個時候我們可以考慮把用到的主機列表配置到一個文件中,然后通過一個函數來解析它,以后修改就只用修改這個配置文件就好了
- 當然處理封裝到公共的腳本中外,我們也可以將其封裝成一個單獨的命令,詳見最后部分 作為定制化工具
公用函數腳本
listIniSections
Desc:
列舉配置文件中所有的section名稱
Usage:
listIniSections ini-config-file
listIniKeys
Desc:
列舉配置文件中 給定的section 下所有的key名稱
Usage:
listIniSections ini-config-file section-name
listIniValues
Desc:
列舉配置文件中 給定的section 下所有的value名稱
Usage:
listIniSections ini-config-file section-name
listIniKeysValues
Desc:
列舉配置文件中 給定的section 下所有的key 和 value名稱
Usage:
listIniSections ini-config-file section-name
腳本
## 定義一個公用的腳本,把常見的函數放到里面,別的腳本就可以引用然后調用
## 類似于面向對象語言的包含文件
vim /devOps/shell/common/functions
#!/usr/bin/env bash
##
## 2016-05-12
## 得到ini配置文件所有的sections名稱
## listIniSections "filename.ini"
##
listIniSections()
{
inifile="$1"
# echo "inifile:${inifile}"
# # exit
if [ $# -ne 1 ] || [ ! -f ${inifile} ]
then
echo "file [${inifile}] not exist!"
exit
else
sections=`sed -n '/\[*\]/p' ${inifile} |grep -v '^#'|tr -d []`
echo "${sections}"
fi
}
##
## 2016-05-12
## 得到ini配置文件給定section的所有key值
## ini中多個section用空行隔開
## listIniSections "filename.ini" "section"
##
listIniKeys()
{
inifile="$1"
section="$2"
if [ $# -ne 2 ] || [ ! -f ${inifile} ]
then
echo "ini file not exist!"
exit
else
keys=$(sed -n '/\['$section'\]/,/^$/p' $inifile|grep -Ev '\[|\]|^$'|awk -F'=' '{print $1}')
echo ${keys}
fi
}
##
## 2016-05-12
## 得到ini配置文件給定section的所有value值
## ini中多個section用空行隔開
## listIniSections "filename.ini" "section"
##
listIniValues()
{
inifile="$1"
section="$2"
if [ $# -ne 2 ] || [ ! -f ${inifile} ]
then
echo "ini file [${inifile}]!"
exit
else
values=$(sed -n '/\['$section'\]/,/^$/p' $inifile|grep -Ev '\[|\]|^$'|awk -F'=' '{print $2}')
echo ${values}
fi
}
## 2016-06-01
## 得到ini配置文件給定section的所有key - value值
## ini中多個section用空行隔開
## listIniSections "filename.ini" "section"
##
listIniKeysValues()
{
inifile="$1"
section="$2"
if [ $# -ne 2 ] || [ ! -f ${inifile} ]
then
echo "ini file [${inifile}]!"
exit
else
values=$(sed -n '/\['$section'\]/,/^$/p' $inifile|grep -Ev '\[|\]|^$'|awk -F'=' '{print $1, $2}')
echo ${values}
fi
}
具體實例
ini格式配置文件
root@pts/2 $ cat hostList.ini
[juepei]
web1=192.168.88.2
web2=192.168.88.15
[miaowu]
web1=192.168.200.2
web2=192.168.200.3
測試腳本
root@pts/0 $ cat 20160620.sh
#!/usr/bin/env bash
source /devOps/shell/common/functions
iniconfig="/tmp/liuchao/hostList.ini"
echo "list all sections"
listIniSections ${iniconfig}
echo -e "\nlist section [juepei] keys"
listIniKeys ${iniconfig} juepei
echo -e "\nlist section [juepei] values"
listIniValues ${iniconfig} juepei
echo -e "\nlist section [miaowu] keys and values"
listIniKeysValues ${iniconfig} juepei
測試結果
root@pts/0 $ bash 20160620.sh
list all sections
juepei
miaowu
list section [juepei] keys
web1 web2
list section [juepei] values
192.168.88.2 192.168.88.15
list section [miaowu] keys and values
web1 192.168.88.2 web2 192.168.88.15
<a name="md-anchor" id="md-anchor">作為定制化工具</a>
#!/usr/bin/env bash
# -*- coding: utf-8 -*-
#Filename: lc_parseINI
#Author: Liuchao
#Email: 137642091@qq.com
#Date: 2016-06-20
#Desc: Linux下解析INI 格式的配置文件
#
source /devOps/shell/common/functions
echo "$1 - $2 - $3"
## 如果沒有指定參數或者給出的參數大于2則輸入用法
if [ $# -gt 3 ] || [ $# -lt 1 ]
then
echo "Usage:`basename $0` [-s/-k/-v/-a] iniconfig [section]"
exit
else
# param1="$1"
# if [ $# -eq 1 ] && [ "${param1:0-3}" = "ini" ]
if [ $# -eq 1 ] && [ "${1:0-3}" = "ini" ]
then
listIniSections "$1"
elif [ $# -eq 2 ] && [ "$1" = "-s" ]
then
listIniSections "$2"
elif [ $# -eq 3 ] && [ "$1" = "-s" ]
then
listIniSections "$2"
elif [ $# -eq 3 ] && [ "$1" = "-a" ]
then
listIniKeysValues "$2" "$3"
elif [ $# -eq 3 ] && [ "$1" = "-k" ]
then
listIniKeys "$2" "$3"
elif [ $# -eq 3 ] && [ "$1" = "-v" ]
then
listIniValues "$2" "$3"
else
echo "You enter wrong params!"
echo "Usage:`basename $0` [-s/-k/-v/-a] iniconfig section"
exit
fi
fi
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。