Monday, March 2, 2015

Basic Unix Stuffs



1) What is a shell?  Name the different types of shell

Shell is a command line interpreter. It's an interface that sits between the user and the hardware.

The different types of shells are

a) C shell ( csh) ---> Linux
b) Bash shell ( bash) -------> Linux
c)  Bourne shell ( sh) -------> Unix/Solaris
d) T Shell (Tcsh)
e) Korn Shell (ksh) ----------> AIX
f) Z shell ( zsh)




2) What is the command to know your current shell?

Echo $SHELL




3) How do you locate a unix command's executable i.e how do you locate a command?

"TYPE" command.
If you have to find the executable or location to see where is 'ls' command located, then give the below command

type 'ls'
result is : ls is /bin/ls



4) Where are most of the unix commands stored?

/bin and /usr/bin directories




5) When you give a command on a shell prompt, where all places does the shell usually searches to find the location of the command?

/bin/  
/usr/bin
/usr/local/bin
/usr/local/java/bin




6) What is a PATH and how to find it?

The sequence of directories that a shell searches to look for a command.
The command to find the PATH is  echo $PATH




7) What is an Internal command?

They are shell-built in commands.
Internal command are not present in /bin or /usr/bin directory. Even if they are present in these directories, they won't be executed.
Example : Echo, PWD, CD




8) What is a External command?

External commands are not shell built-in.
They are executed by /bin and /usr/bin

Example : LS, DATE, MV, CP




9) Is shell an external command or Internal command?

Shell is an external command with its own set of internal commands. ie Shell has its executable shown up when you give echo $PATH. However, it does have specific internal commands within it as well.




10) Does Internal command have more precedence than external command?

Yes. Internal command has more precedence( more priority).  Example : Echo command is both internal and external command.
When a user gives echo command on the prompt, the shell executes "ECHO" from its own internal commands rather than executing the "ECHO" from /bin directory.





11) When you give ls -Z "filename", what happens? Explain

When a user gives ls -Z "filename", the shell interprets and sees that the command is incorrect.
The command then displays an error that ls : illegal option -Z. 
This message comes from the command and not the shell. The command LS recognizes that -Z is not one of it's valid options.

Another example is

$ ls-l
bash:  ls-l: command not found.

when giving options, we need to make sure that space is given between ls and -l



12) What is a command line?

Command line is a command with arguments and options

example :   ls -ltr filename

ls -ltr filename ------> The whole command
ltr ---------------------> option
filename --------------> argument


Example 2

 ls --all   -------------------->>> you can also two hypens(--)

The options can also be (-) or (+) or (=!)

The difference between an argument and option is that an option is also an argument but used with (-) hyphen.


13) List one command that doesn't accept any arguments and options?

PWD ( Print working Directory)




14) How can one combine two or more unix commands and redirect the output to a file?

Combining two commands can be done by piping and output can be redirected via tee command or redirection operator

Example

( ls -ltr filename.txt | wc -l ) > output.txt


Explaination

Filename.txt is the input file which is listed in the latest modified time. i.e ls -ltr filename.txt.  
The output of this file is given as an input to the command wc -l. 
This is possible by using the pipe operator (|). The pipe operator combines two commands. This command gives the number of lines from the filename.txt.
The final output is then redirected to a new fresh file which gets created by name "output.txt".

The overall advantage is that you don't have to type three commands one by one and instead can get the task done in one line command.




15) What is MAN Command?

Man command stands for manual documentation in Unix system. UNIX OS has thousands of commands and there is a manual for each and every command with detailed description.

If you want to know what is the manual for Date command. Give " $ man date".

It will list out

NAME  -----> One line description of the command
SYNOPSIS ----->> Syntax of the command
DESCRIPTION ------>>> Detailed information of the command.
Options
OPERANDS
USAGE
EXIT Status




16)  How does MAN Command work internally?

When user gives man wc, the output is displayed to the console. This is possible by sending the output of WC manual pages to a program called "PAGER".

PAGER is a program is also a command. MAN always works with either one of the below PAGER programs

