Multi dimensional array

Multi-dimensional arrays are one of the most confusing element for beginners. In hope of helping the beginners, I am writing this post. To make things simple, I will assume we use only and only integer numbers.

Array (1D Array)
To understand multi-dimensional array, we need to first understand an array. An array is a data structure that contains a group of elements. So, a collection of multiple items make an array.


Follow the example -
  • 2 - a single element
  • [2, 3, 4] - an array of elements
  • [2] - an array containing only one element
So, an array can be thought of as a row of elements. Where each element has a index associated with it. Arrays can give access to its elements using an index. Array indexes start from 0.

Follow the program -
var a = [1,2,3]

console.log(a)

console.log(a[0])


2D Array

A 2D Array is an array of multiple 1D Arrays. Now we know 1D array, so, we can stack them together to form a 2D array.



In 1D array, we saw that each element has an index and we can access the elements using the index. In case 2D Arrays, we do the same. The only difference is that, for 1D array, we access a single element using index, in 2D arrays, we access an array using an index.

Follow the program -
var _2d = [[1,2],[3,4],[5,6]]

console.log(_2d) //output [[1,2],[3,4],[5,6]]

console.log(_2d[0]) //output [1,2]

console.log(_2d[1]) //output [3,4]

console.log(_2d[2]) //output [5,6]


We saw how we can access an array from a 2D Array, now we will see how we can access a single element from a 2D Array. To access a single element from a 2D array, we need two indexes, first one is to access the nested 1D array and the second one to get access to the single element.
console.log(_2d[0][0]) // output 1

console.log(_2d[0][1]) // output 2

console.log(_2d[1][0]) // output 3

console.log(_2d[1][1]) // output 4

console.log(_2d[2][0]) // output 5

console.log(_2d[2][1]) // output 6


Remember that, we can also change the values of the 2D arrays using the similar way we did in 1D array
_2d[0][0] = 100

console.log(_2d) //output [[100,2],[3,4],[5,6]]

3D Array
A 3D Array is an array of multiple 2D Arrays. we can extend the same process of defining and using a 2D array to define and use 3D Array.

Here, the array index gives you access to a 2D Array.
var _3d= [ [ [1,2], [3,4], [5,6] ], [ [7,8], [9,10], [11,12] ] ]

console.log(_3d) // output [ [ [1,2], [3,4], [5,6] ], [ [7,8], [9,10], [11,12] ] ]

console.log(_3d[0]) //output [ [1,2], [3,4], [5,6] ]

conosle.log(_3d[1]) //output [ [7,8], [9,10], [11,12] ]


Now to access a single element in the array, now we need 3 indexes, the first one is to get the 2D Array, the second is to access the nested 1D array, the third is to get the element in the 1D array.
var _3d= [ [ [1,2], [3,4], [5,6] ], [ [7,8], [9,10], [11,12] ] ]

console.log(_3d) // output [ [ [1,2], [3,4], [5,6] ], [ [7,8], [9,10], [11,12] ] ]

conosle.log(_3d[0][0][0]) // output 1

conosle.log(_3d[0][0][1]) // output 2

conosle.log(_3d[0][1][0]) // output 3

conosle.log(_3d[0][1][1]) // output 4

conosle.log(_3d[0][2][0]) // output 5

conosle.log(_3d[0][2][1]) // output 6


conosle.log(_3d[1][0][0]) // output 7

conosle.log(_3d[1][0][1]) // output 8

conosle.log(_3d[1][1][0]) // output 9

conosle.log(_3d[1][1][1]) // output 10

conosle.log(_3d[1][2][0]) // output 11

conosle.log(_3d[1][2][1]) // output 12

The modification also works the same way.

console.log(_3d) // output [ [ [1,2], [3,4], [5,6] ], [ [7,8], [9,10], [11,12] ] ]

_3d[0][0][1]=50

console.log(_3d) // output [ [ [1,50], [3,4], [5,6] ], [ [7,8], [9,10], [11,12] ] ]


4D Array
Moving on to the next stage, 4 dimensional array. Now, we know what a 3 dimensional array looks like. Now we will extend it to create 4 dimensional array.

A 4 dimensional array is an array of a three dimensional array.

var _4d = [[[[1,2], [3,4]], [[5,6], [7,8]]],  [[[9,10], [11,12]], [[13,14], [15,16]]]]

