cxs7225032 发表于 2018-8-23 07:47:21

shell编程高级

userad.sh  #!/bin/bash
  usage() {
  echo -e "\033 username"
  }
  while getopts ":u:" SWITCH ;do
  case $SWITCH in
  u)
  NEWUID=$OPTARG
  cut -d: -f3 /etc/passwd | grep "^NEWUID" &> /dev/null && echo "UID exists."&& exit 1
  ;;
  \?)
  usage
  exit 1
  ;;
  esac
  done
  NGID=$[`cut -d: -f3 /etc/group | grep -v "65534" |sort -n |tail -1`+1]
  NUID=$[`cut -d: -f3 /etc/passwd | grep -v "65534" |sort -n |tail -1`+1]
  TODAY=$[`date +"%s"`/86400]
  #groupadd
  if ! cut -d: -f1 /etc/group |grep $1 &> /dev/null;do
  echo "$1:x:$NGID: ">> /etc/group
  else
  echo "group $1 is already exist";exit 1
  fi
  #useradd
  if ! cut -d: -f1 /etc/passwd |grep $1 &> /dev/null;do
  echo "$1:x:${NEWUID:=$NUID}:$NGID::/home/$1:/bin/bash" >> /etc/passwd
  echo "$1:!!:$TODAY:0:99999:7:::" >> /etc/shadow
  else
  echo "user $1 is already exist";exit 1
  fi
  #home directory
  cp -r /etc/skel /home/$1
  chown -R $1:$1 /home/$1
  chmod -R go=--- /home/$1

页: [1]
查看完整版本: shell编程高级