Browse Source

Changes to createperson

- Added command line options
- Removed calls to `read` in order to make the script work without user
  interaction

If necessary, I might create a new script in the future that enables
interactive user creation again, but this time only calling the existing
(non-interactive) scripts.
theo 2 years ago
parent
commit
93da9bfbfb
1 changed files with 30 additions and 22 deletions
  1. 30 22
      createperson

+ 30 - 22
createperson

@@ -1,11 +1,34 @@
-#!/bin/bash
+#!/bin/bash -e
 . .env
 . funcs
 
-read -p "UID: " uid
-read -p "GivenName: " gn
-read -p "Name: " sn
-pw=$(/usr/bin/slappasswd -s salut$uid)
+usage() {
+	echo "Usage : $(basename $0) -u <uid> -n <First name> -s <Last Name> [-p] [-g group1,group2,...]"
+}
+
+trap usage ERR
+
+while getopts 'u:n:s:p?g:?' opt; do
+  case "$opt" in
+    u)
+      uid="$OPTARG"
+      ;;
+    n)
+      gn="$OPTARG"
+      ;;
+    s)
+      sn="$OPTARG"
+      ;;
+    p)
+      posix="true"
+      ;;
+    g)
+      grouplist=$( echo $OPTARG | tr ',' ' ')
+      ;;
+  esac
+done
+
+pw=$(/usr/sbin/slappasswd -s "salut$uid")
 
 if user_exists $uid; then
     echo "User $uid already exist." 
@@ -23,21 +46,6 @@ userPassword: $pw"
 
 echo "$user" | ldapadd $LDAPOPTS
 
-read -p "User added. Do you want to make it a posixAccount? [y/N] " a
+[ -n "$posix" ] && echo posix &&  ./person2posix $uid
 
-case $a in
-    y*|o* )
-	./person2posix $uid;;
-    * )
-        true;;
-esac
-
-read -p "User added. Do you want to add them to groups ? [y/N] " a
-
-case $a in
-    y*|o* )
-        read -p "Enter list of groups separated by spaces: " grouplist
-	./addtogroups $uid $grouplist;;
-    * )
-        true;;
-esac
+[ -n "$grouplist" ] && echo groups && ./addtogroups $uid $grouplist