1832844 Members
2706 Online
110047 Solutions
New Discussion

Script-wizards?

 
SOLVED
Go to solution
Petter Selin
Occasional Advisor

Script-wizards?

Hi.

I really dunno if this is the right forum to have this thread...but i`ll just post it and hope that there are some programming-wizards out there.

Problem:
I need to write a script that compares files in a dir. and the 2 files with similar extension should..
1) Write the first files content
2) ...into a file named after the second file`s content.

hehe..r u with me?

Oki...lets start with the dir..It looks like this...(but it is in the tmp dir and will change all the time)

/tmp
ldapsearch-cartype-008680 ldapsearch-cartype-f08680 ldapsearch-cartype-l08680
ldapsearch-Descriptiontype-008680

Here you see that the first line and the last looks the same if you look at the last part. (008680)...Now we just say that "echo ldapsearch-carname-008680 = Volvo" and "echo ldapsearch-Description-008680 = Blue".....Then I want the script 2 create a file namned "Volvo" with "Blue" in it. Comprende?..:)

Now when we come to the programming, and i'm really a newbie at that, i have written something like this..(which of course is`nt even close 2 work..=(...)

Solotion:

#!/bin/sh
DIR=/opt/ldap/tmp/*Description*
FILES=`ls -1 /opt/ldap/tmp/*cartype*`
OUTFILE=/opt/ldap/ldapCOMP

rm -f ${OUTFILE}

for F in $FILES;

do if [ $FILES && $Dir =~ "`type-*`" ]

then `cat ${FILES}` >> Symbols/`cat ${Dir}`
else :
fi

done


Hm...I don't expect anyone 2 waste their time on this...But...Se it as a challenge then...;)

best regards
Petter
3 REPLIES 3
Robin Wakefield
Honored Contributor
Solution

Re: Script-wizards?

Hi Petter,

Something like:

#!/bin/ksh
cd /tmp/forum
for file in ldapsearch-cartype-[0-9]* ; do
suffix=${file#ldapsearch-cartype-}
desc=ldapsearch-Description-$suffix
cat $desc > `cat $file`
done

I'm assuming the suffix always starts with a number. Loads of other ways, just the 1st one that came to mind.

Rgds, Robin.
Petter Selin
Occasional Advisor

Re: Script-wizards?

It worked! You really made my day there...Thnxs a lot!....=)

"Every problem is easy as long as you know the answer!
Robert Binkley
Advisor

Re: Script-wizards?

if you want to user the web browser
script called diff_upload.cgi

*****************************************
#!/usr/bin/perl

$DIFF = "/usr/bin/diff";
$PERL = "/usr/bin/perl";

use CGI qw(:standard);
use CGI::Carp;

print header;
print start_html("File Diff Example");
print "Version $CGI::VERSION

";

print <

File Diff Example


Enter two files. When you press "submit" their diff will be
produced.
EOF
;

# Start a multipart form.
print start_multipart_form;
print "File #1:",filefield(-name=>'file1',-size=>45),"
\n";
print "File #2:",filefield(-name=>'file2',-size=>45),"
\n";
print "Diff type: ",radio_group(-name=>'type',
-value=>['context','normal']),"
\n";
print reset,submit(-name=>'submit',-value=>'Do Diff');
print endform;

# Process the form if there is a file name entered
$file1 = param('file1');
$file2 = param('file2');

$|=1; # for buffering
if ($file1 && $file2) {
$realfile1 = tmpFileName($file1);
$realfile2 = tmpFileName($file2);
print "
\n";
print "

$file1 vs $file2

\n";

print "
\n";
$options = "-c" if param('type') eq 'context';
system "$DIFF $options $realfile1 $realfile2 | $PERL -pe 's/>/>/g; s/ close $file1;
close $file2;
print "
\n";
}

print <


Last modified 13 MAR 2001
EOF
;
print end_html;

sub sanitize {
my $name = shift;
my($safe) = $name=~/([a-zA-Z0-9._~#,]+)/;
unless ($safe) {
print "$name is not a valid Unix filename -- sorry";
exit 0;
}
return $safe;
}