Operating System - HP-UX
1834475 Members
2592 Online
110067 Solutions
New Discussion

Re: Need script for profile changes on new users

 
Rudi Martin
Advisor

Need script for profile changes on new users

Hi everyone.

I need to write a script that can run to make a new user's .profile.

It has to ask me the user's home dir and plant name and then copies a standard .profile into their place.

Thanks
Regards
Rudi
5 REPLIES 5
Siddhartha M
Frequent Advisor

Re: Need script for profile changes on new users

#!/bin/sh
LOGIN=$1
UID=$2
GID=$3
HOME="/home/$LOGIN"
SHELL="/usr/bin/ksh"
SKEL_DIR="/etc/skel"

echo "useradd -u $UID -g $GID -d $HOME -s $SHELL -m -k $SKEL_DIR $LOGIN"
useradd -u $UID -g $GID -d $HOME -s $SHELL
-m -k $SKEL_DIR $LOGIN


This script would accept your login name,Userid,GroupID as arguments and create
a user for you.
It would also copy the standard initialization files ( including .profile from /etc/skel directory.
Fragon
Trusted Contributor

Re: Need script for profile changes on new users

Hi Rudi,
I think you can only offer the username, because the home directory of the user is defined in /etc/passwd. So the script like this:
#!/usr/bin/sh
dest_dir=`more /etc/passwd|grep "^$1:"|cut -d : -f 6`
cp -p /etc/skel/.profile $dest_dir
unset dest_dir

If you want to supply user's home directory ,that's more easy:
#!/usr/bin/sh
cp -p /etc/skel/.profile $1

-ux
F. X. de Montgolfier
Valued Contributor

Re: Need script for profile changes on new users

Hi Rudi,

do you absolutely need to create the .profile for each new user manually?

The obvious solution would be to use /etc/profile instead of copying the same info for each profile:

------------------
# man 4 profile
profile(4)

NAME
profile - set up user's environment at login time

DESCRIPTION
If the file /etc/profile exists, it is executed by the shell for every
user who logs in. The file /etc/profile should be set up to do only
those things that are desirable for every user on the system, or to
set reasonable defaults. If a user's login (home) directory contains
a file named .profile, that file is executed (via the shell's exec
.profile) before the session begins. .profile files are useful for
setting various environment parameters, setting terminal modes, or
overriding some or all of the results of executing /etc/profile.

----------------

Hope this helps,

FiX


John Meissner
Esteemed Contributor

Re: Need script for profile changes on new users

Here is a script I've written to do just that.



#! /usr/bin/ksh
if [ `id|cut -c5-11` != "0(root)" ]
then
echo " Quitting - you must run this script as root"
exit 1
fi
BOLD="`tput smso`"
NORMAL="`tput rmso`"
if [ "$1" = "" ]
then
x=1
elif [ "$1" != " " ] && [ "$2" != " " ] &&
[ "$3" != " " ] && [ "$4" != " " ] ;
then
x=2
fi
case $x in
1)
echo "${BOLD}too few arguments${NORMAL}"
echo "usage: `basename $0` {first name} {last name} {uid} {site}"
;;
2)
five=$(echo $2 | cut -c 1-5)
three=$(echo $1 | cut -c 1-3)
usname=$(echo $five$three)
username=$(echo $usname | awk '{print tolower($1)}')
uidnum=$3
site=$4
if [ "$site" = "" ]
then site=MTO
fi
echo "useradd -u $uidnum -g 20 -m -d /home/$username -s /usr/bin/ksh -c \"$1 $2,$site\" $username"
useradd -u $uidnum -g 20 -m -d /home/$username -s /usr/bin/ksh -c "$1 $2,$site" $username
cp .profile.default /home/$username/.
passwd $username
;;
esac

All paths lead to destiny
Robert Salter
Respected Contributor

Re: Need script for profile changes on new users

Are you looking to have a particular .profile for particular users?

If so you could create a /etc/whatever directory and in it you could create any type of profile and environment files you wanted for those users. When adding their logins just specify the /etc/whatever instead of /etc/skel, i.e.
useradd -u 123 -g users -d /home/being -s /bin/ksh -m -k /etc/whatever being

If you already have the users and want to make a bulk change, get a list of the user IDs into a file;

ll /home |awk '{print $9}' > /tmp/filname

you can edit /tmp/filename to make any last minute changes, then

for i in `cat /tmp/filename`
do
if [ -f /home/$i/.profile ]; then
cp /etc/whatever /home/$i/.profile
chown $i /home/$i/.profile
ll /home/$i/.profile # for your verification
done

later
Time to smoke and joke