Here, the array index gives you access to a 3D Array.

console.log(_4d[0])//output [ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ]


Now to access a single element in the array, now we need 4 indexes, the first one to get the nested 3D Array, the second one is to get the nested 3D Array, the third is to access the nested 1D array, the fourth is to get the element in the 1D array.

console.log(_4d[0][0][0][0]) //output 1

console.log(_4d[0][0][0][1]) //output 2

console.log(_4d[0][0][1][0]) //output 3

console.log(_4d[0][0][1][1]) //output 4

console.log(_4d[0][1][0][0]) //output 5

console.log(_4d[0][1][0][1]) //output 6

console.log(_4d[0][1][1][0]) //output 7

console.log(_4d[0][1][1][1]) //output 8


console.log(_4d[1][0][0][0]) //output 9

console.log(_4d[1][0][0][1]) //output 10

console.log(_4d[1][0][1][0]) //output 11

console.log(_4d[1][0][1][1]) //output 12

console.log(_4d[1][1][0][0]) //output 13

console.log(_4d[1][1][0][1]) //output 14

console.log(_4d[1][1][1][0]) //output 15

console.log(_4d[1][1][1][1]) //output 16


Modification of the array elements can be done in the same way.

Multi-dimensional array
Based on the concept of extending, we can simply stack arrays of dimension n and get n+1 dimensional array.

For example-

  1. We stacked array of dimension 0 (a single element) to form a one dimensional array.
  2. We stacked array of dimensional 1 to form a two dimensional array
  3. We stacked array of dimensional 2 to form a three dimensional array
  4. We stacked array of dimensional 3 to form a four dimensional array
In the same way we can stack n dimensional array to get an array of n+1.

Hope this post helps you have a clear concept of arrays.

Programming is fun...

'Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program.' - Linus Torvalds 


Programming actually gives you satisfaction. When a program written by you works as expected, you feel extremely satisfied. Writing a program is more like instructing your slave (in form of a PC). If you instruct a person to give you information for a whole day about different topics, s/he cannot do that, if you ask a person to sing songs for a whole day s/he cannot do that. Because, living creatures are not repetitive in nature. 

Computers love to do repetitive jobs. So you can accomplish your job done using the machine in front of you. It will follow each instructions provided to it without any objection.

Sounds interesting ?
If it grows interest in you, you can follow this space for more.

What will you achieve following these space ?

This series will try to
1.            give you an overview of programming and how computer programs work. 
2.            provide you information on Java with examples.
3.      draw analogy of Java programs to real world scenarios

Who am I ?

I am Palash Kanti Kundu, a programmer.

Why am I doing this ?

1. Almost two years ago, one of my seniors told me, 'Every day you learn something new'
2. Someone in my team told me this morning, 'knowledge sharing always helps'. I am just trying to be helpful to people who are interested in programming.
3. My friend told me, if no one is reading your blog also, you keep it as your own online 


What if you are not getting what you are finding for ?

You can post your comments on blogs and I will try my best to provide you the information.

What if you find wrong information ?

Everyone is error prone. So am I.
Please post as a comment on the blog which contains wrong information. I will verify and correct it.

What and why UNIX ?

Welcome to the series devoted to the base platform of deployment.

In this article we'll look into the basics of computers and very less on the applications that run on any computer.

Many of us use different applications for different purposes like surfing the internet, playing games, watching movies, writing codes and for hell lot of things we use different applications. We use applications not only in our PCs but also in other devices like mobile phones, tablets, embedded systems, Printers, TVs, CD Players, Cars, ATMs, Micro-ovens and in many other devices.

You might be wondering how does a car have a relation to an application.

Well, it does. Wherever you see anything tagged with the word 'DIGITAL', application follows you. You may know it or may be the application is abstracted in a way that you cannot understand it. But the truth is that, when you use a digital device you are basically using an application.

People play Media Files using Windows Media Player or VLC Media Player, surf internet using Mozilla Firefox or Google Chrome. Many programmer like us use the following commands day by day,

gcc
g++
javac
java
python
rustc
./a.out

When asked, how does all these work, many can answer it while most cannot answer.

As a programmer, we should have a basic understanding of the system. If you are not a programmer also, you should know this for your own knowledge.

So, let's start with the discussion.

