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
- 下一篇: Java 异常(Java Excepti