#!/bin/bash
CONFIG_FILE=/home/iibull/desktop/Master/master.conf
TMP1_FILE=/home/iibull/desktop/Master/master.1.conf
TMP2_FILE=/home/iibull/desktop/Master/master.2.conf
function addUserProg() { #[ZhiLian = 直连程序]
#删除所有空白行
/bin/sed '/^$/d' ${CONFIG_FILE} > ${TMP1_FILE}
#替换原有的行
/bin/sed "s/^$1 = .*/$*/" ${TMP1_FILE} > ${TMP2_FILE}
#添加自己的行
/bin/echo $* >> ${TMP2_FILE}
#删除重复行
/usr/bin/awk '!a[$0]++' ${TMP2_FILE} > ${TMP1_FILE}
#覆盖原貌,使之生效
/bin/cp ${TMP1_FILE} ${CONFIG_FILE}
return 0;
}
function updUserProg() { #[ZhiLian = 直连程序]
addUserProg $*
}
function delUserProg() { #[ZhiLian]
#删除所有空白行
/bin/sed '/^$/d' ${CONFIG_FILE} > ${TMP1_FILE}
#删除行
/bin/sed "/^$1 = .*/d" ${TMP1_FILE} > ${TMP2_FILE}
#删除重复行
/usr/bin/awk '!a[$0]++' ${TMP2_FILE} > ${TMP1_FILE}
#覆盖原貌,使之生效
/bin/cp ${TMP1_FILE} ${CONFIG_FILE}
#delete source code
rm -rf /tmp/master/$1
return 0;
}
function getNumUserProg() { #[]
#删除所有空白行
/bin/sed '/^$/d' ${CONFIG_FILE} > ${TMP1_FILE}
#删除重复行
/usr/bin/awk '!a[$0]++' ${TMP1_FILE} > ${TMP2_FILE}
#覆盖原貌,使之生效
/bin/cp ${TMP2_FILE} ${CONFIG_FILE}
#获取行
LINE=`/bin/cat ${CONFIG_FILE} | wc -l`
echo $LINE
return 0
}
function getValueUserProg() { #[ZhiLian]
#删除所有空白行
/bin/sed '/^$/d' ${CONFIG_FILE} > ${TMP1_FILE}
#删除重复行
/usr/bin/awk '!a[$0]++' ${TMP1_FILE} > ${TMP2_FILE}
#覆盖原貌,使之生效
/bin/cp ${TMP2_FILE} ${CONFIG_FILE}
#获取行
LINE=`/bin/grep "^$1 = " ${CONFIG_FILE} | cut -d '=' -f 2`
echo $LINE
return 0
}
function getKeyUserProg() { #[直连程序]
#删除所有空白行
/bin/sed '/^$/d' ${CONFIG_FILE} > ${TMP1_FILE}
#删除重复行
/usr/bin/awk '!a[$0]++' ${TMP1_FILE} > ${TMP2_FILE}
#覆盖原貌,使之生效
/bin/cp ${TMP2_FILE} ${CONFIG_FILE}
#获取行
LINE=`/bin/grep " = $1" ${CONFIG_FILE} | cut -d '=' -f 1`
echo $LINE
return 0
}
function getIndexUserProg() { #[index]
#删除所有空白行
/bin/sed '/^$/d' ${CONFIG_FILE} > ${TMP1_FILE}
#删除重复行
/usr/bin/awk '!a[$0]++' ${TMP1_FILE} > ${TMP2_FILE}
#覆盖原貌,使之生效
/bin/cp ${TMP2_FILE} ${CONFIG_FILE}
#获取行
KEY=`/bin/sed -n "$1p" ${CONFIG_FILE} | cut -d '=' -f 1`
VALUE=`/bin/sed -n "$1p" ${CONFIG_FILE} | cut -d '=' -f 2`
echo $KEY
echo $VALUE
return 0
}
function swapUserProg() { #[index1, index2]
#删除所有空白行
/bin/sed '/^$/d' ${CONFIG_FILE} > ${TMP1_FILE}
#删除重复行
/usr/bin/awk '!a[$0]++' ${TMP1_FILE} > ${TMP2_FILE}
#获取行
NUMS=`getNumUserProg $*`
for(( i=1; i<=${NUMS}; i++ )); do
case $i in
$1)
/bin/sed -n $2p ${TMP2_FILE}
;;
$2)
/bin/sed -n $1p ${TMP2_FILE}
;;
*)
/bin/sed -n ${i}p ${TMP2_FILE}
;;
esac;
done > ${TMP1_FILE}
#覆盖原貌,使之生效
/bin/cp ${TMP1_FILE} ${CONFIG_FILE}
return 0;
}
CMD=$1
shift
if [ "X$CMD" = "Xadd" ]; then
addUserProg $* # sh add ZhiLian = 直连程序
elif [ "X$CMD" = "Xdel" ]; then
delUserProg $* # sh del ZhiLian
elif [ "X$CMD" = "Xupd" ]; then
updUserProg $* # sh add ZhiLian = 直连程序
elif [ "X$CMD" = "Xgetnum" ]; then
getNumUserProg $* # sh getnum
elif [ "X$CMD" = "Xgetvalue" ]; then
getValueUserProg $* # sh getvalue ZhiLian
elif [ "X$CMD" = "Xgetkey" ]; then
getKeyUserProg $* # sh getkey 直连程序
elif [ "X$CMD" = "Xgetindex" ]; then
getIndexUserProg $* # sh getkey 3
elif [ "X$CMD" = "Xswap" ]; then
swapUserProg $* # sh swap 3 5
fi
阅读(2779) | 评论(0) | 转发(0) |