Wednesday, 12 December 2018

The Linux Kernel 4.19 is available for the users. This Linux Kernel version comes with plenty of fixes and improvements. This article will guide you to install or upgrade to Linux Kernel 4.19 in your Ubuntu or Linux Mint system.

Functions in Shell Scripting - We have been learning Shell/Bash scripting and so far we have covered control flow (if-elif-else and case) statements and loops (for loop, while loop and select loop). This article is all about another important and widely used aspect of programming, called Functions.

A function is a block of code which has a name and performs a certain task. We can reuse this block of code by invoking it using the assigned name. Functions do not only improve readability of the program, but also reduce the redundancy in the code.

Defining a function

Its very easy to create a simple function. As said earlier, a function is a name given to a block of code and we enclose this block of code (or function body) between a pair of parenthesis. The basic syntax to create a function is as below:

Syntax:

myFunction(){
...
...
}

Or we can use a keyword function to define a function named myFunction as shown below:

function myFunction {
...
...
}

Lets take an example of a function named sayHello that just has - echo "Hello world!" in the function body.

Yay! You just created your first function. You can put any working code in the function body and give any meaningful name to your function.

Function call

Calling a function or simply a function call, is nothing but making use of the function you already created. To call a function, you must define a function. If you don't define a function, program will fail to execute giving our error. So, lets just call the function we just created - sayHello.

$ sayHello(){
> echo'Hello world!'
> }$ sayHello
Hello world!

Awesome! We just executed a block of code using a simple name! The story doesn't end here. We can provide data to the function, so that it can process the data by executing block of code on it and give us the output. Lets check this in more details.

Passing arguments to a function

In our article on Environment variables and special shell variables, we studied about positional arguments to the script. The case is exactly same here. We can make a function call, provide arguments to it and access those arguments using shell variables $1, $2, $3 and so on. In this case, $1 is the first argument to the function, $2 is the second argument the function and so on. Lets check how it works.

Here, we have made two calls to the function sayHello with two different arguments Dexter and Debra. Each time a function call is made, NAME=$1 stores the first argument to the function (Dexter and Debra in two different function calls) to variable defined as NAME. Once it is stored, we can use this variable as many times as we wish to.

Lets take another example. We create a function magicString wherein it takes an alphanumeric string as an argument and tells you how many alphabets and numerics the string has.

Sunday, 9 December 2018

The Linux Kernel 4.18 is available for the users. This Linux Kernel version comes with plenty of fixes and improvements. This article will guide you to install or upgrade to Linux Kernel 4.18 in your Ubuntu or Linux Mint system.

The Linux Kernel 4.17 is available for the users. This Linux Kernel version comes with plenty of fixes and improvements. This article will guide you to install or upgrade to Linux Kernel 4.17 in your Ubuntu or Linux Mint system.

The Linux Kernel 4.16 is available for the users. This Linux Kernel version comes with plenty of fixes and improvements. This article will guide you to install or upgrade to Linux Kernel 4.16 in your Ubuntu or Linux Mint system.

Saturday, 8 December 2018

The Linux Kernel 4.15 is available for the users. This Linux Kernel version comes with plenty of fixes and improvements. This article will guide you to install or upgrade to Linux Kernel 4.15 in your Ubuntu or Linux Mint system.