Whenever we issue any command or start running any application (like Windows Media Player) we see the work is being done.

Who does the work for you ?
Well the box sitting in front of you known as computer.

May be you have opened your box and have seen that there are some boards embedded into it with lots of pins exposed, some soldered silicon chips(looking more like the memory chip you have in your mobile), some chips are blue, some are green, some yellow and mostly black. You have seen a lot of wires plugged here and there. Something like the following,

Image Courtesy: Wikipedia.

Now, you think, how does all these wires and silicon chips understand what we want to do !

Puzzled and again screwed your box and went back to do something else.

Now, here is the answer.
The computer never understands what you want to do. Neither it understands, 'A, B, C, D, E'.

Then ?
A computer only understands 0 and 1. Yes, you read it correctly, only 0 and 1. Or in a very simple way, if a switch is turned on or turned off.

Then, how am I reading this article ?
It is full of alphabets and not 0 & 1. Nowhere it is showing any switch as well.
Well, it is true that you are reading this article which is in English but still I'll say that computer does not understand English.

Well, no more confusion, let me tell you how does it do that.

There are certain applications built for that and those are the production of programmers. These programs are responsible for converting all the 0s and 1s to English and vice versa.

Now let me take you to a visual experience of the whole end to end process.

When using any device, we just see it as a device and it takes our instruction and works for us. But in reality the process is not so simple, in fact this process of instructing your device encompasses many more things in it.

If you look into the diagram, you can understand that there are 3 components involved in it, Applications, Operating System and Hardware.

Let me go through each of them in detail,
  1. Application: The interface between the user and the computer. The user provides input to the application and the application takes it forward to the Operating System.
  2. Operating System: Operating System is nothing but a set of applications that can actually talk to the hardware directly. These applications are particularly known as kernels which actually deal with the hardware.
  3. Hardware: The actual worker. The hardware deals with the instructions Operating System (to be precise, kernels) has given to it.
The hardware performs the instruction and sends the result back to the Operating System and it correspondingly sends back the output to the application.which actually renders the result for the user.

Well, this is a 10,000 ft view of what goes on in the computer. Actually there are many processes involved in it.

Now, you get a clear picture of how things are dealt in computer world.

Now, let me take the opportunity to answer the question we started with.

What is UNIX ?
Well, UNIX is an Operating System built in 1970 at Bell Labs research Center by Ken Thompson, Dennis Ritchie and others.

Actually, from then it has evolved a lot much and many more Operating Systems have been developed which behaves like UNIX. One of them is Linux, written by Linus Torvalds in 1991. Linux is nowadays the ruling OS spread worldwide.

If you are interested, you can go through the following articles.

Many operating systems are influenced by UNIX and we have many distributions of Unix available.

You may be wondering, why take the pain to learn about it ?
Let me clear your thoughts,
  • An OS by developers for developers
  • Better hardware management
  • Improved productivity
  • Gaining the knowledge on OS Model
  • Unix is run mostly on the deployment servers. Almost 99% servers are UNIX
  • All the major websites(including Google, Facebook, Amazon) today use UNIX for hosting
  • If you are a developer, most probably your application is also going to be hosted in Unix
  • A lion share of people deal with devices which run Android/iOS which are Unixy in nature
  • Unix-Like OS Linux is open source and has a great community support. So, after some days working with Linux you may also change your basic OS from Windows to Linux. This is what happened when I started my first Linux usage in my 5th Semester back in 2009.
Those were some of the points many people discuss. Now let me tell you my own story where a bit  of Unix knowledge helped our team.

Few years ago, we were working in a very critical memory savvy project and we had to fix all the memory leakage in the application. Our team really did a great job and we fixed all the areas where memory leak can occur. The application was built and deployed. The first deployment failed and the System Administrator blamed us that we did not fix anything and simply put the old code in deployment. We again checked every suspected area and deployed, the same thing happened and the same thing repeated four to five times.

This time we were there with the SA while deploying and we found that he was deploying 10 applications in a single go in the server and it was throwing error of insufficient buffer. We realized that only seeing a memory issue, he blamed our team, not even looking into the root cause of Deployment Failure.

Our team checked the system log line by line and found that it was not our application which was creating problem rather it was the procedure of deployment.

I still remember the day when the architecture team praised our team for finding out the real deployment issue.

