Linux系统没有groupadd命令,也不能联网安装,怎么办?自己动手,丰衣足食,通过一个shell脚本搞定
脚本groupadd
#!/bin/bash
# vim:sw=4:ts=4:et
<<INFO
SCRIPYT:groupadd
AUTHOR:运维@小兵
DATE:2020-12-06
DESCRIBE:创建组
SYSTEM:CentOS7/RedHat7
MODIFY:
INFO
set -e
Log(){
local log_level=$1
local log_info=$2
local script_name=$(basename $0)
case ${log_level} in
"INFO")
echo -e "\033[32m$(date "+%Y-%m-%d %T.%N") [INFO]: ${log_info}\033[0m";;
"WARN")
echo -e "\033[33m$(date "+%Y-%m-%d %T.%N") [WARN]: ${log_info}\033[0m";;
"ERROR")
echo -e "\033[31m$(date "+%Y-%m-%d %T.%N") [ERROR ${script_name} ${FUNCNAME[1]}]: ${log_info}\033[0m";;
*)
echo -e "${@}"
;;
esac
}
[ "$#" -ne 1 ] && Log ERROR "Param Error,eg:sudo groupadd 组名" && exit 1
GROUP_NAME=$1
while :
do
GID=`echo $(($RANDOM%20000+10000))` #GID范围10000~30000
if grep -q "^${GROUP_NAME}:" /etc/group;then
Log ERROR "${GROUP_NAME} Is Exist!!!" && exit 1
elif ! grep -q ":${GID}:" /etc/group;then
Log INFO "Create Group ${GROUP_NAME}"
[ ! -f "/etc/group.bak" ] && sudo cp /etc/group{,.bak}
sed -i "\$a ${GROUP_NAME}:x:${GID}:" /etc/group
exit 0
fi
done
set +e
使用
chmod +x groupadd
cp groupadd /usr/local/bin/
grouadd t2