CARDS 3.0.0
Package manager for the NuTyX GNU/Linux distribution
Loading...
Searching...
No Matches
BASH Basics

What do you need to know to contribute ?

You don't need to be an experienced programmer to contribute to this project.

A rudimentary knowledge of the bash scripting language is good enough. If you have no knowledge of bash, this article will explain the basic principles for creating a recipe without going crazy.

The bash langage is used to edit a recipe and to generate a package from it. The fact that bash is a programming language might seem frightening to some potential users, who might think: "I don't know how to program". There are three reasons why you should not feel scared:

  • Bash is an interpreted language, not a compiled one, which means that the commands you type in at the keyboard (or read in from a file) are directly executed without the need to construct a complete program.
  • The syntax of bash is very simple and intuitive.
  • Finally the recipe and the creation of the binary package from it seldom use the more complex options of the bash language.

So let's start our little course on the bash programming

This article will explain some basic features of the language that are good to knows to comprehend the syntax of a recipe:

Declaring a variable

All you need to do to declare a variable is this:

name=mypackage

You declare the variable name by using the equals sign, with the name of the variable to the left of the sign and the value mypackage to the right.

Using a variable

So you know how to declare a variable; that's very nice, but how do you get at its content in order to use it?

To extract the content of a variable you use the dollar sign ($) with braces if required. Both forms are valid:

${name}
$name

It is better to use the first form since otherwise, if the variable name contains an underscore, it will not be interpretated correctly by bash:

name=xorg-libx11
_name=libX11
$name # this works
${_name} # so does this
$_name # wrongly interpreted and won't work

In order to see the result of this command, I suggest you create a little file containing this code. You can call the file DeclareVar

name=kernel
echo "The variable name contains the value: ${name}"$

Now give the command:

sh DeclareVar
The variable name contains the value: kernel

Using predefined variables in a recipe

When you construct a recipe, you will have access to a whole series of predefined variables.

PKG: Certainly the most often used, this defines the destination folder for the compiled files.

This folder then contains the file structure before it is packaged up

SRC: This variable defines the folder where all the source files are extracted.

It is therefore very useful when you need to loop through subfolders and then return at the end of the loop to where you started. This is also the variable which defines the current working directory for the main function of the recipe.

MAKEFLAGS: This variable is not used so often. It is initialized in the pkgmk configuration file. Its use is detailed in another article. For the time being, all you need to know is that it allows you to specify the extent of parallel compilation. There are others, but personally I see no need for them when constructing recipes.

Declaring an array

To declare an array of strings (or anything else), you add parentheses:

source=(firefox.png firefox.desktop)

This array source contains 2 variables: firefox.png and firefox.desktop. The source array from the recipe for the kernel provides a particularly good example:

source=(http://www.kernel.org/pub/linux/kernel/v3.0/linux-${_BaseVersion}.tar.bz2 \
config_64 config \
http://www.kernel.org/pub/linux/kernel/v3.0/patch-$version.bz2 \
http://downloads.nutyx.org/files/patchs/$name/broadcom-wl-kernel3-01.patch \
http://downloads.nutyx.org/files/patchs/$name/3.2.8.patch \
http://downloads.nutyx.org/files/patchs/$name/3.4.patch \
http://downloads.nutyx.org/files/patchs/$name/makefile_compat.patch \
http://downloads.nutyx.org/files/patchs/$name/bc_wl_abiupdate.patch)

Each line is a variable in its own right and can be accessed using the syntax:

echo "$(source[1])"

You will notice that each variable can in turn contain other variables provided that they have been defined earlier.

Declaring a function

To declare a function is almost as simple. You use parentheses when giving the name of the function; the function code is always enclosed in braces { etc }. A little example:

build() {
cd $name-$version<br />./configure --prefix=/usr
make
make DESTDIR=$PKG install
}

As you can see, it's very pretty and also contains a minimal recipe (without the variable declarations). Here you can see the essential use of the PKG variable.

Conclusion

We are now ready to learn what's a recept or a port