Search

2021 on Linux Terminal

Here is a bash script which can be used to create the text 2021 on the terminal using any characher of your choice.



Copy the script and save it as 2021.sh. Open a terminal and type

It will prompt for the characher which should be used. We have entered "*" as the characher, and you should see the output as shown below.

Extracting images from a PDF

To extract images from a pdf document we can use the command



The syntax to use the command is



The above command will extract the images from page number 1 to page number 4 and the name of the images will be image-000.ppm ,image-001.ppm etc. The default format is ppm if the format of the file is to be changed, png option can be passed.



The above command will extract the images in the png format.

Package inputenc Error: Unicode character Σ (U+03A3) (inputenc) not set up for use with LaTeX.

Running Pdflatex with math symbols not put inside proper math block can throw this error



In the above message the sigma symbol is being used outside the math block as shown below.

The solution is to put the equation into the math block, so surround the text with $ symbols and use the sum command which is the latex for sigma.



The error should not apper now on the usage of pdflatex .

Taking a screenshot from command line in gnome

The command

Allows us to cpature screenshot of our screens from the command line itself and it is a pretty flexible commnad too . If just the command is exectued with no arguments, the whole screen is captured and the screenshot gets saved in the folder, with the name having the date and time when it was cpatured.

d

If we want to save the file to with a specific name we can pass the command line option -f , we can also define the path along with the name to change the folder into which it will get saved.



The above command will save the screenshot in folder Docments under the home folder with the name my_screenshot.

The option -c on the other hand, will not save the file at all, instead it copies the screenshot to the clipboard, which we can paste directly into a document or presentation etc.

Instea of capturing the full screen, if we need to capture a specific area on the screen we can pass the optinon -a



This will allow us to choose the area that has to be captured using our mouse.The -a option combined with -f option will allow us to save the chosen area in the required fodler with the required name.

We can also cause the screenshot to be cpatured after a certain delay from the time the command is executed using the option -d along with the delay in seconds.



The above command will cause a screenshot to be captured after a delay of 10 seconds. The -d and -a option can not be passed at the same time, but -f can be passed with -d to save the screenshot in the required destination.

The current active window can be captured using the option -w, and the while capturing the active window, if we do not want the border of the window it can be removed using the option -B.

Disable gnome extenstions from command line, to workaround a login loop

Some gnome extenstions can cause ubuntu to go into a login loop, causing it to keep showing the login screen every time you login. The only workaround is to disable the extension which is causing the login loop from a command line login. From the GUI screen type



Login by providing the correct credentials.

To see all a list of all the extentions, run the command

To disable the extensions, run the command.

Try logging in after disabling one extenstion at a time, and you should be able to figure out the one causing the login issue. Keep that disabled untill you can find an upgrade which resolves the issue.

installation doesn't have the spyder‑kernel module or the right version of it installed

 

A recent update of python, broke the spyder kernel and it failed to launch with the following error.



The workaround that worked was to make spyder use its default kernel, which can be done by going to Tools->Preferences->Python Interpreter
Click on apply and then ok. Launch spyder again and the error should not appear.

Show line numbers in Gedit


Gedit is a popular text editor in Linux. If we want every line in the text file to be shown with a line number automatically, we can do so by enabling it in the preferences.

Open the preferences as shown below.



On the view tab, select the Display line numbers and close the preferences window.



Now as you type in the file, each line will be shown with a line number.



But note that this is just to display and the line numbers are not part of the file that gets saved.

Anaconda downgrade tensorflow verrsion


Some times after an update the code written on a previous version might not work as is. For example after update to tensorflow 2.1 a lot of previous codes broke. A quick fix would be to downgrade the version of tensorflow to the one which we are sure it works for. To downgrade the tensorflow in anaconda, we can do the following

Launch anaconda and click on the environments on the left tab and select the environment on which you want to work and search for the package by typing in the text box as shown below.





Click on the green tick mark and then move down to the option "Mark for specific version of installation" and choose the version that is required , click on apply .




Anacdonda will solve the package dependencies and then the following window should appear.




Click on apply that should take care of changing the version of the package.

Creating IOCTL linux kernel 5.3


Code for creating IOCTL command in linux kernel 5.3 . The details of creation and usage of kerenl is given in the post here