If our team members could not find the Unix Administration issues, we would still be blamed for not fixing the memory leaks.

That day I realized that developers should also learn Unix at least to such a level that it helps you find out the systems errors and the System Administrators can not blame you without proper reason.

Now with all that, we should be looking into the distribution/version we should use for our learning purpose.

As mentioned above, Unix has many versions of it while some Unix-Like Operating Systems like Linux has different distributions.

So, the question arises which version to use and how to use it ?
Each distribution of Unix/Linux is meant for a specific reason and keeping specific goals in mind. But in all these, one thing is common that is the shell.
When we want to explore the basics of UNIX, we will mostly look into the details of Shell commands and different system configurations through shell.
In many cases people want to write some programs dealing with different areas of the Operating Systems like File System, Networking etc.

So, ideally for this series any Unix/Linux distribution will work fine.

But if you are very new to Unix/Linux, you can start with Ubuntu 14.04. This OS has a faster response, better GUI, easier set-up and almost Windows Like GUI.

In my case, I will work with both Ubuntu 14.04 and FreeBSD 10.2. If you are using other distributions, chances are some commands need a small change. Which you can get help from the particular distribution community.

Now you may want to know how you can use either of these. I will try to provide some suggestions.

Caution: Instructions below are dangerous to try without proper knowledge and you may loose all your data if done incorrectly. So, if you do not know how to format your PC, don't try these. Instead, ask someone for help.

Download and Install

Choose any open source version of Unix/Linux distribution. Here is the link for top 10 Distribuiton List. Once you are done with the selection, download the distribution and burn the content to a DVD and boot your system with the DVD. As an alternative, if you BIOS supports booting from Pen Drive, you can also make your Pen Drive bootable and boot with it.

Once you are in, you may or may not install it. You can simply try the Distribution and on your choice can install it or simply get out of it without making any change to your system.

Try inside a VM Box

If you are not in the mode of installing the whole system on your PC and remove everything else or you want to try different flavors of Unix/Linux, you may use this option. Only drawback of this is, it requires high Hard Disk space.

Same goes here for choosing the distribution and download.
Now, you need to work with a VM Box. You can use VMWare Player or Oracle VM Virtual Box for this purpose.

Please do not use these tools for commercial purposes. They are meant to be used personally.

Here are links,
  1. https://www.virtualbox.org/
  2. https://www.vmware.com/products/player
You can try both and according to your choice, you can keep either one or both. They both work together without hampering your base system.

Now install the OS inside the VM Box with downloaded image of UNIX/Linux Distribution.

Now, that's all about UNIX/Linux.
  1. You can now understand what UNIX is and what is its use.
  2. You surely have understood that Linux is not UNIX.
  3. You may have got an idea how many devices are Unixy in nature.
  4. You also know what are the distributions available of Unix/Linux.
  5. You may have chosen your distribution.
  6. You know how to use Unix/Linux. Another reminder, if you are not aware of system format, please ask for help from your friends or any computer vendor.
  7. May be you are planning for Virtual Box installation of the Unix/Linux Distribution.
Hope that, you enjoyed this article...

The basics of UNIX

With our previous post, we knew that UNIX is an operating system which was developed in and Bells Labs back in 1970 and after that many versions of UNIX have become available and some UNIX Like Operating Systems like Linux has came into picture. In the time line Linux has also gained popularity and most of the people today use Linux whether they know it or not.

We have taken look on the big picture. Now, we'll be looking into the basics of the Operating System.

UNIX/Linux has the following programs at its bare minimum,

Kernel

The programs that deals with the hardware directly. This is the main building block of the OS. These basically allocates time and memory of other programs. Also deals with low level file structure and communication related works.

Shell

Shell can be thought of a bridge between the Kernel and the user. It is a Comman Line Interpreter (CLI). When user types in any command in the Shell, the shell finds the exact program that deals with the command and invokes the kernel.

There are many versions of shell available. Each comes with its own features.
To name a few different Unix/Linux shells,
  • B Shell
  • C Shell
  • K Shell
  • Bash Shell
  • tcsh
  • Zsh
Don't worry, if you don't know which shell you are using, if yo are defining anything by your own, you will be provided with the system default. Apart from the features a shell provides, it does the basic thing, interpret the command you key in. So, you are safe with any shell. Once you know the basic commands and shell programming, you wil find the best suit for you.

