23-2017-12-18-练习-脚本进阶服务

练习3-函数

1、编写函数,实现OS的版本判断

2、编写函数,实现取出当前系统eth0的IP地址

3、编写函数,实现打印绿色OK和红色FAILED

4、编写函数,实现判断是否无位置参数,如无参数,提示错误

# color
RED="\033[0;31m"
YELLOW="\033[0;33m"
GREEN="\033[1;32m"
DARKGREEN="\033[0;32m"
OCHRE="\033[38;5;95m"
BLUE="\033[1;34m"
WHITE="\033[0;37m"
RESET="\033[0m"

get_os_version(){
        echo ` cat /etc/system-release |sed -r 's@.*release ([0-9]+).*@\1@' `
}
get_ip_of_eth0(){
        echo    `ip a show dev eth0 | sed -nr '/[[:space:]]+inet /s@[[:space:]]+inet (.*)/.*@\1@p'`
}
print_ok(){
        echo -e ${GREEN}OK${RESET}
}
print_failed(){
        echo  -e ${RED}FAILED${RESET}
}
has_parameter(){
        if [ "$#" -eq 0 ] ; then
                echo "not a parameter"
                return 1
        fi

练习4-sysv

编写服务脚本/root/bin/testsrv.sh,完成如下要求
(1) 脚本可接受参数:start, stop, restart, status
(2) 如果参数非此四者之一,提示使用格式后报错退出
(3) 如是start:则创建/var/lock/subsys/ SCRIPT_NAME , 并显示“启动成功”
考虑:如果事先已经启动过一次,该如何处理?
(4) 如是stop:则删除/var/lock/subsys/ SCRIPT_NAME , 并显示“停止完成”
考虑:如果事先已然停止过了,该如何处理?
(5) 如是restart,则先stop, 再start
考虑:如果本来没有start,如何处理?
(6) 如是status, 则如果/var/lock/subsys/ SCRIPT_NAME 文件存在,则显示
“ SCRIPT_NAME is running...”
如果/var/lock/subsys/ SCRIPT_NAME 文件不存在,则显示“ SCRIPT_NAME
is stopped...”其中: SCRIPT_NAME 为当前脚本名
(7)在所有模式下禁止启动该服务,可用chkconfig 和 service命令管理
#!/bin/bash
# chkconfig:   345 99 99
# description:      this is a testsrv


. /etc/init.d/functions

lockfile=/var/lock/subsys/$0

start(){
    if [ -e $lockfile ] ; then
        action "$0 is running"
    else
        touch $lockfile
        if [ "$?" -eq 0 ] ; then
            action "$0  start"
        else
            action "$0  failed" false
        fi
    fi
}
stop(){
    if [ -e $lockfile ] ; then
        rm -rf $lockfile
        if [ "$?" -eq 0 ] ; then
            action "$0 stop"
        else
            action "$0 stop failed" false
        fi
    else
        action "$0 is stoped"
    fi
}
restart(){
    start
    stop
}
status(){
    if [ -e $lockfile ] ; then
        action "$0 running"
    else
        action "$0 stoped"
    fi
}
usage(){
    echo "$0 {start|stop|status|restart}"
}
case $1 in
start)
    start;;
stop)
    stop;;
restart)
    restart;;
status)
    status;;
*)
    usage;;
esac

练习5-copycmd

编写脚本/root/bin/copycmd.sh
(1) 提示用户输入一个可执行命令名称
(2) 获取此命令所依赖到的所有库文件列表
(3) 复制命令至某目标目录(例如/mnt/sysroot)下的对应路径下
如:/bin/bash ==> /mnt/sysroot/bin/bash
/usr/bin/passwd ==> /mnt/sysroot/usr/bin/passwd
(4) 复制此命令依赖到的所有库文件至目标目录下的对应路径下
: 如:/lib64/ld-linux-x86-64.so.2 ==>
/mnt/sysroot/lib64/ld-linux-x86-64.so.2
(5)每次复制完成一个命令后,不要退出,而是提示用户键入新
的要复制的命令,并重复完成上述功能;直到用户输入quit退出
#!/bin/bash
#================================================
#FileName   :copycmd.sh
#Author     :zhaojiedi
#Description:
#DateTime   :2017-12-22 15:57:39
#Version    :V1.0
#Other      :
#================================================
dst=/mnt/sysimage
overwrite=yes

if [ ! -e "$dst" ] ; then
        mkdir -pv $dst
fi
my_cp() {
        local src=$1
        local dst=$2
        local dir=`dirname $dst`
        if [ ! -e $dir ] ; then
                mkdir -pv $dir
        fi

        if [ ! -f "$dst"  -o "$overwrite" == "yes" ] ; then
                cp  $src $dst
        fi
}


