Pages

Create happy new year message on the linux terminal

We can use the following python script to generate happy new year using the diamond symbol on the linux terminal.


Save the file as happynewyear.py and execute it using



You should see the output as shown below.It can be stopped by pressing any key to exit,note that it will exit only after the display of the message.




Using the function list_is_first for finding if a node is the first node in kernel linked list

In the post Create a linked list in linux kernel 5.3 we learnt the details of creating a linked list in a linux kernel. There are number of functions available to help us manage the linked list easily once created, all of them defined in the file "linux/list.h" Let us look at the function list_is_first, which is used to find if the given node is the first node of the linked list or not.



Where list is the node which is to be checked if the it is the first node of the list or not.
head is the head node of the list.

The function list_is_first returns true of the node being checked is the head node, else it will return false. In the example below we will create a linked list with 3 nodes , one,two and three. One being the first node of the list and three being the last node. The nodes two and one both are passed to the list_is_first function and in the output we will see that when the node two is passed as the entry to be checked, the function returns false



When the node one is passed it returns true because node one is the first node of the list.

The full module code is shown below.



Save the file as linked_list_is_first.c . Use the following makefile to compile the module .



Compile and insert the module using



To see the output run the command

The output should look as shown below



We can see that for node two, the message printed is Not the first node where as for node one the function returns true and prints the message It is first node

Using the function list_del for deleting a node from the kernel linked list

In the post Create a linked list in linux kernel 5.3 we learnt the details of creating a linked list in a linux kernel. There are number of functions available to help us manage the linked list easily once created, all of them defined in the file "linux/list.h" Let us look at the function list_del, which is used for deleting one of the nodes of the list. The syntax for calling the function is



Where entry is the pointer to the list_head data in the node which we want to delete. In the sample code below we first create a linked list with three nodes ,one,two and three and print each element of the list. Then we use the function list_del to delete one of the entries.



When we print the elements of the list again, we will see in the output that only two nodes are printed and the node that was deleted is no longer printed, hence making it clear that the node did get deleted.

The full module code is shown below.



Save the file as linked_list_del.c . Use the following makefile to compile the module .



Compile and insert the module using



To see the output run the command

The output should look as shown below



We can see that as we passed the first node for deletion, only 20 and 30 are printed as part of the new list.

Python script to create a matrix on the linux terminal

Use the following script to create a matrix like background on the terminal. Save the script as matrix.py, open the terminal and run the script using

You should see an output as shown in the video below, hit any key to stop the matrix.

Thanks to the following link for the code of kbhit


Create a linked list in linux kernel 5.3

In the post "Creating linked list in linux Kernel using the list" we saw how to create a linked list in a kernel. Find below the updated code which works is tested in kernel 5.3



Save the file as linked_list.c and use the following makefile to compile the code

Compile and insert the module using the following commands



Run the command dmesg to see the linked list values being printed.

Create a welcome message on the linux terminal using python

Use the following code to create a welcome message,"Have a good day" on the linux terminal using python and ncurses library.



Save the above file, let us say as hagd.py. and execute it in the linux terminal using the command.



You should see the message have a good day appearing on the screen as shown below.



You can call this as a part of .bashrc to display the message every time terminal is launched

Create a count down timer on terminal using curses library in python

We can use the following code to create a countdown timer of 10 seconds on the terminal. It uses the curses library in python to create the timer.

Note that to be able to run the code you will need to have curses library for python installed.

You can refer to the following site to learn the installation How To Install ncurses Library on a Linux



Save the code as timer.py . Execute the code in the terminal using the command


You should see an output that looks as shown below,with the count changing every second.







Unable to save path to file : matlab

When executing the command, savepath in matlab in linux if you encounter the error.



It is because the pathdef.m file does not have the permissions required. To verify this execute the command ls -l /usr/local/MATLAB/R2017a/toolbox/local/pathdef.m

We should see an output which displays the following permissions, only read permissions.



To workaround the error, execute the command



The error should not appear anymore when you run the command savepath.

Creating proc read write function in linux kernel 5.3

In the post "Creating proc read and write entry (linux kernel 4.6)" we saw the details of creating a proc entry in linux kernel version 4.6 .The following is the code to create a simple proc entry to which we can write as well as read from it works with the linux kernel 5.3.

The only change in this as compared to the previous post for version 4.6 is the use of function "simple_read_from_buffer" which copies data from buffer, which is memory in the kernel space to user space. It returns the size of the data of bytes of data to read. The return type of function written to read the proc content is changed to ssize_t from int. Thus making the above two changes the read_proc function now looks like this



