Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Using the user defined bash script to create groups and users

Code Blocknoformat
\#\!/bin/bash
\# Author: Henry Jiang
\# Script: setupAccess
\# Summary: set up groups and users
\# Syntax: setupAccess.sh <group> <user>

if \[\[ \! $# > 0 \]\]
then
&nbsp;&nbsp;
   echo "Usage: setupAccess <group> <user> "
&nbsp;&nbsp;
   exit
fi
if \[\[ $# == 0 \]\]
then
&nbsp;&nbsp;
   echo "Please specify a group name"
&nbsp;&nbsp;
   exit
fi
if \[\[ $# == 1 \]\]
then
&nbsp;&nbsp;   echo "Please specify a user name"
&nbsp;&nbsp;   exit
fi

group="$1"
user="$2"
defaultpass="newpass"
password=`openssl passwd $defaultpass`

if \[\[ \! $? == 0 \]\]
then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        echo "openssl not installed\!"
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        exit
fi
\#Try to create the group, if it already exist, add user to group
groupadd \-r $group

if \[\[ \! $? == 0 \]\]
then
&nbsp;&nbsp;
   echo "Group $group already exists, adding user: $user to group."
&nbsp;&nbsp;
   useradd \-g $group \-p $password \-d /home/$user&nbsp;  \-m $user
&nbsp;&nbsp;
   if \[\[ $? == 0 \]\]
&nbsp;&nbsp; then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   then
        echo "User '$user' added to group '$group' with default password '$defaultpass'"
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit
&nbsp;&nbsp; else
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        exit
   else
        echo "failed to create user"
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit
&nbsp;&nbsp;
        exit
   fi
else
&nbsp;&nbsp;   echo "Group $group has been created, creating user: $user ..."
&nbsp;&nbsp;
   useradd \-g $group \-p $password \-d /home/$user&nbsp;  \-m $user
&nbsp;&nbsp;
   if \[\[ $? == 0 \]\]
&nbsp;&nbsp;
   then
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        echo "User '$user' added to group '$group' with default password '$defaultpass'"
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit
&nbsp;&nbsp; else
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        exit
   else
        echo "failed to create user"
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit
&nbsp;&nbsp;
        exit
   fi
fi