看看我为cygwin编的login和su脚本(转)
http://bbs.chinaunix.net/viewthread.php?tid=133502
刚安装了cygwin,很有意思,想到可以用来做公司操作员的测试环境。但由于cygwin没有登录等操作,于是自己编写了su等脚本,只有最基本的功能。以下是程序脚本,时间紧,而且小弟shell刚刚入门,希望各位老大能够给俺除除虫,提提建议,小弟在此先谢了。
首先在更改默认的.bash_profile:
echo -e "\n选择要进入的系统:\n"
echo -e "(1) SYSTEM1\n"
echo -e "(2) SYSTEM2\n"
echo -e "(*) EXIT\n"
echo -e "Your choice:\c"
read choice
case "$choice" in
1) clear
exec /home/Admin/bin/mylogin /etc/passwd.1 ;;
2) clear
exec /home/Admin/bin/mylogin /etc/passwd.2
;;
*) exit
;;
esac
下面是passwd.?的格式(反正没有权限设定,所以密码不加密也无所谓啦):
username,password,HOME,shell,PATH,profile
mylogin程序脚本:
if [ $# -ne 1 ]
then
exit
fi
PASSFILE=$1
trap "" 2 3 15
i=1
while [ $i -le 3 ]
do
echo -n "login:"
read username
stty -echo
echo -n "password:"
read password
stty echo
echo -e "\n\c"
chkuser=`grep "$username" ${PASSFILE}|wc -l`
if [ $chkuser -ne 1 ]
then
echo "UX:login: ERROR: Login incorrect"
else
userpasswd=`grep "$username" ${PASSFILE}|awk -F, '{print $2}'`
if [ "$password" = "$userpasswd" ]
then
PS1="\$ "
HOME=`grep "$username" ${PASSFILE}|awk -F, '{print $3}'`;export HOME
PATH=`grep "$username" ${PASSFILE}|awk -F, '{print $5}'`;export PATH
cd $HOME
PROFILE=`grep "$username" ${PASSFILE}|awk -F, '{print $6}'`
if [ -f $HOME$PROFILE ]
then
. $HOME$PROFILE
fi
USERSHELL=`grep "$username" ${PASSFILE}|awk -F, '{print $4}'`;export USERSHELL
$USERSHELL -i
i=3
else
echo "UX:login: ERROR: Login incorrect"
fi
fi
i=`expr $i + 1`
done
这样就可以在passwd里添加用户了,登录后进入自己的目录,还有用自己的环境变量,但是su不能用,所以也自己编了个简单的:
PASSFILE=/etc/passwd.1
if [ "$1" != "-" ]
then username=$1
envchg=0
else username=$2
envchg=1
fi
chkuser=`grep -- "$username" ${PASSFILE}|wc -l`
if [ $chkuser -ne 1 ]
then
echo "UX:su: ERROR: Unknown user id: $username"
exit
else
stty -echo
echo -n "password:"
read password
stty echo
echo -e "\n\c"
userpasswd=`grep "$username" ${PASSFILE}|awk -F, '{print $2}'`
if [ "$password" = "$userpasswd" ]
then
if [ $envchg -eq 1 ]
then
HOME=`grep "$username" ${PASSFILE}|awk -F, '{print $3}'`;export HOME
PATH=`grep "$username" ${PASSFILE}|awk -F, '{print $5}'`;export PATH
cd $HOME
PROFILE=`grep "$username" ${PASSFILE}|awk -F, '{print $6}'`
if [ -f $HOME$PROFILE ]
then
. $HOME$PROFILE
fi
fi
刚安装了cygwin,很有意思,想到可以用来做公司操作员的测试环境。但由于cygwin没有登录等操作,于是自己编写了su等脚本,只有最基本的功能。以下是程序脚本,时间紧,而且小弟shell刚刚入门,希望各位老大能够给俺除除虫,提提建议,小弟在此先谢了。
首先在更改默认的.bash_profile:
echo -e "\n选择要进入的系统:\n"
echo -e "(1) SYSTEM1\n"
echo -e "(2) SYSTEM2\n"
echo -e "(*) EXIT\n"
echo -e "Your choice:\c"
read choice
case "$choice" in
1) clear
exec /home/Admin/bin/mylogin /etc/passwd.1 ;;
2) clear
exec /home/Admin/bin/mylogin /etc/passwd.2
;;
*) exit
;;
esac
下面是passwd.?的格式(反正没有权限设定,所以密码不加密也无所谓啦):
username,password,HOME,shell,PATH,profile
mylogin程序脚本:
if [ $# -ne 1 ]
then
exit
fi
PASSFILE=$1
trap "" 2 3 15
i=1
while [ $i -le 3 ]
do
echo -n "login:"
read username
stty -echo
echo -n "password:"
read password
stty echo
echo -e "\n\c"
chkuser=`grep "$username" ${PASSFILE}|wc -l`
if [ $chkuser -ne 1 ]
then
echo "UX:login: ERROR: Login incorrect"
else
userpasswd=`grep "$username" ${PASSFILE}|awk -F, '{print $2}'`
if [ "$password" = "$userpasswd" ]
then
PS1="\$ "
HOME=`grep "$username" ${PASSFILE}|awk -F, '{print $3}'`;export HOME
PATH=`grep "$username" ${PASSFILE}|awk -F, '{print $5}'`;export PATH
cd $HOME
PROFILE=`grep "$username" ${PASSFILE}|awk -F, '{print $6}'`
if [ -f $HOME$PROFILE ]
then
. $HOME$PROFILE
fi
USERSHELL=`grep "$username" ${PASSFILE}|awk -F, '{print $4}'`;export USERSHELL
$USERSHELL -i
i=3
else
echo "UX:login: ERROR: Login incorrect"
fi
fi
i=`expr $i + 1`
done
这样就可以在passwd里添加用户了,登录后进入自己的目录,还有用自己的环境变量,但是su不能用,所以也自己编了个简单的:
PASSFILE=/etc/passwd.1
if [ "$1" != "-" ]
then username=$1
envchg=0
else username=$2
envchg=1
fi
chkuser=`grep -- "$username" ${PASSFILE}|wc -l`
if [ $chkuser -ne 1 ]
then
echo "UX:su: ERROR: Unknown user id: $username"
exit
else
stty -echo
echo -n "password:"
read password
stty echo
echo -e "\n\c"
userpasswd=`grep "$username" ${PASSFILE}|awk -F, '{print $2}'`
if [ "$password" = "$userpasswd" ]
then
if [ $envchg -eq 1 ]
then
HOME=`grep "$username" ${PASSFILE}|awk -F, '{print $3}'`;export HOME
PATH=`grep "$username" ${PASSFILE}|awk -F, '{print $5}'`;export PATH
cd $HOME
PROFILE=`grep "$username" ${PASSFILE}|awk -F, '{print $6}'`
if [ -f $HOME$PROFILE ]
then
. $HOME$PROFILE
fi
fi