Introduction to Linux - A Hands on Guide | Linux Bible | Linux From Scratch | A Newbie's Getting Started Guide to Linux | Linux Command Line Cheat Sheet | More Linux eBooks



Friday, 13 January 2017

How To: Install or Upgrade to Linux Kernel 4.9.3 in Ubuntu/Linux Mint

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


Installation

For 32-Bit Systems

Download the .deb packages.
$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.3/linux-headers-4.9.3-040903_4.9.3-040903.201701120631_all.deb

$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.3/linux-headers-4.9.3-040903-generic_4.9.3-040903.201701120631_i386.deb

$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.3/linux-image-4.9.3-040903-generic_4.9.3-040903.201701120631_i386.deb
Install them.
$ sudo dpkg -i linux-headers-4.9.3*.deb linux-image-4.9.3*.deb
Reboot the system.
sudo reboot

For 64-Bit Systems

Download the .deb packages.
$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.3/linux-headers-4.9.3-040903_4.9.3-040903.201701120631_all.deb

$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.3/linux-headers-4.9.3-040903-generic_4.9.3-040903.201701120631_amd64.deb

$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.3/linux-image-4.9.3-040903-generic_4.9.3-040903.201701120631_amd64.deb
Install them.
$ sudo dpkg -i linux-headers-4.9.3*.deb linux-image-4.9.3*.deb
Reboot the system.
sudo reboot

To uninstall,
sudo apt-get remove 'linux-headers-4.9.3*' 'linux-image-4.9.3*'

How To: Install or Upgrade to Linux Kernel 4.9.2 in Ubuntu/Linux Mint

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


Installation

For 32-Bit Systems

Download the .deb packages.
$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.2/linux-headers-4.9.2-040902_4.9.2-040902.201701090331_all.deb

$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.2/linux-headers-4.9.2-040902-generic_4.9.2-040902.201701090331_i386.deb

$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.2/linux-image-4.9.2-040902-generic_4.9.2-040902.201701090331_i386.deb
Install them.
$ sudo dpkg -i linux-headers-4.9.2*.deb linux-image-4.9.2*.deb
Reboot the system.
sudo reboot

For 64-Bit Systems

Download the .deb packages.
$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.2/linux-headers-4.9.2-040902_4.9.2-040902.201701090331_all.deb

$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.2/linux-headers-4.9.2-040902-generic_4.9.2-040902.201701090331_amd64.deb

$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.2/linux-image-4.9.2-040902-generic_4.9.2-040902.201701090331_amd64.deb
Install them.
$ sudo dpkg -i linux-headers-4.9.2*.deb linux-image-4.9.2*.deb
Reboot the system.
sudo reboot

To uninstall,
sudo apt-get remove 'linux-headers-4.9.2*' 'linux-image-4.9.2*'

Friday, 6 January 2017

How To: Install or Upgrade to Linux Kernel 4.9.1 in Ubuntu/Linux Mint

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


Installation

For 32-Bit Systems

Download the .deb packages.
$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.1/linux-headers-4.9.1-040901_4.9.1-040901.201701060531_all.deb

$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.1/linux-headers-4.9.1-040901-generic_4.9.1-040901.201701060531_i386.deb

$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.1/linux-image-4.9.1-040901-generic_4.9.1-040901.201701060531_i386.deb
Install them.
$ sudo dpkg -i linux-headers-4.9.1*.deb linux-image-4.9.1*.deb
Reboot the system.
sudo reboot

For 64-Bit Systems

Download the .deb packages.
$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.1/linux-headers-4.9.1-040901_4.9.1-040901.201701060531_all.deb

$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.1/linux-headers-4.9.1-040901-generic_4.9.1-040901.201701060531_amd64.deb

$ wget kernel.ubuntu.com/~kernel-ppa/mainline/v4.9.1/linux-image-4.9.1-040901-generic_4.9.1-040901.201701060531_amd64.deb
Install them.
$ sudo dpkg -i linux-headers-4.9.1*.deb linux-image-4.9.1*.deb
Reboot the system.
sudo reboot

To uninstall,
sudo apt-get remove 'linux-headers-4.9.1*' 'linux-image-4.9.1*'

Thursday, 29 December 2016

Python filter() Function

Python filter() Function - We have been learning some of the important utilities used in function programming that includes functions – map(), reduce() and filter(). We have already covered map() function and reduce() in our recent articles. In this article, we will learn about filter() function. Let’s begin with the discussion straight away.

python-filter-function

Using reduce() with lambdas

As the name suggests, filter() function filters out some elements from a sequence. Just like map() and reduce() functions, this function receives two arguments – a function and an iterable object. The function is usually called as filter and returns either True or False. This function is then executed on each element of the iterable. As a result, we get a list of items for which the filter returns True.

