#!/bin/bash# pass a username parameter to the script, determine if the user's user name is consistent with the group name of their base group, and display the result # to determine if a parameter is passed if [! $#-eq 1]; Then echo "usage:./group.sh ARG" exit 1fi# determine if the user ID is present $ &>/dev/nullif [! $?-eq 0]; then echo "\ n OT exits. " Exit 1fi# takes a given user to the base group iduserid= ' ID $ | Cut-d '-f3 | cut-d=-f2 | Cut-d ' ('-f1 ' #取出组名称GROUPNAME = ' grep '. *:.*: $USERID:. * "/etc/group | cut-d:-f1 ' #判断用户名和组名是否一致if [$ = $GROUPNAME]; then< C4/>echo "UserName equals GroupName." else echo "UserName not equals GroupName." fi############ #判断结束 ############
Optimization:
1#!/bin/Bash2 3 #传递一个用户名参数给脚本, determine whether the user name of this user is consistent with the group name of their base group and display the results4 5 #判断是否传递一个参数6 if[! $#-eq1]; Then7 Echo "usage:./group.sh ARG"8Exit19 fiTen One #判断是否存在该用户 A#ID$1&>/dev/NULL - if!ID$1&>/dev/NULL; Then - Echo "$ not exits." theExit1 - fi - - #取给定用户所属基本组id +#USERID = 'ID$1|Cut-D' '-f3 |Cut-d=-f2 |Cut-D'('-F1 ' - #取出组名称 +#GROUPNAME = 'grep ". *:.*: $USERID:. *"/etc/group |Cut-D:-F1 ' A at #判断用户名和组名是否一致 - if[ `ID-u-n $1` == `ID-g-n $1` ]; Then - Echo "UserName equals GroupName." - Else - Echo "UserName not equals GroupName." - fi in############ #判断结束 ############
Determine whether the user's user name and group name of their base group are consistent