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.www58058.com/38966