昨天的 shell script sample:
#!/bin/bash
#
# to create new linux user account
#
p_group=it
p_gid=
users="user1 user2 user3" # or get list from other sources
m_day=30
e_date=2015-12-31
getent group | grep -q "^${p_group}:" || {
if [ "$p_gid" ]; then
groupadd -g $p_gid $p_group
else
groupadd $p_group
fi
[ $? -ne 0 ] && {
echo $0: Error: Fail to create group $p_group.
exit 1
}
}
for u in $users; do
if id -u $u &>/dev/null ; then
echo $0: Warning: account $u is already existing.
else
useradd -g $p_group $u || {
echo $0: Warning: failed to create account $u.
add_error=1
}
fi
[ "$add_error" ] || {
# set account aging, more specification avaliable
chage -M $m_day -E $e_date $u
# password is the commbination of user name and group name with a @ between
# it is better to be generated randomly, with notification to administrator or/and users
echo "${u}@$p_group" | passwd --stdin $u
}
unset add_error
done