while true; do
        read -p "enter you cmd :  " short_cmd
        if [ "$short_cmd" == "quit" ] ; then
                exit 0
        fi
        type $short_cmd 2>/dev/null |grep -q "builtin"
        if [ "$?" -eq 0 ] ; then
                echo "this is a buildin cmd, nothing todo,  input 'bash' for copy buildin cmd"
                continue
        fi
        short_cmd=`basename $short_cmd` &>/dev/null
        long_cmd=`which --skip-alias --skip-functions --skip-dot --skip-tilde $short_cmd 2>/dev/null`
        if [ "$?" -ne 0 ] ; then
                echo "not a valid command, please check and try again"
                continue
        fi
        # cp bin
        my_cp $long_cmd $dst$long_cmd
        # cp lib
        for lib in `ldd $long_cmd |egrep -o  "/[^[:space:]]+" ` ; do
                #echo -e "\n$lib\n"
                my_cp $lib $dst$lib
        done
        # cp man
        #for man in `man -aw $short_cmd` ; do
        #       my_cp $man ${dst}${man}
        #done
done

练习6-

1、编写函数实现两个数字做为参数,返回最大值斐波那契数列又称黄金分割数列,因数学家列昂纳多·斐波那
契以兔子繁殖为例子而引入,故又称为“兔子数列”,指的是
这样一个数列:0、1、1、2、3、5、8、13、21、34、……
,斐波纳契数列以如下被以递归的方法定义:F(0)=0,F(
1)=1,F(n)=F(n-1)+F(n-2)(n≥2)
利用函数,求n阶斐波那契数列
#!/bin/bash
#================================================
#FileName   :tuzishulie_46_zhaojiedi.sh
#Author     :zhaojiedi
#Description:
#DateTime   :2017-12-22 15:28:04
#Version    :V1.0
#Other      :
#================================================
f() {
        if [ "$1" -eq 0 ] ; then
                echo 0
                return 0
        fi
        if [ "$1" -eq 1 ] ; then
                echo 1
                return 0
        fi
        echo $[`f $[$1 -2]` + `f $[$1 -1]`]
}

echo ` f 10`
::
2、汉诺塔(又称河内塔)问题是源于印度一个古老传说。大梵天 创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上 按照大小顺序摞着64片黄金圆盘。大梵天命令婆罗门把圆盘 从下面开始按大小顺序重新摆放在另一根柱子上。并且规定, 在小圆盘上不能放大圆盘,在三根柱子之间一次只能移动一个 圆盘,利用函数,实现N片盘的汉诺塔的移动步骤
#!/bin/bash
f(){
        v1=$[$1%3]
        v2=$[$2%3]
        v=$[3-v1-v2]
        if [ "$3" -eq 1 ] ; then
                if  [ "$[$4%2]" -eq 1 ] ; then
                        echo -e "$v1=>$v2\n"
                else
                        echo  -e "$v=>$v2\n"
                fi
                return 0
        fi
        vv=$[$4-1]
        echo -e `f $v1 $v $[$3-1] $vv`
        echo -e "$v1=>$v2"
        echo  -e `f $v $v2 $[$3-1] $vv`
}
f2(){
        echo `f 0 2 $1 $1`
}
if [ "$#" -ne 1 ] ; then
        echo " must a parameter,only one "
fi

f2 $1

练习6-

1、输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序

#!/bin/bash
#================================================
#FileName   :test_bubble_sort_46_zhaojiedi.sh
#Author     :zhaojiedi
#Description:
#DateTime   :2017-12-26 17:49:51
#Version    :V1.0
#Other      :
#================================================

read  -p "enter some value "  -a arr

cnt=${#arr[*]}
echo "count="$cnt
my_print (){
        for i in `seq 0 $[cnt-1]`; do
                echo -ne "${arr[$i]}\t"
        done
        echo
}
echo "befor bubble sort"
my_print

for i in `seq 0 $[cnt-1]`; do
        for j in `seq 0 $[i-1]`;do
                if [ "${arr[$i]}" -lt "${arr[$j]}" ] ; then
                        tmp=${arr[$i]}
                        arr[$i]=${arr[$j]}
                        arr[$j]=$tmp
                fi
        done
done

echo "after bubble sort"
my_print

2、将下图所示,实现转置矩阵matrix.sh

1 2 3          1 4 7
4 5 6  ===>    2 5 8
7 8 9          3 6 9
#!/usr/bin/python
def trans(m):
        return zip(*m)

def my_print(m):
        for i in m :
                print i

m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print "before transform"
my_print(m)
m2= trans(m)
print "after transform"
my_print(m2)