shell脚本——循环和函数
1、打印一个等腰三角形
#!/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
2、打印99乘法表
#!/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
3、输入网段,探测网段内存活的主机
#!/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
4、打印国际象棋棋盘
#!/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
5、编写函数(my_functions),实现打印绿色OK和红色FAILED
#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 }
6、编写脚本copycmd.sh
- (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
7、求斐波那契数列的第N个数
#!/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
8、求N的阶乘
#!/bin/bash fact() { if [ $1 -eq 0 -o $1 -eq 1 ] ;then echo 1 else echo $[$1*`fact $[$1-1]`] fi } fact $1
9、汉诺塔:问题是源于印度一个古老传说。大梵天创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着64片黄金圆盘。大梵天命令婆罗门把圆盘从下面开始按大小顺序重新摆放在另一根柱子上。并且规定,在小圆盘上不能放大圆盘,在三根柱子之间一次只能移动一个圆盘,利用函数,实现N片盘的汉诺塔的移动步骤。
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
10、编写服务脚本testsrv.sh,完成如下要求
- (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
个人学习笔记,只供参考 ^_^