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

相关推荐

  • Shell 位置参数总结

    描述: 位置参数指的是shell脚本的命令行参数,同时也表示shell函数的额函数参数。 说明: 示例: #!/bin/sh echo "number:$#"     # 传给脚本的参数个数 echo "scname:$0" &n…

    Linux干货 2016-08-16
  • 抓包获取QQ好友IP地址

    作者:网海过客 原文连接:https://www.chinasa.net/archives/326.html 原理:通过抓包软件,抓取QQ进程,向QQ好向发送UDP数据包,获取QQ好友IP地址 抓包软件:科来网络分析系统 步骤: 1、打开抓包软件,选择网卡,本地进程分析。 2、向QQ好友发起语音通话 3、在抓包软件里,找到QQ进程,数据包,过滤UDP协议,在…

    Linux干货 2017-06-30
  • Linux下用户属性的更改

      Linux下更改用户的属性一般使用usermod命令   基本格式如下:            Usermod[options]  login      [options]中具体参数如下:    -u  U…

    Linux干货 2017-05-30
  • 马哥教育网络班21期+第六周课程练习

    1、描述计算机的组成及其功能 答: (1)计算机组成: 输入(键盘)、输出(显示器),存储器(硬盘),运算器,控制器(CPU) (2)实现功能:    1)文件系统    2)内存管理    3)网络功能    4)进程管理    5)驱动程序 2、按系列罗列…

    Linux干货 2016-08-15
  • LVS工作原理浅析

    一、什么是LVS         LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统。本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一。         LVS集群采用IP负载均衡技术和基于内容…

    Linux干货 2016-11-29