Bash Scripting Tutorial

Under construction

The purpose of this Bash Scripting Tutorial is to get you started with Bash scripting as fast as possible without any prior knowledge. This Bash scripting Tutorial can also serve you as a quick, comprehensive guide to the Bash Scripting basics without much technical jargon. However, understanding basic programming concepts when reading going through this tutorial will be advantages.

ARE YOU LOOKING FOR MORE DETAILED BASH SCRIPTING BASICS?
If you are looking for a beginner style Bash scripting tutorial covering the bash scripting fundamentals in more detail, visit our Bash Scripting Tutorial for Beginners.

In this tutorial you will learn:

  • How to XXX
  • How to XXX
  • How to XXX
  • How to XXX

Read more

Redirect or block traffic based on country geographical location using Apache mod_geoip

In this config you will learn how to block or redirect web traffic based on the visitor’s country geographical location using Apache’s geoip mod.

Apache mod_geoip installation

Assuming that you already have Apache webserver installed and running we start by the installation of mod_geoip:

UBUNTU/DEBIAN
# apt-get install libapache2-mod-geoip
CENTOS/REDHAT/FEDORA ( epel enabled only )
# yum install mod_geoip.x86_64

Read more

C++ pointer example diagram

C++ : Understanding pointers

Author: Lubos Rendek

Date: 10.09.2009

Update: 04.03.2010 – Section 7.2 was created to clarify relation between Pointers and Arrays in C++

Introduction

This article is intended to all programing enthusiasts on all levels who do wish to understand pointers in C++ language. All code presented here is not a compiler specific and all examples will be written in plain ANSI C++. Debate about pointers can stretch for miles, and you would need to go really far to master it all. If you really want to run that far, this article gives you a clear understanding of fundamental concepts about pointers and prepares you for that journey. However, those who are new to C++ programming make sure that you are able to write and run your own C++ “hello world” program, and also it is recommended that you have a basic understanding of C++ functions and classes. If you need to refresh your knowledge about how to compile and run C++ program, use functions and classes, please read an appendix at the end of this document before you continue reading this article.

What is a Pointer?

Pointer is a variable that stores a memory address. OK, that is simple ! But, what is a memory address then? Every variable is located under unique location within a computer’s memory and this unique location has its own unique address, the memory address. Normally, variables hold values such as 5 or “hello” and these values are stored under specific location within computer memory. However, pointer is a different beast, because it holds the memory address as its value and has an ability to “point” ( hence pointer ) to certain value within a memory, by use of its associated memory address.



Retrieving a Variable’s Memory Address

OK, enough talking and let’s get down to the pointer business. To retrieve a variable’s memory address, we need to use address-of operator &.

#include <iostream>
int main()
{
using namespace std;
// Declare an integer variable and initialize it with 99
unsigned short int myInt = 99;
// Print out value of myInt
cout << myInt << endl;
// Use address-of operator & to print out
// a memory address of myInt

cout << &myInt << endl;

return 0;
}

OUTPUT:

99
0xbff26312

The first line of the output contains an integer value 99 and on the second line, there is a memory address of myInt printed out. Please note that your output will be different.

Assigning a Variable’s Memory Address to a Pointer

Before we can assign a memory address to a pointer, we need to declare one. Declaring a pointer in C++ is as simple as to declare any other variable with one single difference. Asterix symbol ” * ” needs to be add and located after variable type and before a variable name. One rule has to be followed when assigning memory address to a pointer: pointer type has to match with variable type it will point to. One exception is a pointer to void, which can handle different types of variables it will point to. To declare a pointer pMark of type unsigned short int a following syntax is to be used:

#include <iostream>

int main()
{
using namespace std;

// Declare and initialize a pointer.
unsigned short int * pPointer = 0;
// Declare an integer variable and initialize it with 35698
unsigned short int twoInt = 35698;
// Declare an integer variable and initialize it with 77
unsigned short int oneInt = 77;
// Use address-of operator & to assign a memory address of twoInt to a pointer
pPointer = &twoInt;
// Pointer pPointer now holds a memory address of twoInt

// Print out associated memory addresses and its values
cout << "pPointer's memory address:\t\t" << &pPointer << endl;
cout << "Integer's oneInt memory address:\t" << &oneInt << "\tInteger value:\t" << oneInt << endl;
cout << "Integer's twoInt memory address:\t" << &twoInt << "\tInteger value:\t" << twoInt << endl;
cout << "pPointer is pointing to memory address:\t" << pPointer << "\tInteger value:\t" << *pPointer << endl;

return 0;
}