File and Processes

Well, in UNIX everything can be categorized in either of these types, File/Process.
A file is a collection of data. As I mention data, it may be anything, a document, a C program, a media file, an executable. Files are arranged in directories.

Each file has a name associated with it which works as an unique identifier for the file within a directory. Similar goes for a directory, within a single parent directory, two sub-directories cannot have same name.

Now coming to process, a process is a program that is being executed in the hardware. A process has also a unique identifier for it and is known as PID (Proces Identifier). We can perform many operations by our own with this PID.

Directories

As we've known that files are arranged into directories and in the similar fashion, directories can be again arranged into directories. The hierarchy starts usually with root directory and is usually marked with front slash (/). If we think this a graph, then directories are the node where files are the leaf nodes of the graph.
Let's look at a directory hierarchy,
In this hierarchy, we can see that root directory has two directories bin and home(There are many more directories present in the root. This is just for demonstration purpose. Don't get confused.). So, bin and home are sibling. In home directory, we see another two directories palash and Anyone. Similarly, in palash we've git and within that we've me and within me we find a file pom.xml. So, except pom.xml all others are directories and bin & home are sibling while palash & Anyone are sibling.

Now, let's look into the running processes. To see all the running processes, issue the following command in the shell, To open a shell press Ctrl+Alt+T. This is the default key combination for opening a terminal.

ps -ef

You will see the output similar to the following,


UID         PID   PPID  C STIME TTY          TIME CMD
root 1 0 0 Feb23 ? 00:00:19 /sbin/init
root 2 0 0 Feb23 ? 00:00:00 [kthreadd]
root 3 2 0 Feb23 ? 00:00:04 [ksoftirqd/0]
root 4 2 0 Feb23 ? 00:00:00 [kworker/0:0]
root 5 2 0 Feb23 ? 00:00:00 [kworker/0:0H]
root 7 2 0 Feb23 ? 00:00:32 [rcu_sched]
root 8 2 0 Feb23 ? 00:00:00 [rcu_bh]
root 9 2 0 Feb23 ? 00:00:28 [rcuos/0]
root 10 2 0 Feb23 ? 00:00:00 [rcuob/0]
root 11 2 0 Feb23 ? 00:00:01 [migration/0]
root 12 2 0 Feb23 ? 00:00:00 [watchdog/0]
root 13 2 0 Feb23 ? 00:00:00 [watchdog/1]
root 14 2 0 Feb23 ? 00:00:00 [migration/1]
root 15 2 0 Feb23 ? 00:00:03 [ksoftirqd/1]
root 17 2 0 Feb23 ? 00:00:00 [kworker/1:0H]
root 18 2 0 Feb23 ? 00:00:06 [rcuos/1]
root 19 2 0 Feb23 ? 00:00:00 [rcuob/1]
root 20 2 0 Feb23 ? 00:00:00 [khelper]
root 21 2 0 Feb23 ? 00:00:00 [kdevtmpfs]
root 22 2 0 Feb23 ? 00:00:00 [netns]
root 23 2 0 Feb23 ? 00:00:00 [perf]
root 24 2 0 Feb23 ? 00:00:00 [khungtaskd]
root 25 2 0 Feb23 ? 00:00:00 [writeback]
root 26 2 0 Feb23 ? 00:00:00 [ksmd]
root 27 2 0 Feb23 ? 00:00:19 [khugepaged]
root 28 2 0 Feb23 ? 00:00:00 [crypto]
root 29 2 0 Feb23 ? 00:00:00 [kintegrityd]
root 30 2 0 Feb23 ? 00:00:00 [bioset]
root 31 2 0 Feb23 ? 00:00:00 [kblockd]
root 32 2 0 Feb23 ? 00:00:00 [ata_sff]
root 33 2 0 Feb23 ? 00:00:00 [md]
root 34 2 0 Feb23 ? 00:00:00 [devfreq_wq]
root 36 2 0 Feb23 ? 00:00:07 [kworker/1:1]
root 38 2 0 Feb23 ? 00:00:00 [kswapd0]
root 39 2 0 Feb23 ? 00:00:00 [fsnotify_mark]
root 40 2 0 Feb23 ? 00:00:00 [ecryptfs-kthrea]
root 52 2 0 Feb23 ? 00:00:00 [kthrotld]
root 53 2 0 Feb23 ? 00:00:00 [acpi_thermal_pm]
root 54 2 0 Feb23 ? 00:00:00 [scsi_eh_0]
root 55 2 0 Feb23 ? 00:00:00 [scsi_tmf_0]
root 56 2 0 Feb23 ? 00:00:00 [scsi_eh_1]
root 57 2 0 Feb23 ? 00:00:00 [scsi_tmf_1]
root 59 2 0 Feb23 ? 00:00:29 [kworker/0:1]
root 63 2 0 Feb23 ? 00:00:00 [ipv6_addrconf]
root 84 2 0 Feb23 ? 00:00:00 [deferwq]
root 85 2 0 Feb23 ? 00:00:00 [charger_manager]
root 139 2 0 Feb23 ? 00:00:00 [mpt_poll_0]
root 140 2 0 Feb23 ? 00:00:00 [mpt/0]
root 141 2 0 Feb23 ? 00:00:00 [kpsmoused]
root 142 2 0 Feb23 ? 00:00:00 [kworker/1:2]
root 143 2 0 Feb23 ? 00:00:00 [scsi_eh_2]
root 144 2 0 Feb23 ? 00:00:00 [scsi_tmf_2]
root 146 2 0 Feb23 ? 00:00:00 [scsi_eh_3]
root 147 2 0 Feb23 ? 00:00:00 [scsi_tmf_3]
root 148 2 0 Feb23 ? 00:00:00 [scsi_eh_4]
root 149 2 0 Feb23 ? 00:00:00 [scsi_tmf_4]
root 150 2 0 Feb23 ? 00:00:00 [scsi_eh_5]

Well, these are some processes running in my system. This list may vary from system to system. Also the output may vary depending on the system you are working.
For example, this output was taken from Ubuntu 14.04 while in FreeBSD you will get some different output. But one thing will be common in all the output, PID. This shows the system generated identifier for the process.

That's all what I can tell you for now about the very much basics of UNIX/Linux.

Happy learning...

Terminal, Console, Shell, Kernel, Commands - Different parts of a Computer

We somewhat interchange the terms Console, Terminal, Shell. Also, in contexts, we come across the word Kernel. So, this creates a confusion while first starting with Unix-like Operating systems. In this article, we will start digging into it to understand, what they mean.

To really know the meaning of all these, you have to go back to the era of computers sized as a room. Otherwise, it’s not possible for you to get it. As technology evolved and advanced day by day, these all came into a single place. Because, in today’s world all these are in a single box and you cannot really differentiate much. But remember, all these work independently still today, all these are still different parts in today’s world as well. Just, the difference is, they all live in the same place as opposed to the earlier when they used to be one single component of their own.
Enough intro, let’s start differentiating.
First I will go through the Kernel and Shell.
You must have seen an walnut, which looks something like this,


What is the main part of it ?
You might throw me something like, “Obviously, the inner part of it. Come on, is it a question to ask at all ?”
You know, what it is called, right ?
Yes, they are known as Kernel and what is the part called which wraps the kernel ?
You might connect the dots right now. Yes, it is known as Shell.

Yes, there is the connection of these two. So, the outer cover of Kernel is Shell, where Kernel is the important core part of the Operating System which directly talks to the hardware. So, shell is what connects the kernel to the user. It all dates back to 1960’s when computers used to do some specific tasks and each computer was different in their own flovors and working on one computer was not enough to certify you as a computer genius. Because, to work on different computers, you had to know them differently as well.

But in 1970’s in Bell Labs some really genius people (Ken Thompson - Wikipedia, Dennis Ritchie - Wikipedia) mostly noted for their contribution) started thinking of a solution of this problem and they thought of a process of minimising the stuffs and distinguished each part of a computer. They found that, only a smaller part of the computer systems needs to be changed to work differently for different computers and all other stuffs can be ported to any other computers without much hassle. That’s where the concept of kernel comes in. Kernel is actually that part which needs to be different for each computer architecture and other stuffs can be same for each of them. Shell is one of those programs which can be similar for all kernels running on different computers.

