代码:
user.sh:
1 #!/bin/bash
2 #Author:ChickenLxg
3 #Date & Time:2010-12-23 03:55:02
4 #Description:
5
6 GIDD=$[`cat /etc/group | cut -d: -f3 | sort -n | grep -v "65534" | tail -1`+1]
7 UIDD=$[`cat /etc/passwd | cut -d: -f3 | sort -n | grep -v "65534" | tail -1`+1]
8 NOWDAY=$[ `date +"%s"`/86400 ]
9
10 usage(){
11 echo "Usage: `basename $0` [-u UID] [-g DESCRIPTION] [-d DIRECTORY] [-s SHELL] username"
12 }
13
14 while getopts ":u:g:d:s:" OPT ;do
15 case $OPT in
16 u)
17 NEWUIDD=$OPTARG
18 cut -d: -f3 /etc/passwd | grep "^$NEWUIDD" &> /dev/null && echo "The UID has exist." && exit 1
19 ;;
20 g)
21 NEWDESCRIPTION=$OPTARG
22 ;;
23 d)
24 NEWDIRECTORY=$OPTARG
25 ;;
26 s)
27 NEWSHELL=$OPTARG
28 ! grep "^$NEWSHELL" /etc/shells &> /dev/null && echo "The shell is wrong." && exit 1
29 ;;
30 /?)
31 usage
32 exit 1
33 ;;
34 esac
35 done
adduserview.sh:
1 #!/bin/bash
2 #Author:ChickenLxg
3 #Date & Time:2010-12-23 20:41:09
4 #Description:
5
6 TMFILE=`mktemp /root/homework/userinfo.XXXXX`
7
8 dialog --stdout --title "Add a user" --form "Please input the information of new user:" 12 45 5 / "Username:" 1 1 "" 1 15 15 0 / "UID:" 2 1 "200" 2 15 15 0 / "Description:" 3 1 "" 3 15 15 0 / "Directory:" 4 1 "/home/" 4 15 15 0 / "Shell:" 5 1 "/bin/bash" 5 15 15 0 > $TMFILE
9
10 USERNAME=`head -1 $TMFILE | tail -1`
11 UIDD=`head -2 $TMFILE | tail -1`
12 DESCRIPTION=`head -3 $TMFILE | tail -1`
13 DIRECTORY=`head -4 $TMFILE | tail -1`
14 SHELL=`head -5 $TMFILE | tail -1`
15
16 /root/homework/user.sh -u ${UIDD} -g ${DESCRIPTION} -d ${DIRECTORY} -s ${SHELL} ${USERNAME}
17
18 PASSWORD=`dialog --stdout --insecure --title "Password" --passwordbox "Please give a password for the ${USERNAME}:" 12 45`
19
20 echo ${PASSWORD} | passwd --stdin ${USERNAME} &> /dev/null
21
22 rm -f $TMFILE
截图:
总结:
不用交互式给一个用户添加密码
echo "123435" | passwd --stdin username &> /dev/null
图形界面挺好弄,命令语句也没有什么复杂的。关键是怎么样接受图形界面传过来的数据,必须现在dialog 后面加一个"--stdout"让结果输出到标准输出上。
接下来就是创建用户的问题了,用前面做的user.sh稍加修改进行套用,必须在user.sh中加几个选项。
最后就是必须修改user.sh中的修改用户家目录的属主属组的问题,因为当在图形化界面下接受的家目录项如果不是默认的话,原来的user.sh不会正确的更改家目录的属主属组,必须由/home/$1改为${NEWDIRECTORY:="/home/$1"},这样才能正确运行。
首次接触图形化界面,很新奇。