LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 793|回复: 8

用shell命令写一个用户添加,该怎么写?

[复制链接]
发表于 2004-9-21 23:22:18 | 显示全部楼层 |阅读模式
用shell命令写一个用户添加,该怎么写?
老师布置的 作业,小弟刚刚接触linux.
发表于 2004-9-21 23:27:24 | 显示全部楼层
直接编辑/etc/passwd文件
ps:作业要自己写!
发表于 2004-9-22 17:20:50 | 显示全部楼层
楼上的已经给出很大提示了,自己揣摩吧
 楼主| 发表于 2004-9-22 18:53:22 | 显示全部楼层
我明白了,这个我会,老师的意思就像用户注册,实在是不会,给点提示吧
 楼主| 发表于 2004-9-22 20:14:27 | 显示全部楼层
就像/usr/sbin/adduser  一样
发表于 2004-9-22 23:10:45 | 显示全部楼层
给你个提示吧
1,创建一个passfile文件
echo 1234 >passfile
2,文件大致
  1. #!/bin/bash
  2. useradd $1 && passwd $1 --stdin <passfile
复制代码

3,运行:
./creatuser abc
那么,就新添加了用户abc,他的password就是passfile中的1234,
当然,用手动也是一样的,
useradd abc
passwd abc
比较cool的方法:
修改/etc/passwd文件,例如,添加:
abc:x:507:508::/home/abc:/bin/bash
存盘退出鲎,
给它一个主目录
touch /home/abc
把abc的需要的环境cp过去
cp /etc/skel/.bash* /home/abc
给他一个登录shell
chsh -s /bin/bash abc
OK
 楼主| 发表于 2004-9-24 19:29:14 | 显示全部楼层
谢谢,这是我自己写出来的,给点意见
#!/bin/bash

echo "lease Enter Your Name:"

read name

if grep ^$name /etc/passwd >/dev/null 2>&1

then

    echo "User exist!"

else

    echo "User do not exist,You can add the User"

fi

useradd -s /bin/bash $name >/dev/null 2>&1

mkdir /home/$name >/dev/null 2>&1

touch /home/$name/.bash_profile

touch /home/$name/.bashrc

if grep ^$name /etc/passwd >/dev/null 2>&1

then

    echo "User exist!,Do you want to change the $name's password?[Y/N]"

read choice

case $choice in

              y|Y)

passwd $name

;;

               n|N)

               ;;

               esac

else

    echo "User do
not exist,Do you want to add $name's password?[Y/N]"

read choices

case $choices in

                y|Y)

echo "lease Enter The Password:"

passwd $name

;;

                n|N)

                ;;

                esac

fi

echo "Do you want to add another user?[Y/N]?"

read choice

case $choice in

            y|Y)

./adduser.sh

;;

            n|N)

;;

            esac
发表于 2004-9-25 01:18:05 | 显示全部楼层
1:
if grep ^$name /etc/passwd >/dev/null 2>&1

then

echo "User exist!"#后面加上一个退出指令比较合理,
#echo "User exist!"&&exit
else

echo "User do not exist,You can add the User"

fi
2:
useradd -s /bin/bash $name >/dev/null 2>&1
用了useradd命令,系统会自动为这个用户创建目录的,也自动会在
/etc/passwd加入相应的记录
ps:简单问题复杂化了!
发表于 2004-9-25 02:01:59 | 显示全部楼层
chpasswd 是改密码的。:)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表