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
下一篇 2016-08-24

相关推荐

  • linux操作系统rpm软件包管理

    软件包管理 软件包运行环境: API:Application Programming Interface     使用标准:POSIX:     源代码运行步骤:预处理(如处理注释)—>编译成汇编代码—>链接其他库文件 ABI:applicatio…

    2016-08-22
  • 第五周作业

    1. 显示当前系统上root、fedora或user1用户的默认shell; grep -E "^(root|centos|user1)\>" /etc/passwd 或 awk -F: '{if($1=="root" || $1==…

    Linux干货 2017-01-10
  • 第四周 N28 (用户及权限管理及BRE、ERE应用)

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 # cp -r /etc/skel /home/tuser1 # chmod -R og= /home/tuser1 2、编辑/etc/group文件,添加组hadoop。 hadoop:x:1000: 3、手动编辑/etc…

    2017-12-22
  • 10分钟学会理解和解决MySQL乱码问题

    本文将详细介绍MySQL乱码的成因和具体的解决方案。在阅读本文之前,强烈建议对字符集编码概念还比较模糊的同学 阅读下博主之前对相关概念的一篇科普:十分钟搞清字符集和字符编码 MySQL出现乱码的原因 要了解为什么会出现乱码,我们就先要理解:从客户端发起请求,到MySQL存储数据,再到下次从表取回客户端的过程中,哪些环节会有编码/解码的行为。为了更好的解释这个…

    2015-03-17
  • corosync v2+pacemaker实现mariadb的高可用

    高可用mariadb拓扑图 一、设计前提 1、时间同步 # ntpdate 172.16.0.1 或者 # chronyc sources 2、所有的主机对应的IP地址解析可以正常工作, 主机名要与命令#uname -n 所得的结果一致 因此,/etc/hosts中的内容为以下内容         172.16.23.10 node1.rj.com node…

    2017-11-02
  • 如何正确安装一个源码包

        下周就要考试了,心情挺忐忑不安的,前几天做了25期的考试题,感觉每个题都是老师上课讲过的,但是自己做却想不起来了。这应该就像学习数学一样,需要大量练习,做的多了自然就会了。     这周我们学习了如何使用yum,还学习了磁盘管理。针对如何安装源码包,以httpd包为例我们…

    2017-08-19