a) MORE
b) LESS  ----- >  Its more powerful than "MORE" command.


Inorder to navigate through MAN pages, press "f" or spacebar to move forward one page at a time.
Use "b" key to move pages backward.

To search for keyword within Man pages output, use the "/" followed by search string and press enter
i.e  /searchpattern

To repeat the search, use 'n'

To quit Man pages, press 'q'


17) How to know the default PAGER value and how to set it to a different value?

$ echo $PAGER.  -------> Command to know the default value of your PAGER

To set a pager to your desired settings,

$ PAGER = less/more -----> Choose if you want less or more
$ export PAGER ;     --------->  This is only temporary for the session

Now type $PAGER and see if it shows the set value


18) I have given $ echo PAGER, but it doesn't show any value.  Why is it?

It means the man page on your system is using default pager i.e /etc/default/man.


19) What is MAN -K command ? 

Man doesn't use any options. The only exception is the -k option. When a user gives man -k, the man searches the summary and prints a one-line description of the command.

Example :

$ man -k awk

awk       awk (1)    -  pattern scanning and processing language




20)  What is APROPOS command?

Apropos command lists the files and commands associated with a command.

Example

$ apropos FTP

ftp                             ftp(1)             --------file transfer program
ftpd                           ftpd  ------------------- FTP daemon


The alternate to APROPOS command is "whatis" command.

example $whatis FTP

ftp                             ftp(1)             --------file transfer program



21) What is the command to erase text from your terminal?

[cntrl-h] or [cntrl-w]


22) What is the command to interrupt a command ?

[cntrl-c] or DELETE


23) What is the command to terminate a login session or a programme that expects a input?

[cntrl-d]


24) What is the command to stop scrolling the screen & lock the keyboard and how to resume it?

[cntrl-s]   ------------------------------->  Stop Scrolling

[cntrl-q] ---------------------------------> Resumes scrolling


25) What is the command to kill a command without executing it ?

[cntrl-u]


26) How do you kill a running command? What happens after you execute the command?

[cntrl-\].  You will get a core file created.


27) How do you suspend a process and return to shell prompt?

[cntrl-z] ------     Suspend a process

fg           ------>   to return to shell prompt



28) How to restore the computer terminal?

Enter the command "stty sane"




Sunday, February 23, 2014

Getting Started - Interview Questions - Part 1



1) How do you login to a unix machine with your username and password?

Ans )  If your username is Michael. Then this is how you should login

login : Michael [Enter]
password :



2) What is a shell?

Ans) A Shell is a command line interpreter. Any command you type in will be translated by the shell to the kernel which inturn gives you the output


3) What is the command to display the "DATE" and "TIME"?
Ans )  $ date [ enter ]
           Sun Feb 23 15:41:12 IST 2014


4) What is the command to clear the screen?
Ans )     tput clear

5) What is the CALENDAR in UNIX?

Ans ) Calendar is used to display the days of the year and the dates. To view the output, you need to give
          $  cal

6)  How do you find the USERS in your system?

sdcapc-a31-p:/etc>who -Hu
NAME     LINE         TIME             IDLE          PID COMMENT
vpadmana pts/0        2014-02-09 00:43   .         24527 (192.168.11.252)

7)  What is the UNIX Command to find your USERNAME?

sdcapc-a31-p:/etc>who am i
vpadmana pts/0        2014-02-09 00:43 (192.168.11.252)


8) Write a UNIX command to find your MACHINE name, its operating system, its version?

sdcapc-a31-p:/etc>uname
Linux

sdcapc-a31-p:/etc>uname -r ----------------------------------> To know the Version of the Operating system
2.6.18-274.7.1.el5

sdcapc-a31-p:/etc>uname -n  ------------------------------> To know your hostname
sdcapc-a31-p

sdcapc-a31-p:/etc>hostname     ---------------------------> Alternate command to find the hostname
wysao-a31-p


9)  Write a UNIX command to find the name of your TERMINAL?

sdcapc-a31-p:/etc>tty
/dev/pts/0


10) What is the command to view the processes running in your machine?

