我已经掌握了bash脚本的基本知识,并且需要创建一个函数来创建具有自定义设置的用户: 1.用户名和密码 2.组和用户ID 3.Comment 4.Home目录
`function user_custom {
if [ $(id -u) -eq 0 ] ; then
read -p "Enter name here: " username
read -s -p "Enter password: " password
grep "^$username" /etc/passwd > /dev/null
if [ $? -eq 0]; then
echo -e "${LIGHTGREEN}This user already exists! Try another name
please.${RESET}" ; break
else
pass=$(perl -e 'print crypt(($AVG[0], "password")' $password)
read -p "Enter group for user: "GID
egrep "^$GID" /etc/passwd > /dev/null
if [ $? -eq 0] ; then
$GID=$?
else
echo "No such group"
fi
read -p "Enter custom comment for $username: "comment
read -p "Enter unique user ID" Uid
getent passwd `grep "^Uid" /proc/$3/status |awk '{printf "%4s",$2}'` | if
[$? -eq $Uid ] ; then
echo "Select diferent ID"
fi
useradd -m -p -u -c -g $username $pass $Uid $comment $GID
fi
}`
我准备好了一个功能,但我确信有很多明显的错误。 它就像一条修补过的裙子,我收集了很多来自不同来源的数据。
如果有人可以帮忙解决问题,我将感激不尽。 我提供了整个功能的截图。
我来自乌克兰,但我擅长英语\俄语。
PS:顺便说一句,包括最后的'if',所有ifs都是closeTd
非常感谢! < 3
答案 0 :(得分:0)
我为你纠正,我希望这符合你的需要。你有很多错字,比如if [ anything ]; then
,在[和之前]之后总是需要空间。
制动仅适用于循环,您需要使用退出1来终止错误代码为1的函数。
我不想在脚本中加密密码,而是让passwd完成这项工作,它还会强制执行密码规则。
以下方法可以将几乎所有内容传递给命令,就像您从控制台输入一样:
(echo $password ; echo $password ) | passwd $username
我会添加很多其他检查来使此脚本强制执行以下操作:
还有一些功能如下:
这只是我现在想到的那些,但可能会有更多。
#!/bin/bash
function user_custom {
if [ $(id -u) -eq 0 ] ; then
read -p "Enter name here: " username ;
read -s -p "Enter password: " password ; echo
grep "^$username" /etc/passwd > /dev/null
if [ $? -eq 0 ] ; then
echo -e "\nThis user already exists! Try another name please.\n" ; exit 1
else
read -p "Enter group for user: " GID
egrep "^"$GID":" /etc/group > /dev/null
if [ $? -gt 0 ] ; then
echo "No such group ( "$GID" )"; exit 1
fi
read -p "Enter custom comment for $username: " comment
read -p "Enter unique user ID: " Uid ; echo
if [ $(getent passwd | awk -F ":" '{print $3}' | egrep -c "^"$Uid"$" ) -gt 0 ];then
echo " ID already exist! " ; exit 1
fi
fi
useradd -m -u $Uid -c $comment -g $GID $username &&
(echo $password ; echo $password ) | passwd $username
fi
}
user_custom