shell script.从文件中获取用户名和密码添加用户
#在“/”下创建username和password两个文件,分别存放用户名和密码.格式为:username中每行一个用户名;password中每行一个密码,与username中的user一一对应。
#!/bin/bash
#判断文件是否存在
if [ ! -f /username ]; then
echo "ERROR:file username
unexistd"
elif [ ! -f /password ]; then
echo "ERROR:file passwd
unexistd"
else
#判断行数是否匹配
NAMELINE=` wc -l /username |cut -d
" " -f 1`
PASSWDLINE=` wc -l /password |cut -d
" " -f 1`
if [ "$NAMELINE" =
"0" -a "$PASSWDLINE" = "0" ];then
echo "file username and password empty"
elif [ "$NAMELINE" =
"0" ]; then
echo "file username
empty"
elif [ "$PASSWDLINE"
= "0" ];then
echo "file password
empty"
elif [ "$NAMELINE"
!= "$PASSWDLINE" ]; then
echo "error,please
check 'username' or 'passwd'"
#行数相等,创建用户
elif [ "$NAMELINE" =
"$PASSWDLINE" ]; then
for num in $(seq 1
$NAMELINE)
do
USERNM=`sed -n ${num}p
/username`
PASSWD=`sed -n ${num}p
/password`
USERADD=`useradd
$USERNM &> /dev/null && echo "1" || echo
"0"`
#判断用户是否存在
if [
"$USERADD" = "1" ]; then
echo $PASSWD|passwd
--stdin $USERNM
echo "user
$USERNM add successfully"
elif [
"$USERADD" = "0" ]; then
echo "useradd
failed, user $USERNM existd"
fi
done
fi
fi