一、打印一個等腰三角形linux
#!/bin/bash
for i in `seq 10` ;do for j in `seq $[10-$i]` ;do echo -n ' ' done for k in `seq $[i*2-1]` ;do echo -n '*' done echo done
二、打印99乘法表bash
#!/bin/bash for i in {1..9} ;do for (( j=1; j<=$i; j++ )) ;do accumulate=$[i*j] echo -ne "${j}*${i}=$accumulate\t" done echo done
三、輸入網段,探測網段內存活的主機函數
#!/bin/bash trap 'exit' 2 read -p "please input a network address: " DNET echo $DNET |grep -qE "^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\.(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-4])\.){2}0$" || { echo "ipaddr error"; exit; } for I in {1..254} ;do { if ping -W1 -c1 ${DNET%.*}.$I &>/dev/null ;then echo "${DNET%.*}.$I is up." fi }& done unset DNET unset I wait
四、打印國際象棋棋盤學習
#!/bin/bash read -p "size: " bsize line=$[bsize*8-1] for i in `seq 0 $line` ;do for j in `seq 0 $line` ;do if [ $[(i/bsize+j/bsize)%2] -eq 0 ] ;then echo -ne "\033[47m \033[0m" else echo -n " " fi done echo done
五、編寫函數(my_functions),實現打印綠色OK和紅色FAILEDui
#Print OK and FAILED####################################### green_OK() { echo -e "[ \033[1;32mOK\033[0m ]"; } red_FAILED() { echo -e "[ \033[1;31mFAILED\033[0m ]"; } Screen=`stty -F /dev/tty size` Columus=${Screen#* } Spa_Col=$[Columus-26] Progress() { local i for i in {1..3} ;do echo -n "." sleep 0.5 done } success() { local i string="$1" Rt_Spa=$[$Spa_Col-${#string}] echo -n "$string" Progress for i in `seq $Rt_Spa` ;do echo -n " " done green_OK } failed() { local i string="$1" Rt_Spa=$[$Spa_Col-${#string}] echo -n "$string" Progress for i in `seq $Rt_Spa` ;do echo -n " " done red_FAILED }
六、編寫腳本copycmd.shspa
- (1) 提示用戶輸入一個可執行命令名稱;
- (2) 獲取此命令所依賴到的全部庫文件列表;
- (3) 複製命令至某目標目錄(例如/mnt/sysroot)下的對應路徑下;
- 如:/bin/bash ==> /mnt/sysroot/bin/bash
- (4) 複製此命令依賴到的全部庫文件至目標目錄下的對應路徑下;
- 如:/lib64/ld-linux-x86-64.so.2 ==> /mnt/sysroot/lib64/ld-linux-x86-64.so.2
- (5)每次複製完成一個命令後,不要退出,而是提示用戶鍵入新的要複製的命令,並重復完成上述功能;直到用戶輸入quit退出;
#!/bin/bash . my_functions Ddir="/mnt/sysroot" [ ! -d "$Ddir" ] && mkdir "$Ddir" cpbin() { cmddir=`echo "$Cmd_path" |grep -o ".*/\b"` [ ! -d "${Ddir}${cmddir}" ] && mkdir -p "${Ddir}${cmddir}" cp -n "$Cmd_path" "${Ddir}${cmddir}" } libcp() { echo "$Lib_path" |while read line ;do libdir=`echo "$line" |grep -o ".*/\b"` [ ! -d "${Ddir}${libdir}" ] && mkdir -p "${Ddir}${libdir}" cp -n "$line" "${Ddir}${libdir}" 2>/dev/null done } while read -p "Input a cmd. (quit): " CMD ;do if [ "$CMD" == 'quit' ] ;then echo "Think you! Bye bye."; break; fi if ! which "$CMD" &>/dev/null ;then echo "not find $CMD, please input again." failed "copy $CMD failed." continue fi Cmd_path=`which $CMD |grep -o "/.*"` Lib_path=`ldd $Cmd_path |sed -nr 's#.*[[:space:]]+(/.*) .*#\1#p'` cpbin libcp success "copy $CMD Complete." done
七、求斐波那契數列的第N個數rest
#!/bin/bash feibo() { if [ $1 -eq 0 ] ; then echo 0 elif [ $1 -eq 1 ] ;then echo 1 else echo $[`feibo $[$1-1]`+`feibo $[$1-2]`] fi } read -p "please a num: " n
八、求N的階乘code
#!/bin/bash fact() { if [ $1 -eq 0 -o $1 -eq 1 ] ;then echo 1 else echo $[$1*`fact $[$1-1]`] fi } fact $1
九、漢諾塔:問題是源於印度一個古老傳說。大梵天創造世界的時候作了三根金剛石柱子,在一根柱子上從下往上按照大小順序摞着64片黃金圓盤。大梵天命令婆羅門把圓盤從下面開始按大小順序從新擺放在另外一根柱子上。而且規定,在小圓盤上不能放大圓盤,在三根柱子之間一次只能移動一個圓盤,利用函數,實現N片盤的漢諾塔的移動步驟。blog
hannuota() { local from=$2 local buffer=$3 local to=$4 local m=$1 if [ "$m" -eq 1 ] ;then echo "$from --> $to" else hannuota $[m-1] $from $to $buffer echo "$from --> $to" hannuota $[m-1] $buffer $from $to fi } read -p "please input num: " n hannuota $n a b c
十、編寫服務腳本testsrv.sh,完成以下要求ip
- (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: 2345 98 01 . /root/bin/35_my_functions lockfile="/var/lock/subsys/testsrv" s_start() { [ -f "$lockfile" ] && failed "testsrv is running." && exit touch "$lockfile" success "testsrv start " } s_stop() { [ ! -f "$lockfile" ] && failed "testsrv is not running." && exit rm -f "$lockfile" success "testsrv stop " } s_status() { [ -f "$lockfile" ] && echo "testsrv is running." || echo "testsrv is not running." } s_usage() { echo "Usage: `basename $0` {start|stop|restart|status}" exit 1 } [ $# -ne 1 ] && s_usage case $1 in start) s_start ;; stop) s_stop ;; restart) if [ -f "$lockfile" ] ;then s_stop s_start else failed "testsrv is not running , stop " s_start fi ;; status) s_status ;; *) s_usage esac
我的學習筆記,只供參考 ^_^