$ ps
 
    PID    TTY                                    TIME                CMD
    345    console                               0:00                    ksh


11) How do you list the files?

sdcapc-a31-p:/home/vpadmana>ls

backupAutosysJILStatus.WYSAO.20140120.txt
backupAutosysJIL.WYSAO.20140120.jil
bounceVls.sh.20140117.log
checkForHungVLS.FIN.20140121.log
checkForHungVLS.FIN.20140205.log
checkForHungVLS.HRM.20140205.log
copyFiles.20140117.log
cycleDriverFINPRD_WOLFS_RUN.20140121.txt
cycleJobReport.FINPRD.DAILY.20140120072856.html
cyclelogs
cycleWait.201401181800.log
cyclogs.tar
jobreports
lockAllFINUsers.RELOCK.20140121190608.txt



12) How do logout from your session in UNIX?

Ans)    $ exit
            login :


13) What is the value associated with any character in UNIX system?

ASCII Value


14)  What is the difference between echo * and ls.   What do you think echo did?

Ans ) Echo * will list all the files only if there is a single file in that directory and if that file happens to be a directory.
     
        ls will list the files in the directory.


15) When you enter a UNIX command on shell prompt, what internal action goes through?

When you enter a command, the shell scans the command for any metacharacters. It simplifies the command to a simplified one and then passes the command to the kernel. The kernel looks for the executable in its PATH variable and if found, it executes the command and the output is displayed on to the SHELL.


16) What are the shells available?

C shell, Bash Shell, KSH - Korn Shell, Sh ( bourne shell ), tcsh


Saturday, October 19, 2013

The Fundamentals of Unix


This chapter focuses on understanding the Unix architecture, the features of Unix operating systems and other aspects about Unix.


Unix Architecture

To explain the concepts in the most simplified way, let's take an example of your personal computer running Windows operating system. 

Your personal computer monitor, keyboard, processor, HDD, Memory, Optical drive, USB drive etc constitutes the Hardware. You just can't have only the hardware for functioning. You need a Software too. This software is your operating system ( Windows). The operating system comprises of Kernel, Command interpreter, applications, databases etc.  The kernel is the core of any operating system. The command prompt ( Non GUI) or a Mouse Click ( GUI) is where you give the input. This can be imagined to be a Interpreter for the user to give inputs. These inputs are passed on to the Operating system kernel for the actual processing. The output is finally given back to the hardware ( monitor or speaker or printer) mainly by the kernel.

With above the above example, I'd like to illustrate the Unix architecture in almost the similar fashion. Every Unix architecture consists of 
1) Hardware
2) Kernel
3) Shell
4) Applications
5) End user.

The schematic representation is illustrated below

Hardware 
This is the Physical component of your computer. It consists of Monitor, Mouse,Keyboard, Network Card, Memory, Processor.

Kernel.
The kernel is the core of the operating system. The kernel interacts with the machine hardware. It also interacts with the Software. Example : Linux Kernel.   The kernel consists of C language written sub-routines which makes it possible for it to communicate with the Hardware and Software. When you boot your machine, the kernel component gets loaded first.
Applications uses the services of kernel. These applications (software's) access kernel through system calls.
Kernel manages the machine's memory, it schedules system processes.

Shell
The Shell is a command line interpreter. It's an interface between the user and kernel. Any command you type in will be forwarded to the kernel for execution.

System calls
These are programs ( functions ) used by user program to access the kernel services to interact with hardware. Example : Open system call, Write system call.
When a user opens a new file. ie he clicks on the mouse and creates a new file, the input is read by shell which then passes to the kernel which calls the "Read system call" function. This read system call function gets executed which ultimately allows a new file to be created. The final output is visible on your hardware ( monitor)



Features of UNIX


1) Files and Process 
       Unix treats everything in the system as files. The hardware is also treated as a file. A directory is also treated as a file. 


2) Multiuser system
Unix allows multiprocessing or multiusers to access the system (CPU).


3) Multitasking system

UNIX is a multitasking system which allows a user to run multiple tasks concurrently. You can edit a file, print a file, e-mail a file and browse the internet simultaneously. 


