两个脚本

#!/bin/bash
##############################################################
# File Name: oldboyedu_fun.sh
# Version: V1.0
# Author: yao zhang
# Organization: www.zyops.com
# Created Time : 2016-09-12 16:58:28
# Description:
##############################################################

# 脚本初始化
function scripts_init(){
  prog=`basename $0 .sh`
  LockFile=/var/lock/subsys/${prog}.lock  # 使用锁文件
  LogFile=/var/log/${prog}.log  # 脚本记录日志
  PidFile=/var/run/${prog}.pid  # 记录进程号,可以管理脚本

  [ -f $LockFile ] && echo "There $LockFile is exist!!" && exit 1 ||touch $LockFile
  [ ! -f $LogFile ] && touch $LogFile
  [ -f $PidFile ] && echo "There $PidFile is exist!!" && exit 2|| echo $$ > $PidFile
}

# 记录日志
function writelog(){
  Date=$(date "+%F_%T")
  ShellName=`basename $0`
  Info=$1
  echo "$Date : ${ShellName} : ${Info}" >> ${LogFile}
}

# 脚本退出扫尾
function closeout(){
  [ -f $LockFile ] && rm -f $LockFile 
  [ -f $PidFile ]&& rm -f $PidFile
}

# 判断输入是整数
function int_judge(){
  fun_a=$1
  expr $fun_a + 1 &>/dev/null
  RETVAL=$?
  return $RETVAL
}

# 判断输入非空
function input_judge(){
  RETVAL=0
  fun_a=$1
  [ ${#fun_a} -eq 0 ]&& RETVAL=1
  return $RETVAL
}
#!/bin/bash
# author:zhangyao

# Defined variables and Judge this file
DbFile=./while_ernie.db
[ ! -f $DbFile ] && touch $DbFile

# Defined color variables
RED_COLOR='E[1;31m'
GREEN_COLOR='E[1;32m'
YELOW_COLOR='E[1;33m'
BLUE_COLOR='E[1;34m'
PINK='E[1;35m'
RES='E[0m'

# Defined trapper function
function trapper(){
    trap ':' INT EXIT TSTP TERM HUP
}

# Defined Random function
function random(){
    local x y
    x=$(($RANDOM%9))
    y=$(($RANDOM%9))
    echo $x$y
} 

# Defined warn function
function warn(){
    echo -e "${RED_COLOR}Please enter the correct format name! Like zhangsan!${RES}"
    countdown
    continue
}

function warn2(){
    echo -e "${RED_COLOR}You have entered the name! Give the chance to others!${RES}"
    countdown
    continue
}

# Defined Countdown function
function countdown(){
        for i in `seq -w 10 -1 1`
          do
           echo -ne "$i";
           sleep 1;
        done
}

# Defined Check Name function
function check_name(){
    echo -e "${YELOW_COLOR}Please enter [exit] to exit the program${RES}" 
    read  -p "Please enter the name of the Pinyin:" name name1
    [ "$name" == "exit" ]&& exit 0
    [ -n "$name1" ] && warn
    [ -n "$name" -a -z "`echo "${name//[a-zA-Z]/}"`" ] || warn
}

# Defined Check File function
function check_file(){
    local Name Id

    # Judge only one name
    Name=`awk '{if($1=="'$name'") print $1}' $DbFile`
    [ -z "$Name" ] || warn2

    # make only one num
    num=`random`
    Id=$num
    while [ -n "$Id" ] 
      do
        num=`random`
        Id=`awk '{if($2=="'$num'") print $2}' $DbFile`
    done

    # write to file
    printf "%-20s %-2s
" $name $num >> $DbFile
}

# Defined Welcome function
function welcome(){
    clear
    echo -e "${GREEN_COLOR}
         This program will select 3 students to participate in
    the training of enterprise project practice out of the oldboy.

    The students are eligible to participate in the list as follows${RES}"
    # Print the name of the first three
    echo -e "${BLUE_COLOR}`sort -rn -k2 $DbFile|head -3`${RES}"
}

# Defined main Functions
function main(){
    while true
      do
        trapper
        welcome
        check_name
        check_file
    done
}

main
原文地址:https://www.cnblogs.com/gaoyuechen/p/8029619.html