OUTPUT:

pPointer's memory address:              0xbff43314
Integer's oneInt memory address: 0xbff43318 Integer value: 77
Integer's twoInt memory address: 0xbff4331a Integer value: 35698
pPointer is pointing to memory address: 0xbff4331a Integer value: 35698

C++ pointer example diagram



The diagram above is a high level visual abstraction of how are variables stored within a computer memory. Pointer pPointer starts at memory address 0xbff43314 and takes 4 bytes. Pointer pPointer holds as a value a memory address of a short int twoInt ( 2 bytes ) which is 0xbff4331a. This address is stored as a binary data within a pointer’s memory space allocation. Therefore, dereferencing a pointer with a memory address 0xbff4331a will indirectly access a value of twoInt which is in this case a positive integer 36698.

Read more

An easy way to encrypt and decrypt large files using OpenSSL and Linux

Below is a quick config on how to to encrypt and decrypt large files using OpenSSL and Linux such as Redhat, Ubuntu, Debian, CentOS, Fedora etc. First, what you will need is a some sort of arbitrary file. Let’s create 1GB file now:

$ fallocate -l 1G large_file.img
$ ls -lh large_file.img
-rw-r--r--. 1 lrendek lrendek 1.0G Jan  2 16:40 large_file.img

Read more

Linux authentication login with USB device

This article describes a method how to use a USB memory device as an authentication token to log in into a Linux system instead of traditional password. This can be accomplished by use of Pluggable Authentication Modules ( PAM ) and some sort of USB storage device such as USB memory stick of Mobile phone with SD card attached.

This authentication technique can be also further expanded into Two-Factor authentication where two authentication methods involving USB token and one-time password can be merged together to produce a greater security. This article is written using Ubuntu Linux systems. However, users of other Linux distributions should be able to follow below described steps to achieve the same results.

Pluggable Authentication modules Installation

Pluggable Authentication modules are available on most of Linux system in a form of pre-compiled packages accessible from a relevant repository. First we need to install required packages for PAM USB authentication:

$ sudo apt-get install pamusb-tools libpam-usb

Read more

How to transfer data over the network with nc (netcat) command on Linux

The nc ( netcat ) command can be used to transfer arbitrary data over the network. It represents a quick way for Linux administrators to transfer data without the need for an additional data transfer services such as FTP, HTTP, SCP etc. This config will show you an example on how to transfer data between to network hosts. We will be transferring data myfile.txt file from a localhost to a destination host with an IP address 10.1.1.2.

Destination host

The nc command first needs to be started on the network host to which we need to transfer data. We will instruct nc to listen for an incoming request on user defined port number and once the client request comes receive the desired data. Pick the port number of your choice to ensure that it is not blocked by firewall and it is accessible from the source host.

$ nc -l -p 7555 > myfile.txt

Read more

How to obtain a laptop battery information and charging state on Linux

The following linux commands will help you to obtain the information about your laptop battery such as manufacturer, part type, capacity, voltage etc. First, tool which can be used for this job is dmidecode command. If you currently do not have dmidecode available on your Linux system you can install it by:

UBUNTU/DEBIAN
# apt-get install dmidecode
CENTOS/FEDORA
# yum install dmidecode

next, using dmidecode we can obtain some hardware information about our battery:

# dmidecode -t 22
# dmidecode 2.12
SMBIOS 2.6 present.

Handle 0x002E, DMI type 22, 26 bytes
Portable Battery
        Location: Rear
        Manufacturer: LGC
        Name: 42T4942
        Design Capacity: 93240 mWh
        Design Voltage: 11100 mV
        SBDS Version: 03.01
        Maximum Error: Unknown
        SBDS Serial Number: 073C
        SBDS Manufacture Date: 2012-03-23
        SBDS Chemistry: LION
        OEM-specific Information: 0x00000000

Read more

How to convert documents between LibreOffice and Microsoft Office file formats on Linux

In this config you can learn how to convert documents format between LibreOffice and Microsoft Office file formats on Linux. The preferred tool to use for a file format conversion between LibreOffice and Microsoft Office is unoconv. Let’s start with the installation of unoconv package:

FEDORA/REDHAT/CENTOS
# yum install unoconv
DEBIAN/UBUNTU
# apt-get install unoconv

and create some LibreOffice document document.odt and spreadsheet spreadsheet.ods to play with:

$ file document.odt 
document.odt: OpenDocument Text
$ file spreadsheet.ods 
spreadsheet.ods: OpenDocument Spreadsheet

Read more