Saturday, 15 July 2017

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.


[Image Courtesy: Computer console - Wikipedia]

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

Another step in the command line - arithmetic

After running some commands in the command line, you are now confident, at least now the command line or shell does not bother you much. Now we'll forward one more step in our journey. We will now learn something which is less likely to mess the operating system.

We'll learn to calculate in the command line. Generally, any operating system now has a graphical calculator installed, so most people do not show any enthusiasm about command line calculators. But from my experience, I can say that it is proven to be quite useful and it is irritating to use the graphical calculator while working on the command line, so at least I think it is better to know about the command line calculator. Moreover, the simplest way to get used to the command line is to  use command line calculator
Generally, all Unix / Linux based operating systems have bc by default, so we will use it for our work. Once you are used to it, you can choose another command line calculator. Let's see how it looks like -

When bc is run, it often provides information about itself, sometimes it does not provide any information and only shows the prompt . We will now see how to add subtract multiply and divide -
totan@home-computer~$ bc
Bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
Details For type  ` warranty ' .
78
78
98 + 75
173
45-3
42
22.12 + 14.98
37.10
22.47-5.6
16.87
7 * 5
35
3/2
1

If you look carefully, the first thing that you can see is the pattern of input / output. In the first line of bc you will give your input and bc will show you the output on the next line . You can see that adding and subtracting is very easy. But if you are not an existing computer user, then multiplication symbol might look a little weird to you. In fact, this symbol is called asterisk, and it is used in mathematical terms as multiplication symbol. 
Everything seems perfect except the division, what happened to it ? 

While performing 3/2, shell seemed to be wrong. What a bad calculator!

Actually, shell did just what we asked it to do. In fact, shell showed us the whole part of the division result (quotient) but it did not show the decimal part. If we want to know the remainder, then we have to use % -
3%2
1

In this case bc does not show the quotient, just the remainder. That means % performs the modulus operation. We will try to understand the matter with a few more examples -
10/3
3

10%3
1

12/5
2

12%5
2

12/6
2

12%6
0

13/6
2

13%6
1

15/7
2

15%7
1

10 divided by 3 quotient is 3 and the remainder is 1; 10/3 and 10% 3 respectively show the value 
12 divided by 5 quotient is 2 and the remainder is 2; 12/5 and 12% 5 respectively show the value 
12 divided by 6 quotient is 2 and the remainder is 0; 12/6 and 12% 6 respectively show that value 
divided by 13 is 6 quotient is 2 and the remainder is 1; 13/6 and 13% 6 respectively showed that value 
divided by 15 to 7 quotient is 2 and the remainder is 1; 15/7 and 15% 7 showed that value as well

One more thing to notice, to keep the screen clean and easy to read, I have now pressed Ctrl - m or Enter after each calculation, which means there is one blank line in between the calculations for better readability

Many people seemingly peek at this question, can't we divide in fractions ? 
Sure you can do that, you just have to tell shell the number of decimal places you want to see in the fraction part. For that you have to write a small program, much like this -
Scale = 3;

3/2
1.500

4/3
1.333

10/9
1.111

3/7
.428

22/7
3.142

2/3
.666

6/9
.666

Scale = 4;

4/3
1.3333

2/3
.6666

10/9
1.1111

3/7
.4285


Scale = 34;

22/7
3.1428571428571428571428571428571428

4/3
1.3333333333333333333333333333333333

10/9
1.1111111111111111111111111111111111

Before throwing all your queries at me, congratulate yourself as you have written the first program in Shell. 
Now I'll answer your question, yes you wrote a very short one-line program and not once, many times (to be exact, 3 times). Well, you probably did not notice in the excitement of using bc, you wrote  scale = 3; in the shell and shell answered you up to 3 decimal places, similarly you wrote scale = 4; And  scale 34; and shell showed upto 4 and 34 decimal places. In fact when bc is run the value of scale is 0 or programmatically we can say scale = 0; so, when 3/2 is run, it does not show any answer to decimal places. So we wrote the program and changed the value of scale and shell showed us results upto that much decimal place. The same way you can change the scale to the different decimal places, see the fraction or see the quotient and remainder in further calculations.

Now is the time for some shortcuts. You can see the previous commands as you press the up / down arrow and you can run it again as needed, much like you did in shell. But remember by typing exit, you can not get out of bc; to exit from bc you have to type quit and press Ctrl - mAnother way to exit the bc is  press Ctrl-d. Ctrl-d indicates end of input or no input is left, so bc is turned off. Other than bc,  Ctrl-d is also used in another shell commands, we will discuss about them later. By using bc as your calculator, you will gradually be getting used to the command line. So, we will see, what else is possible with the help of bc .

Command Line Calculation - Phase II

In our last post, we have seen how to add, subtract, multiply and divide numbers in command line, and now one step further, here we will learn how to make another type of calculation in command line. 

Let us now see how we can evaluate the square root of a number in the command line -

Again wrong answer? 
Think a little 

This time you've to understand what you have to do. If you do not understand, then think before reading the next line, think about whether you can remember something. 



Well, many people have understood this, maybe some have even taken the right path to see the exact answer in command line. 
We have to provide the value of scale -
Scale = 30
Sqrt ( 2 )
1.414213562373095048801688724209

You may have noticed, I have removed the ; after  scale = 30 and still it works, so ; is optional in bc

Now we will take a look on how we can evaluate the value of a number raised to different power -
2^2
4

1^1
1

3^3
27

4^4
256

6^6
46656

6^2
36

9.2^2
84.6

15.7^2
246.4

15.7*15.7
246.4

Scale=2;

15.7*15.7
246.49

15.7^2
246.49

15.7^3
3869.89

Scale=3;

15.7^3
3869.893

At this time you have surely understood that by setting different values ​​of scale, we are seeing the answers in different decimal places. 

Many times it may happen that, shell does not show the proper result as what we're trying to calculate. Shell is actually calculating exactly what it is supposed to do but we just don't know how to convey it properly as the below example illustrates -
3^3^3
7625597484987

After seeing this answer, you might think, what happened, I thought 3 ^ 3 ^ 3 would be 19683, but why did shell show 7625597484987?
Actually no mistake from shell, shell just did what it is supposed to do. But there is a little lack of synchronisation between you and shell. These kinds of problems are often encountered, so we will see how to get rid of such problems -
3 ^ ( 3 ^ 3 )
7625597484987

( 3 ^ 3 ) ^ 3
19683

In the first case it is being seen as - 3 ^ ( 3 ^ 3 ) = 3 ^ (27) =  7625597484987 
and in the second case -  ( 3 ^ 3 ) ^ 3 = 27 ^ 3 = 19683. That is, by using parentheses, we change the sequence of execution. If you remove parentheses, both are essentially the same 3^3^3, isn't it ?

Let's look at some more and it will be clear to us -
Scale = 5;

7 + 6 * 5
37

7+ ( 6 * 5 )
37

( 7 + 6 ) * 5
65

1/2 + 8 * 9
72.50000

1 / ( 2 + 8 ) * 9
.90000

1 / ( 2 + 8 * 9 )
.01351

By using braces in this way we can accomplish a bigger and more complex calculation in one step. With some examples we can see -
7 + 6 * ( 5 + 5 * ( 12 ^ 3 ) + 19 * 5 ^ ( 2 * 3 ) + 6 * ( 3 ^ 2 ) ^ 9 ) / 9 + 78
1549885721

( 7 * 9 ) + 2.2 ^ 3 + 4.5 * 8 * ( 8 + 9.2 ^ 2 + 5 ) + 10 * 10 + 91 + 2 ^ 4
3795.688

So far we have come to know the method of basic calculations of all kinds. Hopefully, now your fear about the Command Line have been cut short and you can work on command line more comfortably than before. We will go back to shell again to learn other commands in the Command Line. 

For those who liked bc and want to know more about bc, I have described the method of calculating some more scientific stuffs in a mini series, willing readers can head to - bc - scientific calculation