码迷,mamicode.com
首页 > 系统相关 > 详细

Shell脚本:推断用户和用户组是否已经存在/创建用户和用户组

时间:2017-06-27 20:46:25      阅读:1112      评论:0      收藏:0      [点我收藏+]

标签:groupadd   创建用户   egrep   code   一个   ted   shell脚本   log   nbsp   

通常作为一个应用程序的部署脚本,開始的第一项工作是为当前应用创建一个专用(dedicated)的用户和用户组。这个脚本非常easy。这里贴一个參考样本:


#!/bin/sh
user=test_user
group=test_group

#create group if not exists
egrep "^$group" /etc/group >& /dev/null
if [ $? -ne 0 ]
then
    groupadd $group
fi

#create user if not exists
egrep "^$user" /etc/passwd >& /dev/null
if [ $? -ne 0 ]
then
    useradd -g $group $user
fi

 对于加入用户来说,我们还能够使用id命令来推断一个用户是否存在,这样建立一个用户的脚本能够这样写:

#create user if not exists
id $user >& /dev/null
if [ $?

-ne 0 ] then useradd -g $group $user fi


可是。使用id命令不能推断一个用户组是否已经存在!至于使用id -g $user仅仅能给出一个已存在的用户所属的用户组是什么,并不能推断一个用户组是否已经存在。所以,为了使用脚本的处理方式统一。我们统一使用从/etc/group和/etc/passwd文件里进行查找的方式来推断一个用户组和用户是否存在!

Shell脚本:推断用户和用户组是否已经存在/创建用户和用户组

标签:groupadd   创建用户   egrep   code   一个   ted   shell脚本   log   nbsp   

原文地址:http://www.cnblogs.com/blfbuaa/p/7086858.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!