Sunday, February 17, 2008

Bash Shell Script - Part III Of C/Shell/Perl Porting.

Good Morning,

Today's bash shell script version of our 3 part porting post, all alliteration aside, wraps up this little experiment and, hopefully, helps tie it all together.

Please note the warning, posted yesterday, about the dangers of actually using this code plain-vanilla and keep in mind that our objective here was to show how the same objective can be accomplished using, and translated between, C code, Perl and shell script.

You can see how today's code looks written in C in our previous post on C code to add user accounts that we kicked this thing off with. Then, if you like, you can check out, virtually, the exact same thing in Perl on our previous post regarding using Perl to create user accounts.

Today's script should run equally well on both Linux and Unix. If you don't use the bash shell, or don't have it installed on your version of Unix or Linux, minor modifications to this script may be necessary, but they won't be quite as intense as the difference between this script and, say, our previous C code or Perl script.

If you do choose to revisit those previous posts, hopefully we've covered enough ground in each for you to notice the things in each that are wildly different and the things that are practically the same. Next week, amongst other things, we'll continue to look at porting between languages, but at a more specific level (which will also allow us to look at C, Perl and Shell concepts within the same post :)

Hope you enjoy this, and have a restful Sunday :)


Creative Commons License


This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

#!/bin/bash

#
# adduser.sh
#
# Add Users, Set Up Profiles,
# Set The Password And Email
# An Admin
#
# 2008 - Mike Golvach - eggi@comcast.net
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

trap 'rm -f $home/.profile;exit 3' 1 2 3

if [ $# -ne 3 ]
then
echo "Usage: $0 [dirname - no slashes ] [ logname ] [ comment - in quotes ]"
exit 1
fi

userdir=$1
username=$2
commentfield="$3"
home="/${1}/${2}"
argument2=${#username}

if [ $argument2 -gt 8 -o $argument2 -lt 5 ]
then
echo "Please choose a logname between 5 and 8 characters!"
exit 1
fi

count=`awk -F":" '{print $3}' /etc/passwd|sort -n|tail -1`
let usernumber=$count+1

echo
echo "Check this out before proceeding!!!"
echo "-----------------------------------"
echo "Logname: $username"
echo "Homedir: $home"
echo "Comment: $commentfield"
echo "-----------------------------------"
echo
echo
echo "All of this ok?"
echo
echo "y or n"
echo

read reply

if [ $reply = "n" -o $reply = "N" ]
then
echo
echo "All right, give it another shot if you want!"
exit 0
elif [ $reply = "y" -o $reply = "Y" ]
then
:
else
echo "Only y or n - case insenstive allowed"
echo "Try Again"
exit 1
fi

echo "$username:x:$usernumber:1:$commentfield:/${userdir}/$username:/bin/ksh" >>/etc/passwd

echo "$username:*LK*:::::::" >>/etc/shadow

mkdir -m 0755 $home
cd $home

echo "stty istrip" >>.profile
echo "PATH=/bin:/usr/bin:/usr/local/bin:/usr/share/bin:." >>.profile
echo "export PATH" >>.profile
echo
echo

chown $username $home
chown ${usernumber}:1 .profile
chmod 0644 .profile

(echo "To: devnull@host.com";echo "Subject: New User Added!!!";echo;echo "$commentfield";echo "has a new account set up!";echo "The email address is ${username}@host.com!";echo;echo "Thank you,";echo " Mike Golvach")|/usr/lib/sendmail -t

echo
echo "All Done!!!"
echo
echo "Now set the Password!"
echo
/usr/bin/passwd $username
echo
echo "Password set!!! Take a break..."


, Mike