Learn the basics of the ZSH shell

Objective

Install ZSH with Oh My ZSH and learn the basic features.

Distributions

ZSH is available in the repositories of nearly every distribution.

Requirements

A working Linux install with root privileges.

Difficulty

Easy

Conventions

  • # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ – requires given linux commands to be executed as a regular non-privileged user

Introduction

Bash isn’t bad. It gets the job done just fine, but have you ever considered what it’d be like if Bash had some extra features to make it more convenient to work with? That’s more-or-less what ZSH is.

It includes all of the features that you’d expect from Bash, but it also has some really nice additions to make your life easier. Actually, you’ll be amazed at how much easier they make working in the command line.

Read more

prime numbers python script fine result

Function to check for a prime number with python

Below is a simple function to check for a prime number. The function is_prime_number() returns False if the number supplied is less than 2 and if the number is equally divisible with some other number different than 1 and itself. If none of the previous conditions apply the function will return True. The below python script will let user to decide how many numbers needs to be check to see whether the number is prime number:

#!/usr/bin/env python

prime_numbers = 0

def is_prime_number(x):
    if x >= 2:
        for y in range(2,x):
            if not ( x % y ):
                return False
    else:
	return False
    return True
	        

for i in range(int(raw_input("How many numbers you wish to check: "))):
    if is_prime_number(i):
        prime_numbers += 1
        print i

print "We found " + str(prime_numbers) + " prime numbers."

Read more

How to Play Audio With VLC In Python

Objective

Play audio with VLC in Python.

Distributions

This will work on any Linux distribution

Requirements

A working Linux install with Python and VLC.

Difficulty

Easy

Conventions

  • # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ – requires given linux commands to be executed as a regular non-privileged user

Introduction

There are plenty of ways to play audio files with Python. It really depends on your application, but the easiest way, by far, is to use the bindings for VLC to control VLC with Python, and play your files.

With VLC, you don’t need to worry about codecs and file support. It also doesn’t require too many complicated methods, and/or objects. So, for simple audio playback, VLC is best.

Read more

How to disable Bash shell commands history on Linux

Commands history is a great feature of the bash shell. However, there are times when it is best to disable it. One good example when you might prefer your bash shell commands history to be disabled is on the production server accessible from the external network where potential attacker might gain an access to your server and re-read your history file to search for useful commands, services in use or accidentally inserted passwords. Below you can find bunch of commands to help you disable history from being stored or how to remove all currently stored commands.

Where are history commands stored

All commands your enter on the shell are stored within your local directory into a file called .bash_history. This is a default history file defined by HISTFILE variable:

# echo $HISTFILE
/root/.bash_history

Read more

Tutorial on how to write basic udev rules in Linux

Objective

Understanding the base concepts behind udev, and learn how to write simple rules

Requirements

  • Root permissions

Difficulty

MEDIUM

Conventions

  • # – requires given linux commands to be executed with root privileges either
    directly as a root user or by use of sudo command
  • $ – requires given linux commands to be executed as a regular non-privileged user

Introduction

In a GNU/Linux system, while devices low level support is handled at the kernel level, the management of events related to them is managed in userspace by udev, and more precisely by the udevd daemon. Learning how to write rules to be applied on the occurring of those events can be really useful to modify the behavior of the system and adapt it to our needs.

Read more

How to automatically execute shell script at startup boot on systemd Linux

The following config will discuss a basic example on how to execute shell script during a boot time on systemd Linux. There maybe various reason why you might want to execute shell script during Linux startup like for example to start a particular custom service, check disk space, create a backup etc.

The following example below will serve as a basic template to be later modified to suit your specific needs. In the example below we will check a disk space of a /home/ directory during a boot time and write a report to /root/ directory.

Systemd service unit

First, we need to create a systemd startup script eg.disk-space-check.serviceand place it into /etc/systemd/system/ directory. You can find the example of such systemd startup script below:

[Unit]
After=mysql.service

[Service]
ExecStart=/usr/local/bin/disk-space-check.sh

[Install]
WantedBy=default.target

Read more

How to Use JSON API Data In Python

Objective

Consume a JSON API in Python.

Distributions

This will work on any Linux distribution.

Requirements

A working Linux install with Python.

Difficulty

Easy

Conventions

  • # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ – requires given linux commands to be executed as a regular non-privileged user

Introduction

One of the main reasons that you’d like to work with JSON in Python is consuming APIs. There are hundreds of excellent public APIs out there and ready to use in your application. Even huge players on the web, like Facebook and Twitter, out out APIs for you to work with.

You can build entire applications around API data, including building web applications that aggregate, manipulate, and display that data in a convenient way.

Read more

How to Encode Data From Python To JSON

Objective

Encode Python data into JSON.

Distributions

This will work on any distribution with Python installed.

Requirements

A working Linux install with Python

Difficulty

Easy

Conventions

  • # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ – requires given linux commands to be executed as a regular non-privileged user

Introduction

JSON is the universal format for passing data between programs and/or languages, especially on the web. Python has excellent built-in support for converting data from a Python program into JSON for another program to use.

Read more

bash scripting

How to modify scripts behavior on signals using bash traps

Objective

The objective of this tutorial is to describe how to use the bash shell trap builtin to make our scripts able to perform certain actions when they receive a signal or in other specific situations.

Requirements

  • No special requirements

Difficulty

EASY

Conventions

  • # – requires given linux commands to be executed with root privileges either
    directly as a root user or by use of sudo command
  • $ – requires given linux commands to be executed as a regular non-privileged user

Introduction

bash scriptingWhen writing scripts that are meant to run for a considerable time, it’s very important to increase their robustness by making them able to react to system signals, executing specific actions when some of them are received. We can accomplish this task by using the bash trap builtin.

Read more

Python Exception Handling

Introduction

Python will let you know when you get your syntax wrong. It’ll immediately fail and not allow your program to run.

What about when your code has a different type of problem? Those are called exceptions, and they tend to be harder to catch. It’s up to you to recognize situations where hey might come up and catch them to prevent your program from crashing altogether.

Imagine a scenario where you need user input. Do you want your program to crash every time a user mistypes something or enters something erroneous? That’s far from ideal. Since you know there could be a problem there, you can tell Python to look out for one, and recover gracefully.

Read more