4) Building Block architecture.

UNIX doesn't have a single command that can do a job at one shot. Instead it has thousand of commands which when combined can do any tasks with ease and perfection. Example ls and wc commands are combined using pipes to get the files listing and line count. 
In simple words, small commands are combined together to get your task done. 


5) UNIX tools

UNIX has beautiful structure of applications and toolkit. It has general purpose tools, text manipulation tools, filters, compilers, interpreters, networked applications, system administration tools and many more.


6) Pattern matching

UNIX has simple expression or characters called as meta characters which are used to match lengthy patterns and long listing of files. The metacharacter (*) is mostly used in pattern matching techniques.

7)  Documentation

UNIX has extensively written documentation about every single command, tools, and it's system. If you need any help or information on a command, you can easily refer to its documentation. 

Essentials of Unix



This chapter focuses on the pre-requisites to learn Unix, Hardware & Software requirements and Outcomes of mastering Unix.

First things you need prior to start learning Unix


1) A Basic Unix book that covers most of the topics. Your RHCE, Solaris Administration, Unix System Programming are advanced books. Keep them later after you master the basics.

2) A X-86 architecture machine on which you should have Linux ( Red Hat, Fedora Core or Ubuntu) installed. If you have a Windows Operating system, have any Virtual machine software installed like VMware workstation and on it, install any Linux, Solaris flavours.  For HP-UX and AIX, you should preferably use their own properietary hardwares. 
You can easily learn the Unix basics and master it through Linux Kernel. 

Tips : Google VMware Workstation and learn about it.
          Unix Executable can be downloaded here on the below link
          Eduunix.ccut.edu.cn

3) Linux doesn't require powerful hardware. You can install a Linux version on a Pentium machine with 128 MB RAM and 500 MB hard disk space.

Tips : 

To Install Linux, UNIX on your computer/machine, refer to the installation manual that came with the Linux executable. Search for easy installation reference guides on the internet and use video tutorials from any video sharing site on the internet.  Installation of Linux is easy.


Do I need to know any programming languages to learn Linux/Unix ?

An understanding of Computers is enough to learn the basics of Unix commands. To learn Unix Shell Scripting ( Programming ), you should have a basic knowledge of C language.

What can I gain by learning Unix ?

Unix will enable you to choose varied career roles. You can be a Unix technician, System Administrator, Network Engineer, Unix Developer, Red Hat Certified Engineer, Solaris Administrator, Perl language expert. One can choose a career in Unix Research and academics. There are endless possibilities.  You can develop your own Kernel and Operating system too. There are many certification course you can take up to master your skills. The courses are Junior Linux Administration, Red Hat Certified Technician ( RHCT), Red Hat Certified Engineer ( RHCE).
Unix is Simple. Unix is Powerful and Unix is beautiful. 




The History of Unix Operating System


First Things You should know about UNIX


What is Unix ?

Unix is a Operating Environment. It can also be called as a Operating System.  It's Origin is the "Unix system" developed by AT&T Bell labs.
Unix is a powerful Operating system which is more than 40 years since it was developed. It was developed before the advent of DOS and Windows operating system.
The Present day Unix system has many contributions made by IT industry and the student community worldwide. The open source movement is mainly due to Unix.


Give us a history on how Unix was developed ?

Unix was developed by Ken Thompson and Dennis Ritchie. In 1965, Bell Telephone Labs, General Electric, MIT (Project Mac) worked on  MULTICS project. It also had a team of AT&T Engineers. The Project didn't materialize giving the intended results. This made AT&T back out of this project in 1969 and it resulted in Dennis and Thompson design and develop a system having it's own file system, utilities and command line interpreter. This however wasn't machine independent system and wasn't capable to run on different hardware.

Dennis who is the founder of C language rewrote this system in C language in 1973. This resulted in the Unix system become portable on different hardware. The different versions of this system was known to be 
Editions, Systems, Systems III, Systems V Release 3.0, Systems V Release 3.2. The Final version was called as Unix System.


