bash脚本之练习

1、编写服务脚本/root/bin/testsrv.sh,完成如下要求 

(1) 脚本可接受参数:start, stop, restart, status 

(2) 如果参数非此四者之一,提示使用格式后报错退出

(3) 如是start:则创建/var/lock/subsys/SCRIPTNAME, 并显示“启动成功” 考虑:如果事先已经启动过一次,该如何处理?

(4) 如是stop:则删除/var/lock/subsys/SCRIPTNAME, 并显示“停止完成” 考虑:如果事先已然停止过了,该如何处理?

(5) 如是restart,则先stop, 再start 考虑:如果本来没有start,如何处理?

(6) 如是status, 则如果/var/lock/subsys/SCRIPTNAME文件存在,则显示“SCRIPTNAMEis running…” 如果/var/lock/subsys/SCRIPTNAME文件不存在,则显示“SCRIPTNAME isstopped…” 

其中:SCRIPT_NAME为当前脚本名

[root@localhost shel]# cat testsrv.sh
#!/bin/bash
#
#discription:server test script

cat << EOF
start)start succeed
stop)stop finished
restart)frist stop then start
status)running... or stopped...
==================================
EOF
read -p "input your chose: " n
prog=$(basename $0)
file=/var/lock/subsys/$prog

start(){
    if [ -f $file ];then
        echo "service is running."
    else
        touch $file
        echo "start succeed."
    fi
}   
stop(){
    if [ -f $file ];then
        rm -f $file
        echo "stop succeed."
    else
        echo "stop already."
    fi
}
status(){
    if [ -f $file ];then
        echo "$file is running..."
    else
        echo "$file is stopping..."
    fi
}
other(){
    echo "select error."
    exit
}
case $n in
start)
    start;;
stop)
    stop;;
restart)
    stop
    start;;
status)
    status;;
*)
    other;;
esac

2、编写脚本/root/bin/copycmd.sh 

(1) 提示用户输入一个可执行命令名称; 

(2) 获取此命令所依赖到的所有库文件列表 

(3) 复制命令至某目标目录(例如/root/testdir)下的对应路径下; 如:/bin/bash ==> /root/testdir/bin/bash /usr/bin/passwd==> /root/testdir/usr/bin/passwd 

(4) 复制此命令依赖到的所有库文件至目标目录下的对应路径下: 如:/lib64/ld-linux-x86-64.so.2 ==> /root/testdir/lib64/ld-linux-x86-64.so.2 

(5)每次复制完成一个命令后,不要退出,而是提示用户键入新的要复制的命令,并重复完成上述功能;直到用户输入quit退出

[root@localhost shell]# cat copycmd.sh
#!/bin/bash
#
read -p "enter an execute command: " n
load=$(whereis -b $n | cut -d ' ' -f 2)

command(){
        dir=$(dirname $load)
        mkdir -p /root/testdir$dir
        cp -r $load /root/testdir$dir
}
library(){
        libload=$(ldd $load | cut -d '>' -f 2 | cut -d '(' -f 1)
        dir1=$(dirname $libload)
        dir2=$(echo $dir1 | cut -d ' ' -f 1)
        mkdir -p /root/testdir$dir2
        cp -r $libload /root/testdir$dir2

}
while true;do
    command
    library
    read -p "enter an execute command: " n
    if [ "$n" == "quit" ];then
        echo "command finish."
        exit
    fi
done

原创文章,作者:pao,如若转载,请注明出处:http://www.178linux.com/38966

(0)
paopao
上一篇 2016-08-24 10:25
下一篇 2016-08-24 10:25

相关推荐

  • Linux基础知识(1)

    在开源的世界当中,有许多的开源协议,其中著名的有Apache、BSD和GPL协议,它们支撑了开源世界,使得让那些开源程序变得丰富多彩,我们知道开源协定的发起人是Stallman,它主张的自由含义为自由学习和更改,自由使用;自由分发和自由创建衍生版。
    而Linux就是在这样的开源协定下在1991年的10月份起义成功,同时,它也有它的哲学思想:

    2017-09-10
  • 网络接口配置–Bonding

    网络接口配置–Bonding Bonding        就是讲到快网卡绑定到同一IP地址对外服务,可以实现高可用或者负载均衡。当然,直接给两块网卡设置同一IP地址是不可能的。通过bonding,虚拟一块网卡对外提供连接,物理网卡被修改为同一MAC地址。 一 Bonding …

    Linux干货 2017-05-07
  • lvs三种基础模型

    1: LVS-DR 模式(调度器与实际服务器都有一块网卡连在同一物理网段上)简要的网络结构如下所示 lvs-DR模型是lvs的默认模型,也是企业中用到的最多的模型    解读:直接路由模型,每个Real Server上都有两个IP:VIP和RIP,但是VIP是隐藏的,就是不能提高解析等功能,只是用来做请求回复的源IP的,Director上…

    Linux干货 2016-08-15
  • Python

    Python中os和shutil模块
    csv文件和ini文件简介

    Linux干货 2017-10-30
  • Linux程序包管理方式

    Linux程序包安装和管理方式共计三种:          一、[yum|dnf],通过官网或者其他开源网站提供的文件服务器,本机镜像源等途径进行安装。         二、rpm,通过官网或者其他开源网站通过…

    Linux干货 2016-07-29
  • 脚本练习

    注:以下脚本练习实验都是以root用户身份执行的,若普通用户运行需要另加相应的权限 1、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小。 [root@fengl bin]# vim systeminfo.sh [roo…

    Linux干货 2016-08-15