SUMMARY: insert text into .profile

From: joe joe (daytona5891@yahoo.com)
Date: Thu Feb 20 2003 - 16:14:41 EST


from daytona5891@yahoo.com (Joe Joe Sheppard)

Contents:
thanks
original email
futhur clarification
advise received

Thanks:

Thanks to all to helped!! This is a great list to be
on. Appreciation to the following helpers:
Alan Davis
Trevor Osatchuk
Dr. Tim Cutts
J.A. Gutierrez
Bob Vickers
Greg Palmer
Mandell Degerness

Original Email:

Greetings,
As a new sys admin, I am in need of some scripting
help. I need to change the path in the .profile of
all my users from:
. /lawson/802/law/system/profile
to
. /lawson/law/system/profile
I could insert the new part, 802/, by hand, but would
prefer a quick script. Thanks for any help you can
give me.

Further Clarification:

When I arrived this morning, I was notified that NOT
all the /usr/users had to be changed. In addition,
linking or changing the /etc/profile was refused.

Further complicating things was the fact that my
original email was unclear to whether I was the 802
text, or was trying to remove it. I was trying to
actually add it. I was able to use a consortium of
Trevor's and Mandell's responses to solve the issue.

Advice Received From:

Alan Davis:
 perl -pi.bak -e 's/<oldtext>/<newtext>/g' file file
file ...

you could wrap this in a find command :

find / -name .profile -exec /bin/perl -pi.bak -e
's/\/802/\/\//g' {} \;

Trevor Osatchuk:
There are a couple of things you can do here.

1. If all users have the same .profile file you only
need to change one and then copy it to all
directories.

OR

2. If everyone has added their own changes to their
.profile files making them all unique, you can do this
in two steps from the command line:

        1. Run this command, this will substitute all
instances of /802 for nothing, eliminating it from the
path for every .profile in /usr/users/*/ and make a
new .profile file in all of the home directories
called .profile.new. This assumes all user home
directories are in /usr/users/*.

for file in `ls /user/users/*/.profile; do sed
's#/802##' $file > $file.new;

done

        2. To change the .profile.new files to be the
new .profile file do

this:
for file in `ls /user/users/*/.chhrc; do mv $file.new
$file; done

This will move the .profile.new files to .profile. I
have tested this here on my site and it seems to work.
 Run the first command and make sure the .profile.new
files are in the home directories and that the
appropriate change was made. Like I said, this will
eliminate ALL instances of /802, so if it show up
somewhere else you may have to change the sed command
to search for the correct line. If this is the case
give it try yourself and if you still need help just
email me back. Check out the sed man page and play
with the for loop from the command line. You can do
some pretty cool stuff without having to write a
script!

Dr. Tim Cutts:
    perl -npi -e
's:^\s*\.\s*/lawson/802/law/system/profile:.
/lawson/law/system/profile:'

Apply that to each user's .profile. If you want to
find all the
.profile files in users' home directories, you can do
something like:

foreach hd ( `awk -F: '$3>100 && $1 !~ /nobody/ {print
$6}' /etc/passwd
| sort -u` )
   if ( -f $hd/.profile ) then
     perl [ perl_command_above ] $hd/.profile
   endif
end

but take *great* care. :-)
It's probably safest not to change their profiles at
all, but just to
make a symbolic link from the old location to the new
one.

J.A. Gutierrez:
You could use something similar to this one:
---------------------------------------------------------------------------
#!/bin/sh

while read user
do

        grep timenet /home/$user/.cshrc ||\
        (

                        ed /home/$user/.cshrc <<- ++
                        /foreach f/s/)$/ timenet )/
                        w
                        q
                        ++
        )
        grep timenet /home/$user/.zlogin ||\
        (
                        ed /home/$user/.zlogin <<- ++
                        /for f in/s/$/ timenet/
                        w
                        q
                        ++
        )
        grep timenet /home/$user/.profile ||\
        (
                        ed /home/$user/.profile <<- ++
                        /for f in/s/$/ timenet/
                        w
                        q
                        ++
        )
done < u

Bob Vickers:
Dear Joe Joe,
I wrote a script which lets you edit lots of files at
once. Downloadmassedit from
http://www.cs.rhul.ac.uk/home/bobv/utils/ and type
something like
massedit /lawson/802/law/system/profile
/lawson/law/system/profile
*/.profile

Michael Campbell:
Joe.. Could you just "vi" the .profile file and end
the misery?

Greg Palmer:
 If ALL your users need the change, why not just
remove the old
profile, and
recreate it as a symbolic link to the new one?
BTW--your note said the 802 was the OLD one (from/to)
was this right?
Your
note later said 'I could insert the new part 802/'...?
mv /lawson/law/system/profile
/lawson/law/system/profile.orig
ln -s /lawson/802/law/system/profile
/lawson/law/system/profile
This points the /lawson/law/system/profile to
/lawson/802/law/system/profile

Mandell Degerness:
This will insert...
sed
's/\/lawson\/law\/system\/profile/\/lawson\/802\/law\/system\/profile/g'
<file> > <file>.new
mv <file>.new <file>

__________________________________________________
Do you Yahoo!?
Yahoo! Tax Center - forms, calculators, tips, more
http://taxes.yahoo.com/



This archive was generated by hypermail 2.1.7 : Sat Apr 12 2008 - 10:49:08 EDT