Search

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.

import curses as cur
from curses import wrapper
import locale
import sys, termios, atexit
from select import select
import curses as cur
from curses import wrapper
import time
import datetime
import random
# save the terminal settings
fd = sys.stdin.fileno()
new_term = termios.tcgetattr(fd)
old_term = termios.tcgetattr(fd)
# new terminal setting unbuffered
new_term[3] = (new_term[3] & ~termios.ICANON & ~termios.ECHO)
# switch to normal terminal
def set_normal_term():
termios.tcsetattr(fd, termios.TCSAFLUSH, old_term)
# switch to unbuffered terminal
def set_curses_term():
termios.tcsetattr(fd, termios.TCSAFLUSH, new_term)
def putch(ch):
sys.stdout.write(ch)
def getch():
return sys.stdin.read(1)
def getche():
ch = getch()
putch(ch)
return ch
def kbhit():
dr,dw,de = select([sys.stdin], [], [], 0)
return dr
def horizontal(row,cols,num):
for x in range(0,num):
stdscr.addch(row,cols+x,cur.ACS_DIAMOND,cur.color_pair(1))
def vertical(row,cols,num):
for x in range(0,num):
stdscr.addch(row+x,cols,cur.ACS_DIAMOND,cur.color_pair(1))
def forDiagonal(row,cols,num):
for x in range(0,num):
stdscr.addch(row+x,cols+x,cur.ACS_DIAMOND,cur.color_pair(1))
def forUpDiagonal(row,cols,num):
for x in range(0,num):
stdscr.addch(row-x,cols+x,cur.ACS_DIAMOND,cur.color_pair(1))
def backDiagonal(row,cols,num):
for x in range(0,num):
stdscr.addch(row-x,cols-x,cur.ACS_DIAMOND,cur.color_pair(1))
def h(beginRow,beginCols):
vertical(beginRow-2,beginCols,5)
horizontal(beginRow,beginCols,4)
vertical(beginRow-2,beginCols+4,5)
def a(beginRow,beginCols):
forUpDiagonal(beginRow+2,beginCols,5)
backDiagonal(beginRow+2,beginCols+8,5)
stdscr.addch(beginRow+1,beginCols+4,cur.ACS_DIAMOND,cur.color_pair(1))
def n(beginRow,beginCols):
vertical(beginRow-2,beginCols,5)
backDiagonal(beginRow+2,beginCols+4,5)
vertical(beginRow-2,beginCols+4,5)
def p(beginRow,beginCols):
horizontal(beginRow-2,beginCols,4)
vertical(beginRow-2,beginCols,5)
horizontal(beginRow,beginCols,5)
vertical(beginRow-2,beginCols+4,2)
def y(beginRow,beginCols):
backDiagonal(beginRow,beginCols+1,3)
forUpDiagonal(beginRow,beginCols+3,3)
vertical(beginRow+1,beginCols+2,2)
def e(beginRow,beginCols):
vertical(beginRow-2,beginCols,5)
horizontal(beginRow-2,beginCols,5)
horizontal(beginRow,beginCols,5)
horizontal(beginRow+2,beginCols,5)
def w(beginRow,beginCols):
backDiagonal(beginRow+2,beginCols+1,5)
forUpDiagonal(beginRow+2,beginCols+1,3)
backDiagonal(beginRow+2,beginCols+5,3)
forUpDiagonal(beginRow+2,beginCols+5,5)
def r(beginRow,beginCols):
horizontal(beginRow-2,beginCols,4)
vertical(beginRow-2,beginCols,5)
horizontal(beginRow,beginCols,5)
vertical(beginRow-2,beginCols+4,2)
backDiagonal(beginRow+2,beginCols+4,3)
if __name__ == '__main__':
atexit.register(set_normal_term)
set_curses_term()
stdscr = cur.initscr()
cur.start_color()
cur.noecho()
cur.cbreak()
locale.setlocale(locale.LC_ALL, '')
stdscr = cur.initscr()
cur.start_color()
cur.curs_set(0)
stdscr.keypad(True)
cur.init_pair(1,cur.COLOR_GREEN, cur.COLOR_BLACK)
cols=lines=0
cols=(cur.COLS)
startRow=(cur.LINES)//2
startCols=2
brow=4
bcols=startCols
while 1:
brow=4
bcols=startCols
stdscr.clear()
if kbhit():
ch = getch()
break
h(brow,bcols)
time.sleep(1)
stdscr.refresh()
bcols=bcols+5
a(brow,bcols)
time.sleep(1)
stdscr.refresh()
bcols=bcols+9
#N
#n()
p(brow,bcols)
time.sleep(1)
stdscr.refresh()
bcols=bcols+6
p(brow,bcols)
time.sleep(1)
stdscr.refresh()
bcols=bcols+7
y(brow,bcols)
time.sleep(1)
stdscr.refresh()
bcols=bcols+9
n(brow,bcols)
time.sleep(1)
stdscr.refresh()
bcols=bcols+6
time.sleep(1)
stdscr.refresh()
e(brow,bcols)
time.sleep(1)
stdscr.refresh()
bcols=bcols+9
w(brow,bcols)
time.sleep(1)
stdscr.refresh()
brow=10
bcols=14
y(brow,bcols)
time.sleep(1)
stdscr.refresh()
bcols=bcols+7
e(brow,bcols)
time.sleep(1)
stdscr.refresh()
bcols=bcols+6
a(brow,bcols)
time.sleep(1)
stdscr.refresh()
bcols=bcols+10
r(brow,bcols)
time.sleep(1)
stdscr.refresh()
stdscr.clear()
cur.nocbreak()
stdscr.keypad(False)
cur.echo()
cur.curs_set(1)
cur.endwin()

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.