Creating a copy of a spreadsheet having only unique rows from a given sheet


Let us say we have the following sheet which has the repeated values highlighted in bold. The following script can be used if we want to create a new sheet having rows with the repeated values of the column ID removed.



The value of file1 points to the original file and copy is the path to the new file to be created.



Save the file as create_sheet_unique_rows.py and execute it.



The new file created should look as below.


Copy rows from one spreadsheet to new one based on columns in a spreadsheet

If we want to copy  the rows from one spreadsheet to a new file based on values in another file, for example we have the files listed below.


                   
                                                File1                                                                                File2



    If we want to create a new file which has the rows of File1 only for those ids which are present in file2 we can use the following pyoo script.

Steps for installation of pyoo has been given here.

Before executing the script, we have to ensure that libreoffice is listening to the pyoo by running the following command


In the script below, file1points to the file with source data ,file2 points to the file which has the reference values based on which we want to copy and copy points to the file which will be created. Update the paths respectively.



Save the script as create_copy.py and execute it.



We should see the following file by the name given in the copy path.



Librecalc automatic grading of columns

We can use the following Pyoo based python script to automatically assign grades based on CGPA values in a column.
For example, consider the following file with CGPAs listed in the column B



If we want to assign grades based on  the CGPA values, we can use the script attached.



The script needs Pyoo to be installed, the steps for the same can be seen here .

Please note that to be able to run the script the following command needs to be run in the terminal,

  
 






If the script is saved as column_grade.py run the script from the terminal.



The file should get updated and look as below.


Python Pyoo based script to find the unique elements in a librecalc column

The following is pyoo based python script will be able to list all the unique values in a given column of a librecalc. For example if we have the following file


If we want to find all the unique terms in the column num, just run the following script. Ensure that the path variable is initialized to the full path pointing to the librecalc file.  The script requires pyoo installed, the steps for which are listed here.

Before running the script, make sure to run the following command.







Saved the script as unique_items.py, and execute it. For the above example file shown, we get the following output.

Python based Pyoo script to add all elements of a column in librecalc


The following is a script in pyoo, to add all the numbers in a column by taking the column header as an input from the user.
For example, let us say we have a file a shown below.


The scripts calculates the sum of numbers in the column whose header is mentioned by the user, for example "num" in this case, and updates the sheet with the sum in the row after the numbers and also puts the formula for the same in the cell.

To be able to run pyoo, the package has to be installed. The steps for installation are given here

 


Save the script as pyoo_column_sum.py. Make sure that the variable "path" has been intialized to point to the file where the librecalc file is located.
Run the following command before executing the script.



Note that because of the headless being added at the end of the above command, the window of librecalc will not be launched, the headless option can be removed if the window is required.

Now we can run the script (Note that the script has been tested on python3.6).




 
We can note that hte sheet has got updated with the sum of the numbers in the column and the required formula also has been updated in the sheet.

from base import Element, Css, Payload, UnoBaseFeature, UnoBaseField ImportError: cannot import name 'Element'

While trying to use pyoo if the importing pyoo



throws the following error



Two possible workarounds for this are

install python3-uno if its already not installed



If after installing this the error still persist, it could be because another package by the name uno, not related to libreoffice has been installed which is causing a conflict. So uninstall the package and try it.




Create a 300DPI image using gimp

To be able to print high resolution graphic images its preferred to have the pictures of 300 dpi. We can create the 300dpi images using GIMP .

Click on



The following image will pop.



Click on the advance options. To open the following window.



Enter 300 in the X and Y resolution with pixels/in chosen and click on "OK".

The image created will be of 300X300 dpi.

Creating a misc characther device and reading from it

Linux has misc devices which meant for small device drivers. According to the article written by Alessandro Rubini Miscellaneous Character Drivers :

Sometimes people need to write “small” device drivers, to support custom hacks—either hardware or software ones. To this end, as well as to host some real drivers, the Linux kernel exports an interface to allow modules to register their own small drivers. The misc driver was designed for this purpose.

miscdevice is declared as the structure in miscdevice.h



For this example we will just use the first three fields.