So, that was somewhat on Shell and Kernel. Now, let’s look into the console and terminal part.
After devising the great mechanism for unifying all the computers under a single operating system also, did not resolve every problem. It was just a part of it. You remember, there is still the risk of having big computers, known as mainframes., which looked something like the following -



The above image is indeed a great picture of Ken Thompson and Dennis Ritchie working on mainframes.

This was how it looked at that time. So, you can understand that, it was fairly impossible to accommodate this really huge machine in each place. So, it was a practice to have a system in a central place and that system was communicated to multiple other smaller systems with almost no processing capacity. These smaller machines are those where users used to log in and connect to the main system (also known as Mainframe). This smaller machine was known as Terminal, which looked like following -




The terminals used to connect to the Mainframes using telephone connections. Maybe, you have heard the term Dial Up Connection.

Now, let’s talk about the console. Console is a special type terminal usually directly attached to the Mainframe for administration purposes. You may understand why the architecture is like that. If for any reason, the Mainframe fails to start up or may be the network connections cannot be established by the terminals to the mainframes, there should be some way to control the machine or to find out the error. That’s where the console comes to picture.



Nowadays, all these pieces of different hardware are fused into a single piece and hundred times smaller than the original. So, basically, you can see them co-exist. So, basically we interchange them nowadays. But what you see in modern computers, is what should be correctly known as Terminal Emulator.