/*Code to show usage of list_is_first . Tested in kernel 5.3*/
#include<linux/kernel.h>
#include<linux/module.h>
#include<linux/list.h>
#include<linux/slab.h>
struct k_list {
struct list_head test_list;
int temp;
};
int create_list_init(void) {
struct k_list *one,*two,*three,*entry;
struct list_head test_head;
struct list_head *ptr;
int i=0;
one=kmalloc(sizeof(struct k_list *),GFP_KERNEL);
two=kmalloc(sizeof(struct k_list *),GFP_KERNEL);
three=kmalloc(sizeof(struct k_list *),GFP_KERNEL);
one->temp=10;
two->temp=20;
three->temp=30;
INIT_LIST_HEAD(&test_head);
list_add_tail(&one->test_list,&test_head);
list_add_tail(&two->test_list,&test_head);
list_add_tail(&three->test_list,&test_head);
ptr=&(one->test_list);
printk(KERN_INFO " Linked list is \n\n");
list_for_each(ptr,&test_head){
entry=list_entry(ptr,struct k_list,test_list);
i=i+1;
printk(KERN_INFO " %d-> ",entry->temp);
}
printk(KERN_INFO " \n\n");
i=0;
printk(KERN_INFO " Checking node %d \n \n ",two->temp);
if(list_is_first(&two->test_list,&test_head))
printk(KERN_INFO " It is first node \n \n ");
else
printk(KERN_INFO "Not the first node \n \n ");
printk(KERN_INFO " Checking node %d \n \n ",one->temp);
if(list_is_first(&one->test_list,&test_head))
printk(KERN_INFO " It is first node \n \n ");
else
printk(KERN_INFO "Not the first node \n \n ");
printk(KERN_INFO " Done.... \n \n\n ");
return 0;
}
void create_list_exit(void) {
printk(KERN_INFO "\n exiting \n \n\n ");
}
module_init(create_list_init);
module_exit(create_list_exit);


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.

/*Code to show usage of list_del . Tested in kernel 5.3*/
#include<linux/kernel.h>
#include<linux/module.h>
#include<linux/list.h>
#include<linux/slab.h>
struct k_list {
struct list_head test_list;
int temp;
};
int create_list_init(void) {
struct k_list *one,*two,*three,*entry;
struct list_head test_head;
struct list_head *ptr;
int i=0;
one=kmalloc(sizeof(struct k_list *),GFP_KERNEL);
two=kmalloc(sizeof(struct k_list *),GFP_KERNEL);
three=kmalloc(sizeof(struct k_list *),GFP_KERNEL);
one->temp=10;
two->temp=20;
three->temp=30;
INIT_LIST_HEAD(&test_head);
list_add_tail(&one->test_list,&test_head);
list_add_tail(&two->test_list,&test_head);
list_add_tail(&three->test_list,&test_head);
ptr=&(one->test_list);
list_for_each(ptr,&test_head){
entry=list_entry(ptr,struct k_list,test_list);
i=i+1;
printk(KERN_INFO "\n Entry %d %d \n \n\n ", i,entry->temp);
}
i=0;
printk(KERN_INFO " Deleting one entry\n \n\n ");
list_del(&(one->test_list));
list_for_each(ptr,&test_head){
entry=list_entry(ptr,struct k_list,test_list);
i=i+1;
printk(KERN_INFO "\n Entry %d %d \n \n\n ", i,entry->temp);
}
printk(KERN_INFO "\n Done.... \n \n\n ");
return 0;
}
void create_list_exit(void) {
return 0 ;
}
module_init(create_list_init);
module_exit(create_list_exit);


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.
import sys, termios, atexit
from select import select
import curses as cur
from curses import wrapper
import time
import datetime
import random
# save the terminal settings
fd = sys.stdin.fileno()
new_term = termios.tcgetattr(fd)
old_term = termios.tcgetattr(fd)
# new terminal setting unbuffered
new_term[3] = (new_term[3] & ~termios.ICANON & ~termios.ECHO)
# switch to normal terminal
def set_normal_term():
termios.tcsetattr(fd, termios.TCSAFLUSH, old_term)
# switch to unbuffered terminal
def set_curses_term():
termios.tcsetattr(fd, termios.TCSAFLUSH, new_term)
def putch(ch):
sys.stdout.write(ch)
def getch():
return sys.stdin.read(1)
def getche():
ch = getch()
putch(ch)
return ch
def kbhit():
dr,dw,de = select([sys.stdin], [], [], 0)
return dr
if __name__ == '__main__':
atexit.register(set_normal_term)
set_curses_term()
stdscr = cur.initscr()
cur.start_color()
cur.noecho()
cur.cbreak()
cur.curs_set(0)
stdscr.keypad(True)
cur.init_pair(1, cur.COLOR_GREEN, cur.COLOR_BLACK)
x=datetime.datetime.now()
cols=lines=0
cols=(cur.COLS)
lines=(cur.LINES)
count=10
x=random.choice([0,1])
stdscr.nodelay(True)
while 1:
if kbhit():
ch = getch()
break
for x in range(0,cols-1):
for y in range(0,lines):
data=random.choice([0,1])
stdscr.addstr(y,x,str(data),cur.color_pair(1))
stdscr.refresh()
print("done")
cur.nocbreak()
stdscr.keypad(False)
cur.echo()
cur.curs_set(1)
cur.endwin()
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.