We use the temp variable to keep track of how much data is being transferred to the user space. count holds the number of bytes of data written by the write function of the proc entry. For writing data to proc entry we use the function and simple_write_to_buffer which write a message from user space to memory buffer in kernel space which will hold the data in the proc entry.The function meant to write data to proc entry is meant to return the size_t, the size of data written to the proc entry.



Thus the full code for creating the proc entry is



Save the file as proc_rw_5_3.c .

The make file required to compile the above code is



Compile and test the module using the following steps.




Raspberry pi : Can not currently show the desktop

Raspberry pi can be connected to a laptop monitor using a network cable. The GUI can be accessed by use of real vnc server. It might pop the following error when connecting



This is generally because the resolution of raspberry is set wrong. To change the resolution and access it using VNC we can follow the steps given below.

Connect to pi using putty and launch the raspberry pi config using the command



Go to the advanced options




Then go to Resolution options



Choose the maximum resolution.




Click on ok and then finish . It will prompt for a reboot, select yes.Once pi reboots , VNC should be able to connect successfully.

Creating read only proc entry in linux 5.3

In the post "Creating proc read and write entry (linux kernel 4.6)" we saw the details of creating a proc entry in linux kernel version 4.6 .The following is the code to create a simple proc entry which can only be read from which works in the linux kernel 5.3.

The only change in this as compared to the previous post for version 4.6 is the use of function "simple_read_from_buffer" which copies data from buffer, which is memory in the kernel space to user space. It returns the size of the data of bytes of data to read. The return type of function written to read the proc content is changed to ssize_t from int. Thus making the above two changes the read_proc function now looks like this



Thus the full code for creating the proc entry is



The make file required to compile the above code is

Compile and test the module using the following steps.




scripts/link-vmlinux.sh: line 45 : killed make error 137

Compiling custom kernel in debian threw the following error.



There was no point looking for errors in the script file as it is standard script of the kernel source and chances of it being wrong is very remote, so the issue had to be in the system where it was being compiled. A look at the system log using dmesg showed the issue was



As the compilation was being done in a virtual machine, the default value of RAM was set to 1 GB, which was not enough. So increasing the RAM to 4GB and recompiling the kernel was all that was needed.

Inserting an Emoji in gedit

gedit is a popular text editor in linux. Traditionally text editors lacked features like emoticons, but gedit now has the option to add smileys to the text being typed in it.

Open the gedit, right click and Select the insert emoji



Select the emoji that has to be inserted by clicking on the emoticon


The selected emoji will appear with the text.



Libreoffice change theme of the sheet

In Libreoffice sheets can have a theme that will change the look of sheets i.e make the text and background appear in different pattern. 
Let us say we have the following sheet, which is the normal pattern.


 


To change the theme, click on Format->Spreadsheet Theme 






Select any of the themes that you want For eg PC Nostalgia looks like this. 





Remove duplicate entries from a column in libreoffice.

Very often while dealing with data in Excel files, we might have rows of data that might have got repeated several times. If we want to remove these duplicate rows we can follow the following steps.

Let us say we have the following excel file 





We can see that the row of names has the same name repeated more than once. If we want to view only those rows which are unique then first select the column from which the duplicate entries have to be removed from











Now click data->More filters->standard filter 




In this make sure none of the columns is chosen in the filed name options, now click on the small triangle labelled options at the left hand side to open more options.







From this tick the option "No duplications" Click OK. 



Now we should see that only the unique rows would be listed .














ValueError: Masked arrays must be 1-D

The following code was written in python to generate a scatter plot of the test data after splitting it into test and train data. The data being read from a sample file was converted to matrix to be able to use the data for the the linear regression algorithm.



But using the same data for the scatter plot generated the following error.



This is because we are passing X_test and Y_test and matrices to scatter plot but which actually are one dimensional arrays. Thus to get around the problem we can cast the data into an numpy array.



The above modification should stop the error from being thrown. The modified code will be.


ValueError: Found input variables with inconsistent numbers of samples

The following code is written in python to run the liner regression algorithm on a given set of data. Two columns were chosen, namely X1 and and Y1 were chosen on which linear regression was to be performed. The code used for the same was



On executing the above code the following error was encountered.



The above error generally comes when the X and Y have different number of samples. But in this case the error appeared even though the number of samples in X_train and Y_train were same as shown in the output.



The problem with the code is that we converted the given data into numpy arrays, but it was required to convert them to numpy matrix to be able to pass it to fit. Thus we modify the reading of data to



While doing the train test split we will need to transpose the matrix



Passing these test data to the fit function should not throw the value error encountered previously. The modified code thus would be