Bash How to Sum Variables

Reading Time: < 1 minute

Bash How to Sum Variables

Let’s define two variables:

FILES_ETC=1416
FILES_VAR=7928

If you want to get total value, you can do:

TOTAL_FILES=$((FILES_ETC+FILES_VAR))
echo $TOTAL_FILES

For 100% compatibility with old shells, you can use external command expr. In modern systems (ec. Centos 7 64bit), in fact, expr is not an external command. It is an internal shell function. So there is not a significant difference in speed between the above and this command:

TOTAL_FILES=`expr $FILES_ETC + $FILES_VAR`
echo $TOTAL_FILES

Funny thing is that there must be spaces between arguments. In fact, expr is a command, $FILES_ETC is the first argument, + is the second argument and $FILE_VAR is the third argument.

If you want to add numbers with decimal points, you can use bc. bc is a very useful external command for making arithmetical operations:

TOTAL_FILES=$(echo $FILES_ETC + $FILES_VAR | bc)
echo $TOTAL_FILES

Common mistakes are spaces before or after ‘=’ sign. However bc and expr commands expect spaces between arguments and operators, so in these cases don’t forget to spaces.

Difference between a Kernel and Shell

Reading Time: < 1 minute

Difference between a Kernel and Shell

A shell is a command interpreted, way to communicate with the operating system and kernel using the command line.

Consider what would happen if we have only kernel and no shell ?. We would have a device with OS but there is no method to use it. We need to have an interface between OS and humans. That’s shell’s purpose

A kernel is a low-level program interfacing with the hardware in privileged mode. It is an essential part of the system. Any requests from the shell is processed by the kernel.

It is impossible to have a shell without a kernel. Without a kernel, we can not execute commands.

A kernel is the lowest level program running on computers. The kernel does task scheduling, handles filesystems, I/O handling, memory management.

Best Shell IDE

Reading Time: < 1 minute

Best Shell IDE

There is no shell scripting ide. There are some examples of text editors that help you to write code.

1. Sublime Text

Sublime Text has many powerful features that make coding painless. It has all basic features mentioned on this page and many others like multi-select (hold CTRL and put mouse cursor in another line), creating your own snippets(lines of code that repeat), minimap(zoomed view of entire file).

2. Atom

Atom is developed by Github so it supports Github integration. It is often called the “hackable IDE of 21st century”, so you could easy customize almost everything.

Cool visual extension is “Power mode”, every time you hit a key, editor does a little move, like you hit the screen.

3. Geany

Geany is a lightweight IDE, aims to provide fast development environment. Features: filebrowser, save actions( autosave, instantsave, backupcopy), split window. In ubuntu you can install Geany using apt:

sudo apt-get install geany

4. Kate

It is a pre-installed text editor in Kubuntu. Some of the useful features: embedded terminal, SQL plugin, find and replace, syntax highlighting, bracket matching, auto backup, auto-completion with argument handling.

Bash How to Rename Directory

Reading Time: < 1 minute

Bash How to Rename Directory

Use the “rename” command. Syntax of rename command:

rename [options] expression replacement file

If we want to rename directory “old-name-dir” to “new-name-dir”:

rename 'old-name-dir' new-name-dir old-name-dir

To preview change type the “ls” command.

You can rename the directory with the “mv” command:

mv old-name-dir/ new-name-dir

It will rename old-name-dir to new-name-dir. If old-name-dir contains any files, it is good advice to add option -R after mv.

Bash How to Escape Single Quote

Reading Time: < 1 minute

Bash How to Escape Single Quote

Enclosing characters or variables in single quotes (‘) represents the literal value of characters. Example:

$foo="Hello world"
echo '$foo'

Output: $foo

If you want to write the content of variable $foo in ‘ use the character \ to escape a single quote.

echo \'$foo\'

Output: ‘Hello world’

In single quotes, escaping a single quote is not possible, thus you can’t include a single quote into single quotes

Instead of single quotes use double quotes (“):

echo "Hello'world"

Output: Hello’world

Single quote may not occur between single quotes, even when preceded by a backslash, but this works:

echo $'I\'m a linux admin.'

Output: I’m a linux admin.

VI Basic Commands for UNIX in Nutshell

Reading Time: 4 minutes

In this chapter, we will explore vi – the excellent Unix editor. There are many ways to edit text files in Unix; however, one of the best is using screen-oriented editors like vi who allow you to see context lines around a line that needs editing.

VIM, or Vi IMproved (commonly shortened to vim) is an improved version of the vi editor. It has rapidly grown in popularity because it can be used as both a command line and graphical interface text-editor with more features than standard vi that you might not find elsewhere like syntax highlighting and multitasking capabilities.

  • It’s usually available on all the flavors of Unix system.
  • Its implementations are very similar across the board.
  • It requires very few resources.
  • It is more user-friendly than other editors such as theΒ edΒ or theΒ ex.

