1833555 Members
3024 Online
110061 Solutions
New Discussion

Re: scripting question

 
SOLVED
Go to solution
Ratzie
Super Advisor

scripting question

Is there a way that I can associate one file for another...
Let me explain...
I have a script that cats a directory for find $SCRIPTS_DIR/reset_pass*.exp -print > /tmp/reset_pass.input

This file contains 4 entries reset_pass_user1 thru 4.

I then do a for loop on /tmp/reset_pass.input that does a number of commands.
What I would like to do, is there is another file created before hand that is associated to user1 throught 4. login_entry_user1, login_entry_user2, etc.

I want to try to do an I dont know if an if statement is the right way, but while going thru the for loop for reset_pass_user1,say, find the related file that was created, IE: login_entry_user1 and cat that file and place it in another file.

I hope I explained it right.
4 REPLIES 4
Mark Grant
Honored Contributor

Re: scripting question

You can't associate it as you have mentioned but if you can make the names similar. For example if the file associated with reset_pass_user2 was called otherfile.user2 then you could find that file by reference to the "user2" part. You could us "awk" or "expr" (expr is my favourite - why use an entire programming language just to extract a substring!) to extract the bit you need.
Never preceed any demonstration with anything more predictive than "watch this"
Sridhar Bhaskarla
Honored Contributor
Solution

Re: scripting question

Hi,

Let me see if I understood it right.

I would get the user name first from the reset_pass entry and then use it to identify
the login_entry.

For ex.,

for ENTRY in $(cat file)
do
USER=$(echo $ENTRY|awk '{FS="_";print $3}')
FILE=login_entry_${USER}
echo file is $FILE
done


In the above your FILE is login_entry_userx. So, you can anything you want with it. For ex., if [ -f $FILE ] etc.,

-Sri



You may be disappointed if you fail, but you are doomed if you don't try
Rodney Hills
Honored Contributor

Re: scripting question

Not sure I understand what you are looking for, but here is a perl script that will look in the "$SCRIPTS_DIR" for filenames that match the types you are interested in. It will then copy the contents (for each user, as defined as part of the filename) into /tmp. So /tmp will have one file for each user found under the directory.

$scripts=$ENV{"SCRIPTS_DIR"};
open(INP,"ls $scripts");
while() {
chomp;
if (/(.+_)(.*)/) {
$a=$1; $b=$2;
if ($a eq "reset_pass_") {
($user)=split('\.',$b);
} elsif ($a eq "login_entry_") {
$user=$b;
} else {
$user="";
}
push(@{$hold{$user}},$_);
}
}
foreach $user (sort keys %hold) {
open(OUT,">/tmp/$user");
foreach $file (@{$hold{$user}}) {
open(FIL,"<$scripts");
while() { print OUT $_; }
close(FIL);
}
close(OUT);
}

HTH

-- Rod Hills
There be dragons...
Elmar P. Kolkman
Honored Contributor

Re: scripting question

Some minor issues with Sridhar's solution:
1) '_' in your $SCRIPTS_DIR will result in nasty problems
2) $SCRIPTSDIR cannot be too long, or you'll run out of commandline length in the for statement

And I will go for a smaller program to cut your string... like Mark already mentioned. But I'm more a 'cut' fan.

My solution would be:
cat | while read ENTRY
do
USER=$(basename "$ENTRY" | cut -d_ -f3)
FILE=login_entry_${USER}
echo $FILE
done
Every problem has at least one solution. Only some solutions are harder to find.