LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Need Help Writing a Package Script (https://www.linuxquestions.org/questions/programming-9/need-help-writing-a-package-script-595705/)

binarybob0001 10-30-2007 02:18 AM

Need Help Writing a Package Script
 
I have designed a fairly generalized package script for Slackware 12.0. Now, what I want to do make one completely generalized build script. Here's what I have so far.
Code:

name="alsa-utils"
version="1.0.15"
dirname=$name-$version
dest="/mnt/backup/Linux/Packages"

tar -xvjf $dirname.tar.bz2          #extract tar file
cd $dirname                        #Do the whole configure/make/make install thing
./configure
make
make DESTDIR=$dest/build install    #Copy files to a temporary build directory
cd ..
rm -r $dirname                      #The uncompressed source is no longer needed
mkdir $dest/build/install          #Add a description file
cp $dirname.desc $dest/build/install/slack-desc
cd $dest/build                      #Make the package
makepkg -l y -c n $dirname-i686-1.tgz
mv $dirname-i686-1.tgz ..          #mv it to its final location
cd ..
rm -r build                        #get rid of the temporary build

This is the config script for alsa-utils and you can see what alsa-lib would look like.
Code:

name="alsa-lib"
version="1.0.15"
dirname=$name-$version
dest="/mnt/backup/Linux/Packages"

tar -xvjf $dirname.tar.bz2          #extract tar file
cd $dirname                        #Do the whole configure/make/make install thing
./configure
make
make DESTDIR=$dest/build install    #Copy files to a temporary build directory
cd ..
rm -r $dirname                      #The uncompressed source is no longer needed
mkdir $dest/build/install          #Add a description file
cp $dirname.desc $dest/build/install/slack-desc
cd $dest/build                      #Make the package
makepkg -l y -c n $dirname-i686-1.tgz
mv $dirname-i686-1.tgz ..          #mv it to its final location
cd ..
rm -r build                        #get rid of the temporary build

Not bad for my first script huh? But what I would like to do is pass in the name of a filename-version.tar.bz2 and have it automatically fill in the name and version. Can some one show me how to parse a string like that in a script?

Disillusionist 10-30-2007 03:33 AM

Am I right in thinking you want to call:

gen_script alsa-utils-1.0.15.tar.bz2

If so, try:
Code:

name =`echo $1|awk -F"-[0-9][0-9]*" ' { print $1 }'`
version=`echo $1|sed "s/$name-//"|sed 's/.tar.bz2//'`

This assumes that the name ends at a "-" followed by one or more numerics

and the version continues until .tar.bz2

binarybob0001 10-30-2007 03:43 AM

Yes, you would be correct. Regular expression are confusing me to no end. I'm trying to use expr for the same purpose, but it doesn't seem to give me the correct result.
Code:

bobby@Veronica:~$ expr 'a-b-c' : 'a'
1
bobby@Veronica:~$ expr 'a-b-c' : 'b'
0

WTF? I'm so confused. I don't know what you wrote, but it looks alien to me. Could you please break it down a little for my pitiful human mind. Thank you so much.

Disillusionist 10-30-2007 04:06 AM

First when you pass parameters into a script they come in as $1 $2 $3 etc

Therefore if you call: gen_script alsa-utils-1.0.15.tar.bz2

$1 will be equal to "alsa-utils-1.0.15.tar.bz2"

I am then passing this string to awk (stay with me)
Code:

awk -F"-[0-9][0-9]*" ' { print $1 }'
the -F sets the delimiter to split a string into sections, we are using a multicharacter delimiter beginning with a "-" and followed by any number of nummerics Examples -0 -012345 would both match.

I am then printing the first section { print $1 }

Note this $1 is seperate from the $1 passed to our script as it is a part of the awk construct.

So now we have our name variable set.

The second line passes "alsa-utils-1.0.15.tar.bz2" to sed (string editor) twice, first to search for the $name and the "-" symbol and replace it with nothing sed "s/$name-//" and then to remove the tail part of the string ".tar.bz2" sed 's/.tar.bz2//'

I really must go to work now (or I'll be late).

binarybob0001 10-30-2007 04:13 AM

Wow, thanks I will certainly have to put this stuff to the test.

gnashley 10-30-2007 01:30 PM

Why don't you have a look at shilo's generic SlackBuild script(see the This is how I do it all sticky) or AlienBob's and rworkmans' scripts at SlackBuilds.org for some ideas. I could suggest that you just use my src2pkg program which obsoletes about 80% of all manually-written SlackBuilds, but I think you are wanting to learn to write bash scripts, so it would be very counter-productive for you to use it. Didn't think I would ever say that... If you just want to make good packages, lots of packages and easily, then do use it. I learned lots of shell language in the process of writing it.

angrybanana 10-30-2007 02:43 PM

Quote:

Originally Posted by binarybob0001 (Post 2941927)
Yes, you would be correct. Regular expression are confusing me to no end. I'm trying to use expr for the same purpose, but it doesn't seem to give me the correct result.
Code:

bobby@Veronica:~$ expr 'a-b-c' : 'a'
1
bobby@Veronica:~$ expr 'a-b-c' : 'b'
0

WTF? I'm so confused. I don't know what you wrote, but it looks alien to me. Could you please break it down a little for my pitiful human mind. Thank you so much.

Regex don't make any sense until you learn them, then you love them!

Here's a perl regex example for you.
Code:

$ cat a
alsa-utils-1.0.15.tar.bz2
alsa-lib-1.0.15.tar.bz2

$ perl -lne 'print "$1 $2" if /(.*)-(.*).tar.bz2/' a
alsa-utils 1.0.15
alsa-lib 1.0.15

$1 = name $2 = version.

Using bash/expr
Code:

file=alsa-utils-1.0.15.tar.bz2
name=$(expr "$file" : "\(.*\)-.*.tar.bz2")
version=$(expr "$file" : ".*-\(.*\).tar.bz2")

Here's one more with no regex
Code:

file=alsa-utils-1.0.15.tar.bz2
name=${file%-*}
version=${file#$name-}
version=${version%.tar.bz2}

3 small steps
1. remove everything after the last "-" (sets $name to this)
2. remove the $name + a "-" from the beginning ($version="1.0.15.tar.bz2" at this point)
3. remove the ".tar.bz2" from the end of version (setting $version to this)

binarybob0001 10-31-2007 05:43 AM

Thanks for all your input guys. I'm finally starting to get the hang for regular expressions though I will have to clobber my way through a few more proplems before I can say I have really learned the well. I managed to write my own build script which I'm rather proud of. Here's the code.
Code:

#Assumes that script is run in the same directory as the tar file and that
#it has write permissions in the directory. Every file given must be
#in the form of appname-version.tar.bz2
filename=$1
name=`echo $filename | awk -F "-[0-9][0-9]*" ' { print $1 }' `    #Search for a hyphen followed by one or more numbers
source=`echo $filename | sed "s/.tar.bz2//" `                    #Search for .tar.bz2 and replace it with nothing
version=`echo $source | sed "s/$name-//" `                        #Search name of app and replace it with nothing

#If no destination is given as second argument set dest to default.
if [ -z $2 ]
then
  dest="/mnt/backup/Linux/Packages"
else
  dest=$2
fi
tar -xvjf $filename

#If application has a special build script us it.
#The build scripts must put all the files in $dest/build
if [ -x $source.inst ]
then
  ./$source.inst $dest
else
  cd $source
  ./configure
  make
  make DESTDIR=$dest/build install
  cd ..
fi

rm -r $source
mkdir $dest/build/install
cp $source.desc $dest/build/install/slack-desc
cd $dest/build
makepkg -l y -c n $source-i686-1bob.tgz
mv $source-i686-1bob.tgz ..
cd ..
rm -r build

What's nice about this script is that it gives the option for a more specific but far shorter build script. To give an example. Here's one I wrote for my ALSA driver.
Code:

dest=$1
cd alsa-driver-1.0.15
./configure --with-cards=emu10k1x
make
make DESTDIR=$dest/build install    #Copy files to a temporary build directory

So, my archive of source files looks fairly clean. Check it from and ls -l command.
Code:

-r--r--r-- 1 root root      278 2007-10-30 14:21 alsa-driver-1.0.15.desc
-r-xr--r-- 1 root root      148 2007-10-30 13:30 alsa-driver-1.0.15.inst*
-r--r--r-- 1 root root  2677415 2007-10-30 15:05 alsa-driver-1.0.15.tar.bz2
-r--r--r-- 1 root root      860 2007-10-29 23:22 alsa-lib-1.0.15.desc
-r--r--r-- 1 root root  793909 2007-10-30 15:05 alsa-lib-1.0.15.tar.bz2
-r--r--r-- 1 root root    1085 2007-10-29 23:24 alsa-utils-1.0.15.desc
-r--r--r-- 1 root root  1014199 2007-10-30 14:55 alsa-utils-1.0.15.tar.bz2

I know it's not a script to brag about, but it's my first really helpful script I've made, and I wanted to share it. Tell me what you think about it.


All times are GMT -5. The time now is 02:48 AM.