VI Basic Commands for UNIX in Nutshell

Vi has 3 basic modes of operation: command (default), input, last line mode.

VI Editor Command mode

In command mode, you can run commands to search, copy, move, remove text.

VI Editor Input mode

In input mode, you can insert text into the file. Everything you type will be interpreted as text. many ways how to activate input mode (vi is case sensitive):

  • i – Inserts text before the cursor.
  • I – Inserts text at the beginning of the line.
  • o – Opens a new blank line below the cursor.
  • O – Opens a new blank line above the cursor.
  • a – Appends text after the cursor.
  • A – Appends text at the end of the line.

VI Editor Last line mode

To get into the last line mode type ‘:’ only from command mode. After type ‘:’ you will see a colon character appear at the beginning of the last line of your vi editor. It means vi is ready for type a “last line command”. To end vi type ‘q’ from last line mode.
You can return to command mode from input or last line mode pressing Esc.

Moving the Cursor

Key Cursor movement
w Forward one word.
b Back one word.
e To the end of the current word.
$ To the end of the line.
0(zero) To the beginning of the line.
^ To the first non-whitespace character on the line.
G Goes to the last line of the file.
IG Goes to the first line of the file.
Ctrl + F Pages forward one screen.
Ctrl + B Pages back one screen.
Ctrl + D Scrolls down one-half screen.
Ctrl + U Scrolls up one-half screen.
Ctrl + L Refreshes the screen.

Text-Deletion Commands

Command Function
R Overwrites or replaces characters on the line at and to the right of cursor. To terminate press Esc.
C Changes or overwrites characters from cursor to the end of the line.
s Substitutes a string for a character at the cursor.
x Deletes a character at the cursor.
dw Deletes a word or part of the word to the right of the cursor.
dd Deletes the line containing the cursor.
D Deletes the line from the cursor to the right end of the line.
:n, nd Deletes lines n-n. Example :2,80d deletes lines 2-80.

Text-Changing Commands

Command Function
cw Changes or overwrites characters at the cursor location to the end of that word.
r Replaces the character at the cursor with one other character.
J Join the current line and the line below.
xp Transposes the character at the cursor and the character to the right of the cursor.
~ Changes the case of the letter, either uppercase or lowercase, at the cursor.
u Undo the previous command.
. Repeats the previous command.

Text-Replacing Commands

Command Function
/string Searches forward for the string from the cursor.
?string Searches backward for the string.
n Searches for the next occurrence of the string. Use this command after searching for a string.
N Searches for the previous occurrence of the string. Use this command after searching for a string.
:%s/old/new/g Searches for the old string and replaces it iwth the new string globally.

Copy and Paste Commands

Command Function
yy Yanks a copy of the line
p Puts yanked or deleted text under the line containing the cursor.
P Put
:n,n co n Copies lines n-n and puts them after line n. Example: 1, 5 co 8 copies lines 1-5 and puts them after line 8.
:n,n m n Moves lines n-n to line n.
Example: 1,5 m 8 moves lines 1-5 to line 8.

File Save and Quit Commands

Command Function
:w Saves the file with changes by writing to the disk
:w new_file Writes the contents of the buffer to new_file.
wq Saves the changed file and quits editor vi.
😑 Saves the changed file and quits editor vi.
ZZ Saves the changed file and quits editor vi.
:q! Quits without saving changes.

Customizing vi Session

Command Function
:set nu Shows line numbers.
:set nonu Hides line numbers.
:set ic Instructs searches to ignore cases.
:set noic Instructs searches to be case-sensitive.
set list Display invisible characters.
:set showmode Display the current mode of operation.
:set noshowmode Turns off the mode of operation display.
:set Displays all the vi variables that are set.
:set all Display all vi variables and their values.

Customizing vi Session
To automatic customization for all vi sessions do the following steps:

  • Create a file in your home directory named ‘ . exrc’
  • Enter any of the set variables into the ‘ . exrc’ file.
  • Enter each ‘set variable’ command on one line.

Vi reads ‘exrc’ file every time before starting vi sessions.

Command Function
:set nu Shows line numbers.
:set nonu Hides line numbers.
:set ic Instructs searches to ignore case.
:set noic Instructs searches to be case-sensitive.
set list Display invisible characters.
:set showmode Display the current mode of operation.
:set noshowmode Turns off the mode of operation display.
:set Displays all the vi variables that are set.
:set all Display all vi variables and their values.

Bash How to Add to Array

Reading Time: < 1 minute

Bash How to Add to Array

If you want to add a new item to the end of the array without specifying an index use:

~]$ my_array=()
~]$ my_array+=("Arch")
~]$ echo ${my_array[@]}
Arch

In our previous article Bash How to Print Array, we have already seen the array creation, accessing the array elements and getting the count of array.