The name of the device can be any name that the user wants. The minor number can be statically assigned by the user , if it has to be dynamically assigned by the user , the value has to be set to `MISC_DYNAMIC_MINOR.

Thus our device structure will look as below.



To register a misc device the function used is



We can create a characther string which will hold the data for now which will be sent out everytime the device is read. Thus we create a function to fill the data for the device.



The init function for the device will have to register the device. Unlike a charachter device, we dont need to get a major number for a misc device as the default major number for misc devices is 10. To see the minor number which will be assigned dynamically we will add a print statement to get the minor number.



We will create two file operations for this, one to open the device and one to read from the device. Thus the file operations structure will be



The open function will fill data into the device,which we can read using the read function.



The read function will copy the contents of the device to the userspace.



Thus the full code misc device will be



Save the above code as misc_device_read.c

Use the following makefile to compile the code.



Compile and insert the code using



To see the minor number allocated to the device, use dmesg



We can also view the minor number and find if the device got registered succssfully by looking into the file /proc/misc



The device will automatically get listed under /dev which we can view using the ls command.



We can see that the major number is 10 and the minor number is 55.

To read the device, we can use the cat command.




Using average and averagea in libreoffice

If we want to find the average of values in a column in libreoffice, and the column has mix of numbers and characters we can use the function "AVERAGE" which takes input the range of cells ,but the drawback is that the function does not count the cells that have characters.

Let us say we have the following file and the column labelled "data" has a mix of numbers and characters.



Using the function "AVERAGE" we will get the average of 1,2,3,4 as 2.5,which is (1+2+3+4)/4. Though there were 2 cells having characters, the function did not consider them and gave an average as if it was for 4 cells.



On the other hand, if we wanted to calculate the average as if the two character cells were 0, and take the number of cells to be 6. We can use the function "AVERAGEA



This gives the output of 1.66 which is nothing but 10/6, thus considering all the 6 cells.




copy string in kernel using kstrcpy

Creating copy of existing strings would require allocation of new memory and then assign the old string to new one. The multiple steps required to create a string copy can be done with the help of a single function in the kernel.



Ref: Kernel API

In the example below, we have created a proc entry to show the usage of the function. A call to read the proc entry creates a copy of the existing string in temp allcates it to the string called copy which we send as output of the proc entry.



Save the file as proc_read_copy.c and compile it using the following makefile.



Compile and load the module into kernel using.



To see the output, read the proc entry created by the module.




Example of using getnstimeofday in linux kernel

The linux kernel provides a number of interfaces to manage time. getnstimeofday is one of them, which gives the time in seconds and nanoseconds . The function is implemented in "timekeeping32.h" and returns a structure of the type timespec which has two members.



To print the time, we only need to print the values of tv_sec and tv_nsec which gets filled by the call to the function getnstimeofday.

In the following example code, we have created a proc entry called gettime, which prints out the values of seconds and nanoseconds when read.



Save the above code as proc_read_gettimeofday.c and compile the code using the following makefile.



Compile and insert the module using



To see the output, just read the proc entry gettime, using the cat command.




Using get_options to read a range of integer arguments from a function in linux kernel.

In the post , we saw how to use read integers using the get_option, if we want to read a range of integers we can use get_options.



Ref: Kernel API

To read the numbers that are passed,as it is a range we will use an array into which the numbers can be filled. The array will have to be declared with the appropriate size.

The array will be passed to get_options which will fill the array with the elements that belong the range that was passed as shown in the function below.



We can pass a range of integers seperated by a "-" , which will be used to fill the array with the elements.

The following is the full code that passes range of numbers to the getargs function shown above,which in turn prints out all the numbers in the range.



Save the file as readOptions.c and compile it using the following makefile.



Compile and insert the code using



To view the output, run the command dmesg.


Parsing integer argument to functions in linux kernel

Kernel API provides functions to help making parsing of integer arguments being passed to functions easier. One of the functions is



Ref: Kernel API

To understand what has been passed as the argument we need to look at the return value of the function.

To understand the working a function is implemented as an example which tests for all the 4 possible return values and prints the option received.



In the function above the argument is received in *str, which is passed to the get_option function which, which in turn assigns the values to pint from the str.

To see the function in action, we can call the function in the init function with different arguments as shown in the example below.



In the code, we have called the function "getargs" with no arguments,one argument,argument and a comma and specified a range.

To compile code,save it as readOptions.c use the following makefile



To comple and insert the code.



To see the output run the command dmesg



In the above output we can see that, the first call to getargs did not have any arguments in it, hence we see the output as no arguments. The calls after that print out exactly as they are passed.


Creating a FIFO in linux kernel

FIFO is a first in first out data structure that which can be used to hold any kind of data. Linux kernel provides useful APIs to make creation and use of FIFO data structure easy.

The first step in creation of FIFO is to define the FIFO required with the type of data it will be holding using



Once the FIFO has been defined we can use a number of functions available to add,remove, check the FIFO. To put data into the FIFO



To take the date out of the FIFO



Just to see the above functions in use we will create a proc entry which will output one data from a FIFO that gets created as soon as the module is inserted into the kernel.

To create a module with 6 char entries



An array of values to be added to the FIFO



To add the entries into the FIFO when the module gets inserted into the kernel, the init function needs to call the kfifo_put function



Once the fifo has been created, we can extract one data at a time using the function kfifo_get . To ensure that only one data is read from the FIFO each time we use the cat command on the proc entry extra checks and flags are needed



The function kfifo_is_empty returns true when the fifo is empty else it returns false.

The full code for creation of the proc entry called "fifo" which will output one data value at a time is:



If the file is saved as proc_read_fifo.c, it can be compiled using



To test the code, once the compile and insert the code



Read the proc entry using the cat command

The output shows that every call to read the entry removes one item from the fifo and when all entries are removed "FIFO empty" message gets printed.

Using sched_clock to get kernel uptime

In this post we will see how we can get the information of number of nanoseconds the kernel has been working from and how to print it. We will create a proc entry which when read will print the number of nano seconds from which the kernel has been up for.

The function that is available in kernel to get information about the kernel uptime in nano seconds is sched_clock. sched_clock for x86 is defined as a function in the file "arch/x86/tsc.c", which basically overwrites a default version of the function.

According to the linux documentation : "This function shall return the number of nanoseconds since the system was started. An architecture may or may not provide an implementation of sched_clock() on its own. If a local implementation is not provided, the system jiffy counter will be used as sched_clock()."

To use the function in a module we will need the header file "linux/sched/clock.h"

The function is called with no arguments and returns a "unsigned long long".

In the code below a proc entry is created that will print out the output of sched_clock by converting it to the string.



The details of proc entry and its creation have been discussed in the posts
Creating read only proc entry in linux 5.3

Creating proc read write function in linux kernel 5.3

. The following is the full code for creating the proc entry to display the system up time in nanoseconds



Assuming the above file is saved as "proc_read_time.c". Here is the makefile that can be used to compile the code.



Once the code gets compiled successfully, insert the module using

To test the code , read the proce entry that was created using the cat command

The number shown is the number of nano seconds from which the kernel has been running.


Create a double linked list using hlist

To create a double linked list with a seperate head node ,in the linux kernel, we can use the pre existing functions and structures defined in the file types.h. The doubly linked list is formed of two nodes, one is the head node



The second one are the nodes used to form the linked list.



The functions and macros needed to create and use the nodes are defined in the file list.h. To initialize the the list we use the macro



Where ptr should be of the type hlist_head. This points to the head of the double linked list. As we can see this structure has only one pointer of the type hlist_node, thus the head node will point to the first node of double linked list that we want to create.

To add a node to the list we can use the function



The function has been implemented in the file list.h



The function adds the node "n" at the head of the double linked list pointed to by the head node h. It does this by

Allocate the first node of the list to a temporary node : *first = h->first Make the temporary node as the next node of the new node: n->next = first; if the list was not an empty list first will not be null: if(first) allocate the pprev pointer to the address of the node itself: first->pprev = &n->next; Update the value of head node pointer to n : WRITE_ONCE(h->first, n);

We can iterate over the list using the function



To test these functions we will create a structure



In the init function we create the required pointers.



Allocate memory for the k_list structures.



Assign values to the integer in the structure.

Initialize the hlist_head



Add the nodes at the head, so every new node gets added at the head with the other nodes moving away from the head.



The list has been formed, so now we can iterate through the list



In the code we have created and displayed the double linked list in the init funtion, thus to test the code we just need to insert the code into the kernel.

This the full code



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



Make and Insert the code using

To see the output execute the message dmesg , we should see the following output.



Thus we can see each node got inserted at the head so the last node inserted which was "30" is the head node.