I am trying to create a shell script that will:
- check if a specific user already exists
- if not, create a specific group and create the user in that group
- assign a password to that user, where the password is passed in as a parameter to the script
The problem that I need help with is 3 on Solaris and AIX.
I can do 1 (using the id command) and 2 (using mkgroup or groupadd and useradd commands), but setting the password is where I am having problems. I have got it to work on Linux by redirecting input to the passwd command but I can not find how to get the same functionality to work on AIX and Solaris. This is what I have so far:
PLATFORM=`uname`
id $NEW_USER_ID > /dev/null 2>&1
if [ "$?" != "0" ]
then
echo "$NEW_USER_ID user does not exist. Attempting to create user and group"
if [ "$PLATFORM" = "AIX" ]
then
mkgroup $NEW_GROUP
else
groupadd $NEW_GROUP
fi
if [ "$?" != "0" ]
then
echo "Failed to create group"
fi
useradd -g $NEW_GROUP -s /usr/bin/ksh $NEW_USER_ID
if [ "$?" != "0" ]
then
echo "Failed to create $NEW_USER_ID user"
fi
if [ "$PLATFORM" = "Linux" ]
then
passwd --stdin $NEW_USER_ID <<EOF
$NEW_PASSWD
$NEW_PASSWD
EOF
if [ "$?" != "0" ]
then
echo "Failed to set $NEW_USER_ID password"
fi
fi
fi
I've searched the forums and found some similar postings but nothing seems to answer exactly what I'm looking for.
Some additional information that might preempt some questions:
- the script will only be run by the root user
- there is not an issue with storing the password unencrypted in a flat file because it will be entered by the user as a parameter at run-time
- the script has to be executable on Linux X86, Linux s390x, Solaris 10, and AIX 6
- the script can not use any command, utility, or any other software that is not installed on the OS by default, so an answer along the lines of "download and install this..." is no good to me.
- I can not use an answer that involves using perl
- I can not use an answer that involves using the expect command
If it can't be done, because of something like OS security restrictions and limitations, then please let me know and I'll stop trying to script it!
Thanks in advance for any help or pointers that anyone can provide.