Don’t be afraid, if its difficult to understand, we will see some examples and explain them in details. To begin with, we try to extract even number from a list of numbers. We need to create the filter, at first. We can use a normal function or a lambda, but it should return either True or False only, nothing else.

Syntax:

filter( function, iterable_object )

Example :

# Create the data set
>>> myList = range(1,16)
>>> myList
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

# A very simple filter
>>> def filterEven(num):
...     if num % 2 == 0:
...             return True
...     else:
...             return False
...

# Output
>>> print filter(filterEven, myList)
[2, 4, 6, 8, 10, 12, 14]

# A better version of above filter
>>> def filterEven(num):
...     return num % 2 == 0
...

# Output
>>> print filter(filterEven, myList)
[2, 4, 6, 8, 10, 12, 14]

# Using a lambda function
>>> filterEven = lambda num : num % 2 == 0

# Output
>>> print filter(filterEven, myList)
[2, 4, 6, 8, 10, 12, 14]

# Using lambda directly inside 'filter()'
>>> print filter(lambda num : num % 2 == 0, myList)
[2, 4, 6, 8, 10, 12, 14]

How it works

In the above examples, we have a function that returns True if a number is even, False otherwise. So, it will start iterating through the iterable, and pass the item to the function as an argument. If for a particular item, function returns True, it is added to the output list, otherwise skipped. In the end, we get a list of all those items for which function has retuned True. If we were to write an equivalent code to have the same behavior, code would have looked like -

>>> def myFilter(varList):
...     result = []
...     for i in varList:
...             if i % 2 == 0:
...                     result.append(i)
...     return result
...

>>> myList
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

>>> myFilter(myList)
[2, 4, 6, 8, 10, 12, 14]

We take another simple example. In this, we will filter the non-negative numbers from a sequence of numbers. The logic to construct a filter is very simply and should be clear.

