You are not logged in.

#1 2007-11-24 16:43:53

Flasher
Member
From: Bavaria / Germany
Registered: 2005-04-24
Posts: 126

Scripting problems with pipes!

Hi Boardies!

I'm working on a script to automatically generate PKGBUILDs for my custom kernels (For my 3 systems I need 3 custom kernel packages).

Each PKGBUILD needs the md5 checksum block, which I can generate with "makepkg -c > PKGBUILD".

Here is the problem wink

This command writes the checksums at the end of the PKGBUILD file. This isn't a clean solution. The checksums should wrote before the "build()" function.

Because of that I need a trickly bash command, which search (grep maybe?) the "build() "-line and writes the md5-checksum block before that line.

I tested around with egrep, but I can't isolate the line-number of "build()" and I don't know how to paste the md5-checksums be at the position (line - 1)!

I hope somebody can help me. Thanks for your answers smile

Best regards,

Flasher

Offline

#2 2007-11-24 16:50:35

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: Scripting problems with pipes!

You probably want to use either awk or sed or a combination of the two. I'm not too good with either of them anymore, so I'll refer to their comprehensive and confusing man pages.

Dusty

Offline

#3 2007-11-24 16:59:33

Sekre
Member
From: The Rainy North
Registered: 2006-11-24
Posts: 116

Re: Scripting problems with pipes!

could this be what you are looking for ?

http://www.skywayradio.com/tech/linux/sed.php wrote:

Other sed commands

11. For every line with string1, put string2 on the beginning of the preceding line

sed '/string1/a\
string2' filename

Offline

#4 2007-11-24 17:11:25

buttons
Member
From: NJ, USA
Registered: 2007-08-04
Posts: 620

Re: Scripting problems with pipes!

A simple perl script would be appropriate.  The easiest way to approach this problem (actual perl programmers feel free to correct me) is to do the following:

iterate over the file, pulling each line into a string appended to an array
iterate over the array, printing each line.  If build() is matched, print md5sum, then the line.
write the corrected file to stdout

Here's one I called test.pl

#!/usr/bin/perl
#Add MD5SUM to file

#Read from stdin into @file
while (<>) {
    chomp;
    push(@file, $_);
}

foreach my $a (@file) {
    if ($a =~ /build\(\)/) {
        print "MD5SUM\n";
    }

    print "$a\n";
}

Then the following command:

cat PKGBUILD | ./test.pl > temp
mv temp PKGBUILD

Produces an identical PKGBUILD with the word MD5SUM printed on the line above build().


Cthulhu For President!

Offline

#5 2007-11-24 18:59:56

Flasher
Member
From: Bavaria / Germany
Registered: 2005-04-24
Posts: 126

Re: Scripting problems with pipes!

Hi!

Perfect! Many thanks for your efforts smile

Greetings,

Flasher

Offline

#6 2007-11-24 19:59:03

peets
Member
From: Montreal
Registered: 2007-01-11
Posts: 936
Website

Re: Scripting problems with pipes!

Wouldn't it be simpler to do it this way?

#!/usr/bin/perl
#Add MD5SUM to file

my $md5sum = shift @ARGV;

#Read from stdin into @file
while (<>) {
    if(/build\(\)/) {
        print "md5sums=('$md5sum')\n";
    }
    print;
}

But I don't know the real PKGBUILD syntax (and also buttons' works, but I just wanted to have some fun)

Offline

#7 2007-11-24 21:50:10

tomk
Forum Fellow
From: Ireland
Registered: 2004-07-21
Posts: 9,839

Re: Scripting problems with pipes!

Notwithstanding all the above, let me just add the following note:

Flasher wrote:

This command writes the checksums at the end of the PKGBUILD file. This isn't a clean solution. The checksums should wrote before the "build()" function.

The checksums can be anywhere, just like any other PKGBUILD element. It is incorrect to say that they should be in a specific place. You may have a preference for them to be somewhere other than the end of the PKGBUILD, but that's a different matter altogether.

Offline

Board footer

Powered by FluxBB