用户自动创建脚本

来源:互联网 发布:plsql导出表数据dmp 编辑:程序博客网 时间:2024/06/05 05:45

1.wc统计行数
[root@localhost mnt]# wc -l username

3 username

[root@localhost mnt]# wc -l username | cut -d ” ” -f 1

3

2.echo $?

上一条命令执行成功显示 0 | 否则显示 1

[root@localhost mnt]# cat usernacat: userna: No such file or directory[root@localhost mnt]# echo $?1[root@localhost mnt]# cat username linux1linux2linux3[root@localhost mnt]# echo $?0[root@localhost mnt]# 
#!/bin/bashUSERNUM=`wc -l /mnt/username | cut -d " " -f 1`PASSWDNUM=`wc -l /mnt/password | cut -d " " -f 1`cat /mnt/username >& /dev/nullif        [ $? = 0 ]thencat /mnt/password >& /dev/null        if                 [ $? = 0 ]        then                if                         [ ${USERNUM} = ${PASSWDNUM} ]                then                        for N in $(seq 1 $USERNUM )                        do                                USERNAME=`sed -n ${N}p /mnt/username`                                PASSWORD=`sed -n ${N}p /mnt/password`                                id $USERNAME >& /dev/null                                if                                        [ $? -ne 0 ]                                then                                      useradd $USERNAME >& /dev/null                                        echo $PASSWORD | password --stdin $USERNAME >&/dev/null                                        echo "$USERNAME 创建成功"                                else                                        echo "$USERNAME 已经存在"                                fi                        done                else                        echo 用户名字与密码个数不匹配                fi        else                echo    密码文件不存在        fielse        echo 用户文件不存在fi

[root@localhost mnt]# cat password
123
456
789
[root@localhost mnt]# cat username
linux1
linux2
linux3
[root@localhost mnt]# sh jluser.sh

linux1 创建成功
linux2 创建成功
linux3 创建成功

[root@localhost mnt]# vim jluser.sh
[root@localhost mnt]# echo 123 > password
[root@localhost mnt]# cat password
123
[root@localhost mnt]# sh jluser.sh

用户名字与密码个数不匹配

0 0