# Create the test data
>>> myList = range(-10,11)
>>> myList
[-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Create the filter
>>> def filterNonNegative(num):
...     return num >= 0
...

# Use filter with the list in 'filter()'
>>> print filter(filterNonNegative, myList)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Using lambda inside 'filter()'
>>> print filter(lambda num : num >= 0, myList)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

In the next example, we work on a list of strings, may be a list of names of continents, and filter out the ones who have less than 8 characters in them. Pretty simple, isn’t it?

# List of continents
>>> myList = ['Asia', 'Africa', 'Australia', 'Europe', 'North America', 'South America', 'Antarctica']

# The filter 
>>> def filterSmall(myStr):
...     return len(myStr) < 8
...

# Output
>>> print filter(filterSmall, myList)
['Asia', 'Africa', 'Europe']

# With lambda inside 'filter()'
>>> print filter(lambda myStr : len(myStr) < 8, myList)
['Asia', 'Africa', 'Europe']

With this, we close our discussion on filter() function. It was pretty straight forward to understand and use. We just need a
filter - a function which returns Boolean values and an iterable. As a result, we get a list with items for which filter returned True. Please share your views and feedback in the comments section below and stay tuned for more articles. Thank you.

Friday, 23 December 2016

Bash Scripting - 'while' Loop

Bash 'while' loop - We have been discussing loops in bash scripting and we have covered one of its kinds, for loop, in our last article - Bash Scripting : 'for' Loops. In that article, we saw that, a loop is required in order to repeat a certain task a number of times. In for loop syntax, we created a loop using Initialization, Condition and Increment / Decrement. This article is about another kind of loop statement - while loop, in which we study its syntax and understand it in more details with examples.


Here we go!

The 'While' Loop

In while loop, we have a condition, which is checked after every iteration. As long as the condition stays True, the block of code keeps executing, thus forming a loop. As soon as the condition becomes False, the loop is terminated. Have a look on while loop syntax:

Syntax:

while [ Condition ]
do

    -- Block of Commands --

done

Example:

#!/bin/bash

n=1

while [ $n -le 5 ]
do
  echo "This is Iteration No. $n"
  n=$( expr $n + 1 )
done

In above code, we have n=1 before we enter the loop. When we enter the loop, the condition is checked whether n <= 5, which is True, and we enter the loop. Now, echo statement displays current value of n which is 1. Then, n is incremented by 1 (Please check our article on Arithmetic Operations for more details about expr). When the block ends, we have n=2 and the control goes back to while, where the condition is checked again. Condition follows, echo prints the value of n and increments its value by 1. So, after 5 iterations, we would have echo statement executed 5 times and with n incremented to value 6. At this point, control goes to while, condition is checked and does not follow. Hence the loop exits here. In the end, we have below result in the output.

Result:

This is Iteration No. 1
This is Iteration No. 2
This is Iteration No. 3
This is Iteration No. 4
This is Iteration No. 5

An Infinite 'while' Loop

An infinite while loop is the one, which never ends. For this, we need a condition which is always True. So, for this, you can mention any non-zero number or a non-empty string or a : (or a condition which is always True) as the condition, which is evaluated to True by bash. The condition always being True, the block of code will keep executing infinitely.

Example:

#!/bin/bash

while [ 1 ]
do
   echo "You are in an Infinite Loop. Press CTRL + C to Exit.."
done

OR

#!/bin/bash

while :
do
   echo "You are in an Infinite Loop. Press CTRL + C to Exit.."
done

OR


#!/bin/bash

while [ 5 -eq 5 ]
do
   echo "You are in an Infinite Loop. Press CTRL + C to Exit.."
done

'break' Statement

The break statements are used in the loops to exit from the loop prematurely, based on certain condition (Please take look over our article on Bash Conditionals). If the condition follows, break statement is executed, which then prevents further iterations from happening and exits from loop.

Syntax:

while [ condition ]
do
    -- Some Commands --

    if [ condition ]
    then
        break
    fi

    -- More commands --
done

Example:

#!/bin/bash

while :
do
    echo "Enter a single-digit number :"
    read n

    if [ "$n" -gt 9 ]
    then
        echo "Wrong Entry! Program will terminate now.."
        break
    fi
    
    echo "Good!"
done

'continue' Statement

The continue statement in a loop, when certain condition becomes true, skips all the subsequent statements, coming after it, and continues with the next iteration of the loop. So, when a continue statement is reached, further portion of code is skipped from execution and next iteration is started.

Syntax:

while [ condition ]
do
    -- Some Commands --

    if [ condition ]
    then
        continue
    fi

    -- More commands --
done

Example:

We take an example of printing all even numbers less than 20. In this case, we check whether a number is perfectly divisible by 2, using mod operator. If it is not, its clearly an odd number, so we prefer not to print it and skip further processing. For this, we introduce a continue statement in this condition, to prevent odd numbers from printing.

#!/bin/bash

i=0
while [ $i -le 20 ]
do
    i=$(expr $i + 1)
    j=$(expr $i % 2)

    if [ $j -ne 0 ]
    then
        continue
    fi

    echo "$i"
done

Result:


2
4
6
8
10
12
14
16
18
20

Example : Factorial of a Number

Factorial of a number is nothing but product of all natural numbers less or equal to the particular number. With this logic, we initialize a variable fact with 1. We then keep this value multiplying in each iteration, with the number getting decremented by 1, after every iteration. In the end, we get n * (n-1) * (n-2) * ... * 1 saved in the variable fact, which is the expected result.

#!bin/bash

echo "Enter the Number"
read num
n=$num
fact=1

while [ $num -ge 1 ]
do
    fact=$(expr $fact \* $num)
    num=$(expr $num - 1)
done

echo "Factorial( $n ) = $fact"

Result:

Enter the Number
6
Factorial( 6 ) = 720

With this, we have come to an end of this discussion on bash while loops. In this article, we've learned how while are constructed and how the control flow works. We have also revised how break and continue statements work in while loops. In the next article, we will learn about another loop statement - select loop. Please share your views and feedback in the comments section below and stay tuned. Thank you.

Wednesday, 21 December 2016

Bash Scripting - 'for' Loop

for Loop in Bash - For a programmer, it might necessitate to run a particular block of code a few or many times, repeatedly. To achieve this, we can use of Loop Statements. In bash, there are two types of loops - for loop and while loop. In this article, we will be discussing the former one, see multiple ways of creating a loop along with its examples.

Here we go!


Bash for Loop

A for loop can be created with three components - Initialization, Control Condition, Iteration. With initialization, we initialize a variable to begin the loop. The control condition decides how long the loop will run for. As long as the condition is True, the loop will goon executing and stop as soon as condition becomes False. The iteration controls the variable initialized, by incrementing/decrementing it. So, we can put down the syntax of a for loop as:

Syntax:

for ((Initialization; Control Condition; Iteration))
do

    -- Block of Commands --

done


Example:

#!/bin/bash

for (( i=1; i<=5; i++ ))
do
   echo "This is Iteration No. $i"
done

In the above code, i=1; is the Initialization part, in which we have initialized value of the loop variable i to 1. As long as the condition i<=5; is True, the loop will continue to run. Now that, if we do not increment the value of our loop variable, the loop will never end. So, its value must be updated after every iteration. Hence the last statement - i++ (a shorthand to i = i + 1 in other programming languages).

So, for iteration 1, i=1 which is also <=5, so echo statement will execute. Here, iteration 1 completes and value of i is incremented by 1, which means, i=2. This also follows the condition, hence echo command is executed. This will go on till i=5. At this point, after the code inside is executed, it's value is incremented, meaning now i=6, which doesn't follow the condition and the loop terminates.

Result:

This is Iteration No. 1
This is Iteration No. 2
This is Iteration No. 3
This is Iteration No. 4
This is Iteration No. 5

An Infinite Loop

If we do not mention any condition in the parenthesis, the loop will run forever, hence termed as Infinite Loop. Thus, it is very important to mention all the three statements inside parenthesis to avoid such state, or the subsequent code after the infinite for loop will never execute and your program will never exit. While, you can create an infinite for loop on the terminal as below and in order to exit from it, press Ctrl + C.

Example:

#!/bin/bash

for (( ; ; ))
do
   echo "You are in an Infinite Loop. Press CTRL + C to Exit.."
done

For Loop with Numeric Ranges

With for .. in combination, we can iterate through a sequence. At each iteration, the loop variable takes the value of each item in the sequence, which then can be operated on in the block of code.

Syntax:

for VAR in 1 2 3 .. N
do
    -- Block of Commands --
done

Example:

for i in 1 2 3 4 5
do
    echo "You are in Loop No. $i"
done

Result:

You are in Loop No. 1
You are in Loop No. 2
You are in Loop No. 3
You are in Loop No. 4
You are in Loop No. 5

Instead of writing the whole series of numbers, you can also specify the range in the {START..END} format (END Inclusive). The variable will iterate through this range of numbers with a step value of 1 and the for loop will execute the block of commands for every number in the specified range.

Syntax:

for VAR in {START..END}
do
    -- Block of Commands --
done

Example:

for i in {1..5}
do
    echo "You are in Loop No. $i"
done

Result:

You are in Loop No. 1
You are in Loop No. 2
You are in Loop No. 3
You are in Loop No. 4
You are in Loop No. 5

In the above example, the variable value increments by 1, by default, after each iteration. You can also specify the step count as {START..END..STEP}, which denotes how many elements to skip.

Example:

for i in {0..15..3}
do
    echo "You are in Loop No. $i"
done

Result:

You are in Loop No. 0
You are in Loop No. 3
You are in Loop No. 6
You are in Loop No. 9
You are in Loop No. 12
You are in Loop No. 15

Using seq Command

The seq command is used to generate a sequence of numbers, as we just did in above example. seq command does the same for us, with similar syntax.

'seq' Syntax:

seq END
seq START END
seq START INC END

Let us now create a for loop using seq command.

Example:

#!/bin/bash
for i in $(seq 0 3 15)
do
   echo "You are in Loop No. $i"
done

Result:

You are in Loop No. 0
You are in Loop No. 3
You are in Loop No. 6
You are in Loop No. 9
You are in Loop No. 12
You are in Loop No. 15

'Break'ing the Loop

The break statements are used in for, while and until loops to prematurely exit from the loop. Normally, these statements are used with some condition, so that, the loop is terminated when the condition is satisfied. Check our article on Bash Conditionals for more details.

Syntax:

for VAR in {RANGE}
do
    -- Some Commands --

    if [ condition ]
    then
        break
    fi

    -- More commands --
done

As an example, we would look for a file /etc/init.d/networking in its parent directory if it exists. We iterate through the files in the directory /etc/init.d. As soon as we find a file with name networking, we print Found! and break out from the loop.

#!/bin/bash

for file in /etc/init.d/*
do
    if [ "$file" == "/etc/init.d/networking" ]
    then
        echo "Found!"
        break
    fi
done
echo "Search Complete!"

The above script will go through each and every file in the /etc/init.d directory. As soon as it finds 'networking' file therein, it will print 'Found!' and exit from the loop.

The 'continue' Statement

The continue statement in a loop, when certain condition satisfies, skips all the subsequent statements from executing and continues with the next iteration of the loop.

Syntax:

for VAR in {RANGE}
do
    -- Some Commands --

    if [ condition ]
    then
        continue
    fi

    -- More commands --
done

Example:

for i in {1..20}
do
    j=$(expr $i % 2)

    if [ $j == 0 ]
    then
        continue
    fi

    echo "$i"
done

In above code, we iterate through the numbers from 1 to 20. We check whether a number is completely divisible by 2 (to check if it's even - please read our article on Bash Arithmetic Operations for more details). If it is, continue statement is executed and the control goes to the beginning of the loop, to start next iteration, thus skipping the execution of echo statement. When a number is odd, condition becomes False, continue statement does not execute and echo statement print the number out. In the end, we get all odd numbers on the terminal screen.

Result:

1
3
5
7
9
11
13
15
17
19

Thus, we close our discussion on for loops. We learned to create for loops using the generic method. We also used for loops to iterate through a sequence of numbers. We also learned to use break and continue statements to break the loop prematurely and skip executing of subsequent code, respectively. We will revisit them when we see another kinds of loops in the further articles. Please share your views and feedback in the comment section below and stay tuned. Thanks.