linux脚本交换式,Linux下交互式添加用户脚本(四个示例)

最近需要用到Linux下脚本交互方式添加用户,自己整了三个脚本分享并记录一下,便于后续使用,同时也分享下,希望能帮到和我一样的Shell脚本菜鸟。

示例一:优雅方式添加用户

根据提示,输入你要添加的用户,如果用户存在,则提示后,退出脚本;#!/bin/sh

# This scripts is created by miaocbin

# QQ:289303323

# blog:http://miaocbin.blog.51cto.com

# 优雅方式添加用户:如果用户存在,则提示已存在,并退出,如果不存在就增加,并且设置密码;

echo -n "Please input your username:"

read username

grep "$username" /etc/passwd > /dev/null 2>&1

if [ $? -eq 0 ] ;

then

echo $username exist.

exit 0

else

echo $username is not exist.

echo "now create user $username"

/usr/sbin/useradd $username

echo -n "Please input your password:"

read password

echo “${password}123” |passwd --stdin $username

echo "User $username have been added,the password is ${password}123"

fi

示例二:优雅方式添加用户

与示例一类似,只是实现方式不同而已,由此可知,Linux中条条道路通罗马,各种实现方式需要不断研究,寻找合适自己的才是最好的。#!/bin/sh

# This scripts is created by miaocbin

# QQ:289303323

# blog:http://miaocbin.blog.51cto.com

#优雅方式添加用户

echo -n "Please input your username:"

read username

if cat /etc/passwd | awk -F : '{print $1}' | grep $username >/dev/null 2>&1

then

echo "User $username already exists"

else

/usr/sbin/useradd $username

echo -n "enter your password:"

read password

echo "${password}123" |passwd --stdin $username

echo "User $username have been added,the password is ${password}123"

fi

示例三:优雅方式添加用户(简单、推荐)#!/bin/sh

# This scripts is created by miaocbin

# QQ:289303323

# blog:http://miaocbin.blog.51cto.com

#优雅方式添加用户

echo -n "Please input your username:"

read username

id $username >/dev/null 2>&1

if [ $? -ne 0 ] ;

then

echo $username is not exist.

echo "now create user $username"

/usr/sbin/useradd $username

echo -n "Please input your password:"

read password

echo “${password}123” |passwd --stdin $username

echo "User $username have been added,the password is ${password}123"

else

echo $username exist.

exit 0

fi

示例四:暴力方式添加用户(慎用!)

先递归删除你输入的用户,然后重新添加,并设置密码,生产环境中请慎用此脚本,此脚本会递归删除用户及其相关的所有数据。#!/bin/sh

# This scripts is created by miaocbin

# QQ:289303323

# blog:http://miaocbin.blog.51cto.com

#暴力方式添加用户,如用户存在,怎删除后,重新添加

echo -n "Please input your username:"

read username

grep "$username" /etc/passwd > /dev/null 2>&1

if [ $? -eq 0 ] ;

then

echo $username exist.

echo "now delete the user $username and Readd the user $username"

/usr/sbin/userdel -r $username

/usr/sbin/useradd $username

echo -n "Please input your password:"

read password

echo “${password}123” |passwd --stdin $username

echo "User $username have been added,the password is ${password}123"

fi

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值