What does Unix Translate to ?

According to Dennis Ritchie in his 1974 CACM's ( Communications of the ACM) paper, UNIX ( All - Caps) meant "The Unix-Time Sharing System".  Dennis Ritchie believed that it should be used in Small caps letter as "Unix" since it doesn't have any Acronym. Hence the right usage of the word should be "Unix" and not "UNIX". 


Tell us more on what happened to AT&T and it's Unix system?

AT&T had plans to sell it's Systems V Release 3.2 commercially. This idea didn't get approval from the U.S government and this made AT&T distribute the software to Academic and research institutes. This resulted in the birth of BSD Unix.


BSD Unix

BSD Unix is Berkeley Software Distribution developed by "The University of California, Berkeley (UCB). They developed this system from the System V Release 3.2 version developed by AT&T. The BSD gathered worldwide attention and attracted universities and the students.
Further developments resulted in Berkeley rewrite their BSD Unix in their way. This resulted in developments of standard vi editor, c shell, TCP/IP, better file system, mail features and linking of files (symbolic links).
BSD Unix is the foundation of Solaris. Sun Micro systems used BSD as a foundation for the development of their own UNIX ( Sun OS). Sun Micro systems has now been acquired by Oracle and the Sun OS is called as "Oracle Solaris"


What are the Main Unix Flavors of BSD Unix ?

FreeBSD, NetBSD and OpenBSD.

What is System V Release 4 ( SVR4) ?

As different vendors created their own version of Unix, BSD release had incompatibilities and differed very much from the AT&T version.  Hence AT&T reworked on the BSD product, System V 3.2, Sun OS and Microsoft's Unix version namely XENIX. They made the final version called as the System V Release 4 SVR4.


What happened later with AT&T after System V Release 4 (SVR4) ?


AT&T sold Unix to Novell. Novell then sold the rights to X/OPEN.  X/Open merged with The Open Group.

Name the Different Flavors of Unix ?

Solaris by Oracle Solaris, AIX by IBM, HP-UX by Hewlett Packard, Tru64 UNIX (Discontinued) by Digital Equipment Corporation and lastly owned by HP, Linux by Red Hat, SUSE by Novell, CentOS, Fedora, Ubuntu, Debian.


POSIX and SINGLE UNIX SPECIFICATION.


These are two different Standard bodies in the Unix world. During the earlier years of development of Unix, it wasn't completely portable and this affected the development of applications that can be easily ported on various flavors of Unix and their hardware. 

AT&T had their own standard mechanism called as System V interface definition. The X/Open ( The open Group) had their own. There was a standard called as POSIX ( Portable Operating System Interface for Computing Environments) developed by IEEE ( Institute of Electrical and Electronics Engineers). POSIX standards were based on Unix and they had two versions called POSIX.1 and POSIX.2.   

Later, The Open Group and IEEE worked together to bring a common standard by unifying the X/Open and POSIX. This is known as "The Single  UNIX specification". It was later approved by ISO ( International Organisation of Standardization). Today UNIX is owned by the The Open Group and most of the UNIX flavors are POSIX compliant.


Open Source and Free Software Foundation


Open source and free software foundation are almost identical except that fact that "Free Software" doesn't really mean that the software can be purchased free of cost. In most cases, the Free software is indeed free of cost, but only in few cases, you may be charged a fee for support or Royality

Both Open source and free software foundation preaches the usage of software freely, modify the source code and share the software to any number of people freely.

The free software comes with a GNU pronounced "guh-noo" ( GNU Not Unix !) license. Linux is one such Unix flavor which comes with a GNU license.



What is Linux ?

Linux is a Kernel. It's inappropriate to term it as a "Operating system". Linux was developed by Linus Torvalds, a student of the Helsinki University.

Linux is a Kernel because most of the applications in Linux are due to the outcome of GNU project. Hence if Linux has to be known as a operating system, it should be called as GNU/Linux.



What are the different GNU/Linux flavors ?

Red Hat, Calder, SuSE Linux, Debian, Mandrake, Fedora. There are Ubuntu and CentOS too.