The array is created and can be verified as follows:

$ declare -p my_array
declare -a my_array=([0]="Arch")

Now we add another element to the my_array:

my_array+=("Debian")
declare -p my_array
declare -a my_array=([0]="Arch" [1]="Debian")

Using declare to check the BASH variables

Now in order to find out the number of elements within our array, you can use “#” to get the index count, The indexed elements count are as follows for 2 element array:

echo ${#my_array[@]}
2

To add the elements to the end of the array you can use this technique demonstrated as follows:

As your array is sequential list, To insert the element to the last index, This is done by getting the total count of elements and adding that as the index:

my_array[${#my_array[@]}]="Fedora"

Now the 3rd element “Fedora” is inserted to the end of array

echo ${my_array[@]}
Arch Debian Fedora

as you already understood that “${#my_array[@]}” gets the length of the array.

Using this technique you can append arrays and also assign them to a new array as demonstrated in the following example:

new_array=(${my_array[@]} "Ubuntu")
echo ${new_array[@]}
Arch Debian Fedora Ubuntu

This is how the elements inside the new array are stored:

declare -p new_array
declare -a new_array=([0]="Arch" [1]="Debian" [2]="Fedora" [3]="Ubuntu")

What is Fork Bomb and How to Avoid It

Reading Time: < 1 minute

What is Fork Bomb and How to Avoid It

The fork bomb is a recursive bash function. It is a DoS attack against linux operating system. Definition of fork bomb:

:(){ :|:& };:

What do all these symbols mean?

  • :() – defines function called “:”
  • :|: – recursive sends output to “:”
  • & – puts function to background
  • ; – terminate the function definition
  • : – at the end calls the function

Be careful this example may crush your computer. We can prevent against fork bomb limiting the number of processes for user (or group of users) in file /etc/security/limits.conf.

For example, we want to limit the number of process to 300:

likeIT hard nproc 30

“likeIT” is name of user. If you want to apply this limitation to the group, use “@groupName”.

There is an example of the whole configuration file:

Let’s explain some important keywords from /etc/security/limits.conf file:

[domain] [type] [item] [value]

The domain can be:

  • a user name
  • a group name – use @group syntax

Type can have these two values:

  • soft – for enforcing the soft limits
  • hard – for enforcing hard limits

Item can be:

  • core – limit the core file size (KB)
  • fsize – maximum filesize (KB)
  • cpu – max CPU time (MIN)
  • nproc – max number of processes

Apple: How to Work with Terminal in Mac

Reading Time: 3 minutes

Apple: How to Work with Terminal in Mac

The iOS operating system is basically modified UNIX with beautiful graphics. This means that if you need something to set up or automate, you can use the command line the UNIX shell.

The shell is available on Apple iMac and MacBooks. On the iPhones and iPads, the command line is hidden.

How to start a MAC terminal?

Click the launchpad on the bottom bar, find the terminal icon and launch it.

First commands

This command displays the current work directory you are in.

pwd

Since the iOS is de facto unix, the entire file system complies with the File Hierarchy Standard (FHS) that specifies the tree directory. At the top of the tree there is a “/” symbol. Under “/” are individual directories and files in tree structure.

Use this command to list the contents of the directory in which you are currently:

ls -l

The first column shows the type (d = directory) and system rights (r = read, w = write, x = execute) in the triad in the following order: owner, group, all. The third column shows who the owner is. The column shows the group that owns the given file. The sixth column shows the time of the last modification and the seventh file name.

Use this command to change the current work directory (to /home):

cd /home

If you specify this command without parameters, the cd command is set to the current home directory that you specify in the $HOME variable.

Do you want to view the contents of any Mac shell variable? For example, $HOME? It is simple:

echo $HOME

How to use terminal: basic advice

Hint#1: Arrow up to view the last commands.

Hint#2: Using Ctrl A, you will get to the top of the line by pressing Ctrl E at the end of the line.

Hint#3: Hold down the left mouse button to select the text and then right-click on the menu to select “copy” or “paste” as needed.

Hint#4: Magic button “home”: command Ctrl C to interrupt the execution of the current command and get back to the command prompt. For example, you can try this by entering a yes command on the terminal, which causes the ypsilon to endlessly. You interrupt this infinite program with Ctrl C.

Most useful commands in terminal

Overview of hard disk usage
This command displays the current use of disks that are “assembled” to your computer:

df -aH

Option H means human-readable output. By selecting and specifying that we want to display all mounted drives.

Which folder does the disk space take?
Use the cd command to set up the folder you want to see how much it takes. You can check the entire file system using the cd /. Using this command, you will be able to print out in a comprehensible manner how much you deal with:

du -sh /* 2>/dev/null

The beaked twin determines that we do not want to see any error messages.

What does MAC do now? Which processes are most active?

This command displays the most active processes.

top

Each process also has a PID (Process ID), according to which the process can be uniquely identified and, for example, shut down. Use the q button to finish the top.

How to find and destroy a particular process by name?

This command looks for the command bash – the command line you are running:

ps aux | grep bash | grep -v grep

The second column indicates the PID. In my case, it is 2335. Use this command to exit the program. beware, the terminal will disappear! Muhaha πŸ˜€

kill 2335

What is currently happening in the system? What bothers MAC?
With this command, you are constantly monitoring what the system says:

tail -f /var/log/system.log

To quit tail command, use Ctrl C

Where do I Find Bash

Reading Time: < 1 minute

Where do I Find Bash

You can find bash as an executable program located in standard binary directories of your operating system.

If you are using an operating system which does not contain bash pre-installed (FreeBSD, Windows), you can download and build source code from gnu.org. Windows users can use Cygwin.

Bash supports two distinct operation modes: interactive and non-interactive mode. In interactive mode, the bash waits for you for entering commands. In non-interactive mode, the bash executes commands from the script file without waiting for the user’s commands.

Assuming you have bash installed, you can run bash from the terminal. Most terminals are pre-configured to start the shell program. To find out where is your bash located enter the following command:

echo $SHELL

Output: /bin/bash

Another way how to get path to bash:

echo $BASH

Output: /bin/bash

If you are not sure if you are using bash, enter:

echo $0

Output: -bash

$0 prints the program name, in our case it is actually running shell.

Linux: File System Hierarchy

Reading Time: 2 minutes

Linux: File System Hierarchy

In this tutorial is described Filesystem Hierarchy Standard (FHS), which specifies required directories. The root directory is “/” and it should contain only the subdirectories.

/bin

  • Contains binaries which can be executed from the command line (ls, grep, mkdir…)
  • Programs that can be used by users (system, admin, normal users)
  • It can be in single-user mode

/boot

  • Contains everything required for the boot process
  • Kernel
  • Grant Unified Boot-loader
  • LILO (LInux LOader)

/sbin

  • Binaries
  • Program used by system and admin
  • Normal users can use programs in /bin if they are allowed
  • Usually, normal users do not have this directory in $PATH variable

/dev

  • Files of all devices
  • Created during installation operating system.
  • Create new devices: /dev/MAKEDEV
File Description
Sda First SCSI drive on the SCSI/SATA bus
md0 First group of meta discs (RAID)
ttyS0 First serial port
lp0 First parallel printer
null bin for bits
random Deterministic random bits
urandom Non-deterministic random bits

/etc

File Description
passwd Users information
fstab Partition and storage mounting information
rc or rc.d or rcX.d Run commands – commands that runs when OS starts

/home

  • The home directory for users
  • All data and system settings of users
  • Can be divided into groups (school, office, financial)

/root

  • Home directory for user root
  • Normal users don’t have permissions.

/lib

  • Libraries for programs
  • /lib/modules: kernel modules, network controls, file system control

/tmp

  • Temporary files
  • Used by running programs

/mnt

  • Mounting temporary file systems
  • File systems from /etc/fstab are mounted during start OS
  • Network file systems
  • /media: DVD, USB

/usr

  • Programs, libraries installed from OS distribution
  • Accessible for everyone
  • /usr/
Directory Description
local Software installed by admin on local device
X11R6 Files of Windows OS
bin Almost all commands for users
sbin Usually server’s programs
include Header files for C language
lib Stable libraries
  • /usr/share/
Directory Description
X11 Files of Windows OS
dict Glossary
man Manual pages
doc Documentation
info Information files
src source files

/var
Contains files that are changed when OS is running.

Subdirectory Description
log Logging files
run Run-time variable data
spool Program using queue (mails, printers)
mail Mailbox
local variable data from /usr/local
lib Holds dynamic data libraries/files
lock Lock files. Indicates that resource (database, file) is in use and should not be accessed by another process.

/opt
Third-party software

/proc

  • Created by OS kernel
  • Information about system
  • Stored only in RAM
  • Does not use any disc space
  • Every process has a subdirectory (by PID)
Subdirectory Description
/PID/status Stats about process
/PID/cmdline How was the process started and what input arguments
/PID/maps Region of contiguous virtual memory in a process or thread
/PID/environ Environment of process
  • Interesting files
File Description
cpuinfo Information about CPU
meminfo Usage of memory
version Kernel version
cmdline Kernel’s parameters from the boot loader
devices List of drivers for the kernel
interrupts Which interrupts are used and how many times
ioports list of currently registered port regions used for input or output communication.
dma ISA Direct Memory Access channel
kcore Image of physical system memory
cmdline Kernel’s parameters from the boot loader
cmdline Kernel’s parameters from the boot loader

/lost+found

  • Recovered or damaged data after a crash.
  • Each partition has its own /last+found directory.