这个shell script是用来操作我的邮服帐号的,现在提供了查看,删除,修改,更新等基本功能。
#!/bin/sh
#menu
#Main Menu Script
#set -x
#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.$$
#select1_result file
SELECT1_RESULT=select1_result.$$
#seleclt_op file
SELECT1_OP=select1_op.$$
#Colour function
colour()
{
#format is background;foregroundm
case $1 in
black_green)
echo "[40;32m"
;;
black_yellow)
echo "[40;33m"
;;
black_white)
echo "[40;37m"
;;
black_cyan)
echo "[40;36m"
;;
red_yellow)
echo "[41;33m"
;;
esac
}
#Just read a single key please
#get_char()
#{
#get_char
#Save current stty settings
#SAVEDSTTY=`stty -g`
#stty cbreak
#dd if=/dev/tty bs=1 count=1 2> /dev/null
#stty -cbreak
#stty $SAVEDSTTY
#}
#Turn the cursor on or off
cursor()
{
#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
restrict()
{
colour red_yellow
echo -e -n " 07Sorry you are not authorised to use this function"
colour black_green
}
user_level()
{
#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 "Soryy $User you have not been authorised to use this menu"
exit 1
fi
}
#called when user selects quit
my_exit()
{
#my_exit
#called when user selects quit!
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
my_select1()
{
clear
colour black_green
cat <
----------------------------------------------------------------------------
EOF
colour black_cyan
echo "use postfix;select username,name from mailbox;">$SELECT1_OP
$MYSQL -uroot<$SELECT1_OP >>$SELECT1_RESULT
rm $SELECT1_OP
#SERIAL=0
#MYPAUSE=0
#while read MAILNAME NAME
#do
# SERIAL=`expr $SERIAL + 1`
# MYPAUSE=`expr $MYPAUSE + 1`
# if [ $MYPAUSE -eq 5 ];then
# echo "Press any key continue..."
# MYPAUSE=0
# read DUMMY
# fi
# echo -e -n "$MAILNAME $NAME "
#done<$SELECT1_RESULT
more -5 -d $SELECT1_RESULT
colour black_cyan
echo -e -n " total:$SERIAL"
colour black_green
cat <
-----------------------------------------------------------------------------
EOF
colour black_cyan
rm $SELECT1_RESULT
}
my_select1_result()
{
while :
do
colour black_green
cat <
$ACCESS
-----------------------------------------------------------------------------
User:$USER Host:$THIS_HOST Date:$MYDATE
-----------------------------------------------------------------------------
1.Detail mailbox
2.Display mailbox again
3.Return
H.Help
Q.Exit
-----------------------------------------------------------------------------
EOF
colour black_cyan
echo -e -n " Your Choice [1,2,3,H,Q >"
read MY_SELECT
case $MY_SELECT in
1)
echo -e -n "Please input you are going to detail mailbox:"
read MAILNAME
echo "use postfix; select username as ‘mailbox‘,password as ‘password‘,name as ‘true name‘, maildir as ‘maildir‘,quota as ‘quota‘,domain as ‘domain‘,created as ‘created time‘,modified as ‘modified time‘,active as ‘active‘ from mailbox where username LIKE ‘$MAILNAME%‘ G;">$SELECT1_OP
clear
$MYSQL -uroot<$SELECT1_OP
rm $SELECT1_OP
;;
2)
my_select1
;;
3)
break
;;
q|Q)
my_exit
;;
*)
;;
esac
done
}
my_delete1()
{
while :
do
colour black_green
cat <
$ACCESS
-----------------------------------------------------------------------------
User:$USER Host:$THIS_HOST Date:$MYDATE
-----------------------------------------------------------------------------
1.Delete mailbox
2.Display mailbox again
3.Return
H.Help
Q.Exit
-----------------------------------------------------------------------------
EOF
colour black_cyan
echo -e -n " Your Choice [1,2,3,H,Q >"
read MY_SELECT
case $MY_SELECT in
1)
echo -e -n "Please input you are going to delete mailbox:"
read MAILNAME
echo "use postfix;select * from mailbox where username=‘$MAILNAME‘;">$SELECT1_OP
MYSELECT=`$MYSQL -uroot<$SELECT1_OP`
if [ -z $MYSELECT ]; then
echo "input failed:"
break
else
{
echo "use postfix;delete from mailbox where username=‘$MAILNAME‘;">$SELECT1_OP
echo "delete $MAILNAME[Y/N]"
read MYDELETE
if [ "$MYDELETE" == "Y" -o $MYDELETE == "y" ];then
$MYSQL -uroot<$SELECT1_OP
echo "delete successful"
break
else
echo "delete failed"
fi
}
fi
;;
2)
my_select1
;;
3)
break
;;
H|h)
echo "this is a help file"
;;
Q|q)
my_exit
;;
esac
done
}
my_update1()
{
while :
do
colour black_green
cat <
$ACCESS
------------------------------------------------------------------------------
User:$USER Host:$THIS_HOST Date:$MYDATE
------------------------------------------------------------------------------
1.Update mailbox
2.Display mailbox again
R.Return
H.Help
Q.Exit
------------------------------------------------------------------------------
EOF
colour black_cyan
echo -e -n " Your Choice[1,2,R,H,Q]:"
read MY_SELECT
case $MY_SELECT in
1)
while :
do
echo -e -n "Please input you are going to modify mailbox:"
read MAILNAME
[ -z "$MAILNAME" ] || break
done
echo "use postfix;select username from mailbox where username=‘$MAILNAME‘">$SELECT1_OP
MYSELECT=`$MYSQL -uroot<$SELECT1_OP`
if [ -z "$MYSELECT" ];then
echo "update failed"
else
while :
do
echo -e -n "Please input password:"
read MYPASSWD
echo -e -n "Please input password again:"
read REMYPASSWD
if [ $MYPASSWD = $REMYPASSWD ];then
break
else
echo "password no match"
fi
done
echo -e -n "Please input user:"
read MYNAME
echo -e -n "active[y/n]"
read MYACTIVE
case $MYACTIVE in
Y|y)
MYACTIVE="1"
;;
N|n)
MYACTIVE="0"
;;
esac
MYMAILDIR="$MAILNAME/"
echo "modify[y/n]"
read MYSELECT
case $MYSELECT in
y|Y)
MYDATE=`date "+%Y-%m-%d %H:%M:%S"`
echo "use postfix;update mailbox set password=‘$MYPASSWD‘,name=‘$MYNAME‘,quota=0,modified=‘$MYDATE‘,active=‘$MYACTIVE‘ where username=‘$MAILNAME‘">$SELECT1_OP
$MYSQL -uroot <$SELECT1_OP
echo "update successful"
;;
n|N)
echo "update failed"
;;
esac
fi
;;
2)
my_select1
;;
R|r)
break
;;
Q|q)
my_exit
;;
*)
;;
esac
done
}
my_new1()
{
while :
do
echo -e -n "Please input new mailbox:"
read MAILNAME
[ -z "$MAILNAME" ] || break
done
echo "use postfix;select username from mailbox where username=‘$MAILNAME‘;">$SELECT1_OP
MYSELECT=`$MYSQL -uroot<$SELECT1_OP`
if [ -n "$MYSELECT" ];then
echo "create mailbox failed"
else
while :
do
echo -e -n "Please input password:"
read MYPASSWD
echo -e -n "Please input password again:"
read REMYPASSWD
if [ $MYPASSWD = $REMYPASSWD ];then
break
else
echo "password no match"
fi
done
echo -e -n "Please input user:"
read MYNAME
echo -e -n "active[y/n]"
read MYACTIVE
case $MYACTIVE in
Y|y)
MYACTIVE="1"
;;
N|n)
MYACTIVE="0"
;;
esac
MYMAILDIR="$MAILNAME/"
echo "modify[y/n]"
read MYSELECT
case $MYSELECT in
y|Y)
MYDATE=`date "+%Y-%m-%d %H:%M:%S"`
MYDOMAIN=`echo $MAILNAME|awk -F@ ‘{print $2}‘`
echo "use postfix;insert into mailbox (username,password,name,maildir,quota,domain,created,active) valuse(‘$MAILNAME‘,‘$MYPASSWD‘,‘$MYNAME‘,‘$MYMAILDIR‘,0,‘$MYDOMAIN‘,‘$MYDATE‘,‘$MYACTIVE‘);">$SELECT1_OP
$MYSQL -uroot <$SELECT1_OP
echo "create successful"
;;
n|N)
echo "create failse"
;;
esac
fi
}
tput init
colour black_green
while :
do
tput clear
colour black_green
cat <
$ACCESS
-----------------------------------------------------------------------------
User:$USER Host:$THIS_HOST Date:$MYDATE
-----------------------------------------------------------------------------
1.Display mailbox
2.Delete mailbox
3.Modify mailbox
4.New mailbox
H.Help
Q.Exit
-----------------------------------------------------------------------------
EOF
colour black_cyan
echo -e -n " Your Choice [1,2,3,H,Q >"
MYSQL="/opt/mysql5/bin/mysql"
[ -x $MYSQL ] || exit 0
read CHOICE
case $CHOICE in
1)
my_select1
my_select1_result
;;
2)
my_select1
my_delete1
;;
3)
my_select1
my_update1
;;
4)
my_new1
;;
H|h)
tput clear
cat <
This is a help file
EOF
;;
Q|q)
my_exit
;;
*)
echo -e " 07unknown user response"
;;
esac
echo -e -n " 07Hit the return key to continue"
read DUMMY
done