Operating System - HP-UX
1748171 Members
4293 Online
108758 Solutions
New Discussion юеВ

Sed /perl/Awk insert - code into user /home/username/.profile

 
SOLVED
Go to solution
rmueller58
Valued Contributor

Sed /perl/Awk insert - code into user /home/username/.profile

I have a question

I need to create a for or do-while that inserts a section of code into the users .profile

###
Say I have a file /tmp/LOCKINFO
that contains..

if [ -d "/tmp/USERLOCK" ]
then
cat /etc/motd
echo "press any key to continue\n"
read
exit
fi


I want to insert into the top of EACH user profile

Basically I have this working in a couple test

cd /home/groupname/
for file in `find . -name .profile -print`
do

### SOME COMMAND to INSERT CODE above into

Doing this put it at the bottom of the file.
cat /tmp/USERLOCK >> $FILE

I need to some how insert this code or call this from the top?

Any ideas appreciated
7 REPLIES 7
Tingli
Esteemed Contributor

Re: Sed /perl/Awk insert - code into user /home/username/.profile

Can you put this in the end of file /etc/profile?
rmueller58
Valued Contributor

Re: Sed /perl/Awk insert - code into user /home/username/.profile

we have multiple groups (districts..)

for example, I /tmp/dist1LOCK exists, then exit. But I have a couple users in this dist1 that I need to allow access, this is why I want to populate user profile.

Michael Steele_2
Honored Contributor

Re: Sed /perl/Awk insert - code into user /home/username/.profile

Hi, and yeah, MOTD is found under /etc/profile in the format:

cat /etc/motd, where /etc/motd contains your message.

So it looks like your code will work in /etc/profile to me also.
Support Fatherhood - Stop Family Law
James R. Ferguson
Acclaimed Contributor
Solution

Re: Sed /perl/Awk insert - code into user /home/username/.profile

Hi Rex:

You could insert your block of code :

...at the top of the '.profile' beginning after line-1:

# cat .insertattop
#!/usr/bin/perl
use strict;
use warnings;
local $^I = '.old';
my $insert = <<"EOF!"
if [ -d "/tmp/USERLOCK" ]
then
cat /etc/motd
echo "press any key to continue\n"
read
exit
fi
EOF!
;
while (<>) {
print $insert if ( $. == 2 );
print;
}
continue {
close ARGV if eof;
}
1;

...or, at the bottom of the '.profile' as the last block of lines:

# cat .insertatbot
#!/usr/bin/perl
use strict;
use warnings;
local $^I = '.old';
my $insert = <<"EOF!"
if [ -d "/tmp/USERLOCK" ]
then
cat /etc/motd
echo "press any key to continue\n"
read
exit
fi
EOF!
;
while (<>) {
print;
}
continue {
if (eof) {
print $insert;
close ARGV;
}
}
1;

...In *both* cases, run the script and pass one or more filenames as its arguments. In both cases, a backup copy of the original file will be created with a suffix of '.old' and the new file updated in-place.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Sed /perl/Awk insert - code into user /home/username/.profile

Hi (again):

And, though I probably don't need to say, you can couple either of the above scripts to a driving 'find()' by doing:

# cd /home/groupname && find . -type f -name .profile -exec /usr/local/bin/insertattop {} +

Regards!

...JRF...
Tim Nelson
Honored Contributor

Re: Sed /perl/Awk insert - code into user /home/username/.profile

not to be too simplistic but how about something like the below..


cat /tmp/USERLOCK > /tmp/mycodefile
cat /home/user/.profile >> /tmp/mycodefile
cp/tmp/mycode /home/user/.profile



rmueller58
Valued Contributor

Re: Sed /perl/Awk insert - code into user /home/username/.profile

Thanks James and Tim.. Got it taken care of.

As always, you guys are tops..