Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19912495
  • 博文数量: 679
  • 博客积分: 10495
  • 博客等级: 上将
  • 技术积分: 9308
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-18 10:51
文章分类

全部博文(679)

文章存档

2012年(5)

2011年(38)

2010年(86)

2009年(145)

2008年(170)

2007年(165)

2006年(89)

分类:

2006-12-05 10:16:05

第22章创建屏幕输入

本章内容有:
• 验证有效输入。
• 增加、删除、修改和查看记录。
• 修改脚本的工作文件。

DBFILE=DBFILE
HOLD1=HOLD1.$$

read_a_char()
{
SAVEDSTTY=`stty -g`
stty cbreak
dd if=/dev/tty bs=1 count=1 2> /dev/null
stty -cbreak
stty $SAVEDSTTY
}

continue_promptYN()
_STR=$1
_DEFAULT=$2
if [ $# -lt 1 ]; then
echo "continue_prompt: I need a string to display"
return 1
fi
while :
do
echo -n "$_STR [Y..N] [$_DEFAULT]:"
read _ANS
: ${_ANS:=$_DEFAULT}
if [ "$_ANS" = "" ]; then
case $_ANS in
Y) return 0;;
N) return 1;;
esac
fi
case $_ANS in
y|Y|Yes|YES)
return 0
;;
n|N|No|NO)
return 1
;;
*)echo "answer either Y or N,default is $_DEFAULT"
;;
esac
echo $_ANS
done
}

continue_prompt()
{
echo -n "Hit any key to continue.."
DUMMY=`read_a_char`
}

length_check()
{
_STR=$1
_MAX=$2
_LENGTH=`echo $_STR|awk '{print length($0)}'`
if [ "$_LENGTH" -gt "$_MAX" ]; then
return 1
else
return 0
fi
}

a_number()
{
_NUM=$1
_NUM=`echo $1|awk '{if($0~/[^0-9]/)print "1"}'`
if [ "$_NUM" != "" ]
then
return 1
else
return 0
fi
}

characters()
{
_LETTERS_ONLY=$1
_LETTERS_ONLY=`echo $1|awk '{if($0~/[^a-zA-Z]/) print "1"}'`
if [ "$_LETTERS_ONLY" != "" ]
then
return 1
else
return 0
fi
}

check_duplicate()
{
_CODE=$1
MATCH=`grep "$_CODE>" $DBFILE`
echo $_CODE
if [ "$MATCH" = "" ];then
return 0
else
return 1
fi
}

add_rec()
{
while :
do
echo -n "employee staff number: "
read NUM
if [ "$NUM" != "" ]; then
if a_number $NUM; then
NUM_PASS=0
else
NUM_PASS=1
fi
if length_check $NUM 10;then
LEN_PASS=0
else
LEN_PASS=1
fi
if check_duplicate $NUM;then
DUPLICATE=0
else
DUPLICATE=1
echo "staff number :there is already a employee with this number"
continue_prompt
fi
if [ "$LEN_PASS" = "0" -a "$NUM_PASS" = "0" -a "$DUPLICATE" = "0" ]
then
break
else
echo "staff number: non-numeric or too many numbers in field"
continue_prompt
fi
else
echo "staff number: no input detected ,this field requires a number"
continue_prompt
fi
done

#First name
while :
do
echo -n "employee's first name: "
read F_NAME
if [ "$F_NAME" != "" ];then
if characters $F_NAME;then
F_NAME_PASS=0
else
F_NAME_PASS=1
fi
if length_check $F_NAME 20;then
LEN_PASS=0
else
LEN_PASS=1
fi
if [ "$LEN_PASS" = "0" -a  "$F_NAME_PASS" = "0" ];then
break
else
echo "staff first name:non-character or too many characters in field"
continue_prompt
fi
else
echo "staff first name :no input detected,this field requires characters"
continue_prompt
fi
done

#SURNAME
while :
do
echo -n "employee;s surname :"
read S_NAME
if [ "$S_NAME" != "" ];then
if characters $S_NAME;then
S_NAME_PASS=0
else
S_NAME_PASS=1
fi
if length_check $S_NAME 20;then
LEN_PASS=0
else
LEN_PASS=1
fi
if [ "$LEN_PASS" = "0" -a "$S_NAME_PASS" = "0" ];then
break
else
echo "staff surname: non-character or too many characters in field"
continue_prompt
fi
else
echo "staff surname : no input detected, this field requires characters"
continue_prompt
fi
done

#department
while:
do
echo -n "company department: "
read DEPART
case $DEPART in
ACCOUNTS|Accounts|accounts)break;;
SALES|Sales|sales)break;;
IT|It|it)break;;
CLAIMS|Claims|claims)break;;
Services|SERVICES|services)break;;
*)echo "department: Accounts,sales,it,claims,services";;
esac
done
}

#main
clear
echo -e "tttADD A EMPLOYEE RECORT"
if [ -s $DBFILE ];then :
else
echo "Information : creating new file to add employee records"
>$DBFILE
fi
add_rec
if continue_promptYN "Do you wish to save this recore " "Y";then
echo "$NUM:$F_NAME:$S_NAME:$DEPART" >>DBFILE
echo "record save"
sleep 1
sort +2 -t: $DBFILE >$HOLD1 2> /dev/null
if [ $? -ne 0 ];then
echo "problems trying to sort the file..check it out"
exit 1
fi
mv $HOLD1 $DBFILE
if [ $? -ne 0 ];then
echo "problems moving the temp sort file check it out "
exit 1
fi
else
echo "record not saved "
sleep 1
fi


阅读(2217) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~