So, these are the basic differences of a computer system in a lower level. Now, let’s step forward and look at higher level. An application is a program based on shell. Any program does not directly interact with Kernel, it usually talks to shell instead and shell executes required kernel instructions. Here comes the concept of Command Line Interface. So, basically, it means that, as a user, you will provide the computer some text based instructions (known as commands) via terminal. And it will connect to the mainframe via shell. In the mainframe, we have shell which talks to the kernel and kernel executes necessary hardware operations and in turn will return text based output on the terminal via shell. These commands are nothing but application programs. When commands are sent to shells, it will decide which application to run for the particular program. In turn applications also use shell to execute the required kernel instruction. It may be surprising to know that all the graphical user interface (e.g. Windows, Mac OS, Linux Graphical Desktop etc.) actually works in the similar model and connect to shell. We'll see how it is done later in this series.

Now, we can draw a big picture -

Getting Started with Command Line

Hopefully it's definitely clear to you, what Unix and Linux is and how today it is. At the same time you can have an idea of the terminal, console and shell etc. So you understand what the command line is or what it does. Now, we will start the command line and type some commands to see how the command line works. I assume that you are brand new to the shell or terminal, so maybe few start-up posts will be at the preliminary level; If you have experience with Unix, Linux or Shell, you might think some stuffs very obvious and it needs no mention.

Typically , the terminal emulator is opened by pressing these three keys together in Linux Mint or Ubuntu: Ctrl-Alt -t, on other Linux-based systems, the Terminal Emulator can be opened from the System menu. Below is a picture of my current terminal -

Terminal usually provides some information, such as the user who has logged in, the computer name where the user has logged in, in which directory. After that there is a $ , % or # sign. Generally, most Linux has GNU BASH default, so it is possible that you will also see $ . If a root user logs in, then the # symbol is seen. For example -

Generally, the root user logs in for administrative activities and the root user is unnecessary for day-to-day activities, and as far as possible , it is best to avoid root user login, we will gradually learn about this. 

When you see $ or % or #, then you understand that Linux is waiting for your command. Generally these symbols are called prompt. Now we will see how to run commands. 
We'll start with the most common command date that shows us today's date. By typing date in the terminal and pressing Enter  , we will see the output. Typically for all commands user has to type it and then press enter to see the output.
Totan @ Home-Computer ~ $ date 
Sat 8 Jul 18:33:28 IST 2017
totan @ Home-Computer ~ $

The next command cal  will show our current calendar month,
Totan @ Home-Computer ~ $ cal 
July 2017
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
totan @ Home-Computer ~ $

Generally, the current day is highlighted, you can understand from the following picture - 


If you want to know which users have logged in at the same time, then you have to enter the command who
Totan @ home-computer ~ $ who 
totan tty8 2017-07-08 17:55 (: 0)
totan @ Home-computer ~ $

For example, my system now only has one user logged in, so the current user is showing in the system along with information like which terminal, at what time etc. If only you need to know the current user's information, you can use who -u
Totan @ home-computer ~ $ who -u 
totan tty8 2017-07-08 17:55 01:13 1864 (: 0)
totan @ Home-computer ~ $

