这个是我们开发机上创建用户的脚本,我简化了一下,使用很简单, ./
add_user.sh username
可以创建一个用户,只能在自己的目录下玩耍。
add_user.sh#!/bin/bash
# Script to add a user to Linux system
function generate_pass() {
MATRIX="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
LENGTH=$1
while [ "${n:=1}" -le "$LENGTH" ]
do
PASS="$PASS${MATRIX:$(($RANDOM%${#MATRIX})):1}"
let n+=1
done
echo "$PASS"
}
username=$1
if [ -z $username ];then
echo "username is empty"
exit 1
fi
password=$(generate_pass 6)
if [ $(id -u) -eq 0 ]; then
egrep "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "$username exists!"
exit 1
else
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
useradd -m -p $pass $username
# record
time=$(date "+%Y-%m-%d-%H-%M-%S")
echo "create username $username with password $password at $time" >> user_account.txt
[ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
fi
else
echo "Only root may add a user to the system"
exit 2
fi