linux shell 编写菜单脚本事例
生活随笔
收集整理的这篇文章主要介绍了
linux shell 编写菜单脚本事例
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
menu2文件代码: --- #!/bin/sh
#menu2
#Main menu script
#ignore ctrl-c and QUIT interrupts
trap "" 2 3 15
MYDATE=`date +%d/%m/%Y`
THIS_HOST=`hostname -s` USER=`whoami` #user level file
USER_LEVELS=priv.user #hold file
HOLD1=hold1.$$ #colour function function colour ()
{
case $1 in
black_green)
echo '[40;32m'
;;
black_yellow)
echo '[40;33m'
;;
black_white)
echo '[40;37'
;;
black_cyan)
echo '[40;36m'
;;
red_yellow)
echo '[41;33m'
;;
esac
} #just read a single key please
function get_char()
{
#get_char
#save current stty setting
SAVEDSTTY=`stty -g`
stty cbreak
dd if=/dev/tty bs=1 count=1 2>/dev/null
stty -cbreak
#restore stty
stty $SAVEDSTTY
}
#turn the cursor on or off
function cursor ()
{
#turn cursor on/off
_OPT=$1
case $_OPT in
on ) echo '[?25h'
;;
off) echo '[?25l'
;;
*) return 1
;;
esac
} #check what privilege level the user has
function restrict ()
{
colour red_yellow
echo -e -n "\n\n\007 Sorry your are not authorised to use this function"
colour black_green } function user_level ()
{
#read in the priv.user file
while read LINE
do
case $LINE in
#ignore comments
\#*) ;;
*) echo $LINE>>$HOLD1
;;
esac
done < $USER_LEVELS
FOUND=false
while read MENU_USER PRIV
do
if [ "$MENU_USER" = "$USER" ] ; then
FOUND=true
case $PRIV in
yes|YES)
return 0
;;
no|NO)
return 1
;;
esac
else
#no match found read next record
continue
fi
done <$HOLD1
if [ "$FOUND" = "false" ] ; then
echo "Sorry $USER you have not been authorsied to use this menu"
exit 1
fi
} #called when user selects quit
function my_exit ()
{
colour black_white
cursor on
rm *.$$
exit 0
} tput init
#display their user levels on the screen
if user_level ; then
ACCESS="Access Mode is High"
else
ACCESS="Access Mode is Normal"
fi tput init
while :
do
tput clear
colour black_green
cat <<MAYDAY
$ACCESS
---------------------------------------------------------------------
User:$USER Host:$THIS_HOST Date:$MYDATE
---------------------------------------------------------------------
1:ADD A RECORD
2:VIEW A RECORD
3:PAGE ALL RECORD
4:CHANGE A RECORD
5:DELETE A RECORD
P:PRINT ALL RECORDS
H:Help screen
Q:Exit Menu
---------------------------------------------------------------------
MAYDAY
colour black_cyan
echo -e -n "\tYour Choice [1,2,3,4,5,P,H,Q] >"
CHOICE=`get_char`
case $CHOICE in
1) ls
;;
2) vi
;;
3) who
;;
4) if user_level; then
ls -l | wc
else
restrict
fi
;;
5) if user_level;then
sort /etc/passwd
else
restrict
fi
;;
P|p) echo -e "\n\nPrinting recirds ......"
;;
H|h)
tput clear
cat <<MAYDAY
This is the help screen ,nothing here yet to help you!
MAYDAY
;;
Q|q) my_exit
;;
*) echo -e "\t\007unknown user respense"
;;
esac
echo -e -n "\tHit the return key to continue"
read DUMMY
done --- priv.user 文件代码: #root quanxian wei YES, daia quanxianwei no root yes daia no
本文转自守住每一天51CTO博客,原文链接:http://blog.51cto.com/liuyu/258791,如需转载请自行联系原作者
#menu2
#Main menu script
#ignore ctrl-c and QUIT interrupts
trap "" 2 3 15
MYDATE=`date +%d/%m/%Y`
THIS_HOST=`hostname -s` USER=`whoami` #user level file
USER_LEVELS=priv.user #hold file
HOLD1=hold1.$$ #colour function function colour ()
{
case $1 in
black_green)
echo '[40;32m'
;;
black_yellow)
echo '[40;33m'
;;
black_white)
echo '[40;37'
;;
black_cyan)
echo '[40;36m'
;;
red_yellow)
echo '[41;33m'
;;
esac
} #just read a single key please
function get_char()
{
#get_char
#save current stty setting
SAVEDSTTY=`stty -g`
stty cbreak
dd if=/dev/tty bs=1 count=1 2>/dev/null
stty -cbreak
#restore stty
stty $SAVEDSTTY
}
#turn the cursor on or off
function cursor ()
{
#turn cursor on/off
_OPT=$1
case $_OPT in
on ) echo '[?25h'
;;
off) echo '[?25l'
;;
*) return 1
;;
esac
} #check what privilege level the user has
function restrict ()
{
colour red_yellow
echo -e -n "\n\n\007 Sorry your are not authorised to use this function"
colour black_green } function user_level ()
{
#read in the priv.user file
while read LINE
do
case $LINE in
#ignore comments
\#*) ;;
*) echo $LINE>>$HOLD1
;;
esac
done < $USER_LEVELS
FOUND=false
while read MENU_USER PRIV
do
if [ "$MENU_USER" = "$USER" ] ; then
FOUND=true
case $PRIV in
yes|YES)
return 0
;;
no|NO)
return 1
;;
esac
else
#no match found read next record
continue
fi
done <$HOLD1
if [ "$FOUND" = "false" ] ; then
echo "Sorry $USER you have not been authorsied to use this menu"
exit 1
fi
} #called when user selects quit
function my_exit ()
{
colour black_white
cursor on
rm *.$$
exit 0
} tput init
#display their user levels on the screen
if user_level ; then
ACCESS="Access Mode is High"
else
ACCESS="Access Mode is Normal"
fi tput init
while :
do
tput clear
colour black_green
cat <<MAYDAY
$ACCESS
---------------------------------------------------------------------
User:$USER Host:$THIS_HOST Date:$MYDATE
---------------------------------------------------------------------
1:ADD A RECORD
2:VIEW A RECORD
3:PAGE ALL RECORD
4:CHANGE A RECORD
5:DELETE A RECORD
P:PRINT ALL RECORDS
H:Help screen
Q:Exit Menu
---------------------------------------------------------------------
MAYDAY
colour black_cyan
echo -e -n "\tYour Choice [1,2,3,4,5,P,H,Q] >"
CHOICE=`get_char`
case $CHOICE in
1) ls
;;
2) vi
;;
3) who
;;
4) if user_level; then
ls -l | wc
else
restrict
fi
;;
5) if user_level;then
sort /etc/passwd
else
restrict
fi
;;
P|p) echo -e "\n\nPrinting recirds ......"
;;
H|h)
tput clear
cat <<MAYDAY
This is the help screen ,nothing here yet to help you!
MAYDAY
;;
Q|q) my_exit
;;
*) echo -e "\t\007unknown user respense"
;;
esac
echo -e -n "\tHit the return key to continue"
read DUMMY
done --- priv.user 文件代码: #root quanxian wei YES, daia quanxianwei no root yes daia no
本文转自守住每一天51CTO博客,原文链接:http://blog.51cto.com/liuyu/258791,如需转载请自行联系原作者
总结
以上是生活随笔为你收集整理的linux shell 编写菜单脚本事例的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: mysql集群mysql-cluster
- 下一篇: Linux 服务器如何修改主机名