Since only one user logged in on my system, who and who -u are showing the same information. 
Now if we want to know which shell program you are using, then use echo $0
Totan @ home-computer ~ $ echo $0 
bash
totem @ home-computer ~ $

My system is using bash, if another shell is used, then we could see the shell program name. For example -
$  echo  $0 
sh
$

Home-Computer: ~%  echo  $0 
csh
Home-Computer: ~%

Home-Computer: ~> echo  $ 0
tcsh
Home-Computer: ~>

Totan @ home-computer ~% echo  $ 0
zsh
Totan @ home-computer ~%

$ Echo  $ 0
mksh
$

Each shell has some features, so users usually develops preference on one of the available shells. However, since the majority of Linux uses GNU BASH as the default shell, BASH is very popular. 

In this way, after running some commands, the screen is usually filled up, the old commands go out of the screen and the new commands start in the last of the screen -

To solve this problem, you can use the clear command so that you can see the cleaner screen omitting all the old commands, where again you can run new commands -
This way , after clearing the screen you will not see the old command. 

Now let's see, what happens when a command can not be recognized by the Linux shell -
Totan @ home-computer ~ $ ckwar
Ckwar: command not found
Totan @ home-computer ~ $

When there is some wrong command entry in Linux Shell, there is a message in shell that the entered command is wrong and in many cases the shell can give you some advice, for example, we have run datw incorrectly while typing date command -
Totan @ home-computer ~ $ datw
No command 'datw' found, did you mean:
Command 'dat' from package 'liballegro4-dev' ( universe )
Command 'date' from package 'coreutils' ( main )
datw: command not found
Totan @ home-computer ~ $

In this case, the terminal advises that you might want to run another command (in this case dat or  date ) but due to typing mistakes you have run another command. In many cases this message can be helpful for typing right command, for example in our case we will run the date command. 
One should always keep in mind that UNIX/Linux is case sensitive, that is, the commands should be small or capital letter. Linux can not recognize it if it runs DATE or date or dAte instead of date, so if you get an error message you should first check the case of the command.
Totan @ home-computer ~ $ DATE
DATE: command not found
Totan @ home-computer ~ $ dAte
No command 'dAte' found, did you mean:
Command 'date' from package 'coreutils' ( main )
dAte: command not found
Totan @ home-computer ~ $ Date
No command 'Date' found, did you mean:
Command 'date' from package 'coreutils' ( main )
Command 'late' from package 'late' ( universe )
Command 'kate' from package 'kate' ( universe )
Command 'yate' from package 'yate' ( universe )
Date: Command not found
Totan @ home-computer ~ $

Now let's learn some keyboard shortcuts. Linux usually saves the commands you have run. So you can see the previous commands by pressing the up/down arrow to run again the commands that you have run earlier. Now suppose you have to change some command or you have run a wrong command and you have to change it and run again, then press up/down arrow to bring the required command to the prompt and press left/right arrow to make necessary changes and you can execute the command. Usually, all the new keyboards have their own shortcuts, but we can know some shortcut, which is useful at times -

  • Press Ctrl-M to Enter.
  • Suppose if you are typing a command and you notice that a wrong character has been pressed somewhere in the command, then you need to press left / right arrow to move the cursor and press backspace or Ctrl - h to remove the character and delete the correct character and press Enter to run the command.
  • Suppose you typed a big command and maybe typing mistakes are in many places, then you feel like removing the whole line is better than typing, press Ctrl-u to delete the whole line together instead of pressing Ctrl-h repeatedly.
Another way, you can see all the old commands by typing history. history will show you the commands you have run one by one, you can copy/paste as needed . Normally you can select the text with the mouse in the terminal. Usually, if you select something with a mouse in the terminal, the text gets copied and right click will automatically paste the copied text. But if this shortcut does not work, then in most terminal emulators, select a text with the mouse and   press Ctrl - Shift - c , the text is copied and press Ctrl - Shift - V to paste the text into the cursor position.

Daily Practice is the key to overcome the fear of Command Line and to earn skills. So these simple commands need to be run repeatedly, so that the concept of terminal emulator is clear and there is no difficulty in making the necessary changes according to the terminal.
When all your commands are run and you want to close the terminal, you will need to exit the terminal by typing the exit command and pressing Ctrl - m (or Enter).
Totan @ home-computer ~ $ exit