Sunday, July 10, 2016

unix interview questions


  • The following command displays output only on the screen (stdout).

$ ls
The following command writes the output only to the file and not to the screen.
$ ls > file
The following command (with the help of tee command) writes the output both to the screen (stdout) and to the file.
$ ls | tee file
http://linux.101hacks.com/unix/tee-command-examples


  • Syntax To redirect all error to file

command-name 2> errors.txt

syntax to redirect both output (stdout) and errors (stderr) to same file
command1 > everything.txt 2>&1

Syntax to redirect errors (stderr) to null or zero devices
Data written to a null or zero special file is discarded by your system. This is useful to silence out errors (also know as ‘error spam’):
command1 2> /dev/null
command1 2> /dev/zero

http://www.cyberciti.biz/faq/linux-redirect-error-output-to-file/


  • /dev/null

On Unix/Linux system there is a special
file called /dev/null. It behaves like a black hole. Whatever is printed to that file will disappear without any trace. The main use of this is when there is a program and the user wants to throw away either the regular output or the error messages.
For example, you might have an application, one that you cannot change, that spit tons of messages to the standard error channel. If you don't want to see that on the screen you can redirect it to a file. But if you do that, it can fill your disk quickly. So instead, you would redirect the standard error to /dev/null and the operating system will help you disregard all the "garbage".
http://perlmaven.com/stdout-stderr-and-redirection


  • Every process in Linux is provided with three open filesusually called file descriptor). These files are the standard input, output and error files. By default :


    Standard Input is the keyboard, abstracted as a file to make it easier to write shell scripts.
    Standard Output is the shell window or the terminal from which the script runs, abstracted as a file to again make writing scripts & program easier
    Standard error is the same as standard output:the shell window or terminal from which the script runs.

A file descriptor is simply a number that refers to an open file. By default , file descriptor 0 (zero) refers to the standard input & often abbreviated as stdin. File descriptor 1 refers to standard output (stdout) and file descriptor 2 refers to standard error (stderr)
http://www.linuxtechi.com/standard-input-output-error-in-linux


  • BASH Shell: How To Redirect stderr To stdout redirect stderr to a File )

Bash and other modern shell provides I/O redirection facility. There are 3 default standard files (standard streams) open:
[a] stdin – Use to get input (keyboard) i.e. data going into a program.
[b] stdout – Use to write information (screen)
[c] stderr – Use to write error message (screen)

Understanding I/O streams numbers
The Unix / Linux standard I/O streams with numbers:

Handle Name Description
0 stdin Standard input
1 stdout Standard output
2 stderr Standard error

The following will redirect program error message to a file called error.log:
$ program-name 2> error.log

http://www.cyberciti.biz/faq/redirecting-stderr-to-stdout


  • 25 Linux Shell Scripting interview Questions & Answers


What is Shell Script and why it is required ?
Ans: A Shell Script is a text file that contains one or more commands. As a system administrator we often need to issue number of commands to accomplish the task, we can add these all commands together in a text file (Shell Script) to complete daily routine task.

Q:2 What is the default login shell and how to change default login shell for a specific user ?
# chsh <username> -s <new_default_shell>
# chsh linuxtechi -s /bin/sh

Q:4 How to redirect both standard output and standard error to the same location ?
Ans: There two method to redirect std output and std error to the same location:
Method:1 2>&1 (# ls /usr/share/doc > out.txt 2>&1 )
Method:2 &> (# ls /usr/share/doc &> out.txt )

http://www.linuxtechi.com/linux-shell-scripting-interview-questions-answers/


  •  What is the system command to find the current environment variables of the shell you’re running?

 The command is “env”. The output will depend on which system you’re running:

 What is swapping and paging?
 Consider a reference book, for example. You only open the page you need- you don’t need to have all the pages open at once. That is both almost impossible and extremely inefficient. A book is comparable to a process in UNIX. At any given time, there are several books that UNIX is reading. However, UNIX only takes a few pages from a book at one time, as needed. Once it is done with the page, or if it becomes “old”, it is sent back to storage. This is known as paging. Sometimes UNIX sends the entire book, with all its pages, back to the memory- this is known as swapping. This happens when the work load is really heavy.

Paging is more efficient if UNIX is running a large number of processes at one time. Pages (memory segments) can be stored and retrieved as needed. Swapping is more demanding in terms of resources and memory, but it provides faster results, as UNIX doesn’t need to search and retrieve pages every single time while it is executing a process
 https://blog.udemy.com/unix-shell-scripting-interview-questions/

Specify the difference between absolute path and related path?
Absolute path refers to the exact path as defined from the root directory. Related path refers to the path related to the current locations.

What is the FIFO?
FIFO (First In First Out) is also called named pipes and it is a special file for date transient. Data is read-only in the written order. This is used to inter-process communications, where data write to one end and reads from another end of the pipe.

What is mean by Super User?
The user with access to all files and commands within the system is called a superuser. Generally, the superuser login is to root and the login is secured with the root password.

What is the process group?
A collection of one or more processes is called process group. There is a unique process id for each process group. The function “getpgrp” returns the process group ID for the calling process.

What are the different file types available with UNIX?
    Regular files
    Directory files
    Character special files
    Block special files
    FIFO
    Symbolic links
    Socket
   
https://www.softwaretestinghelp.com/unix-interview-questions/


  • The mkfifo command basically lets you create FIFOs (a.k.a named pipes)

You'd have seen commands that contain a vertical bar (|) in them. This bar is called a pipe. What it does is, it creates a channel of communication between the two processes (when the complete command is executed).

ls | grep .txt
The command mentioned above consists of two programs: ls and grep.
Both these programs are separated by a pipe (|)
So what pipe does here is, it creates a channel of communication between these programs
when the aforementioned command is executed, the output of ls is fed as input to grep.
So finally, the output that gets displayed on the terminal consists of only those entries that have '.txt' string in them.

mkfifo pipe2

So 'pipe2' is now a named pipe. Now comes the question how named pipes are more useful? Well, consider the case where you have a process running in a terminal and producing output, and what you want is to channelize that output on to a different terminal. So here, a named pipe could of great help.


https://www.howtoforge.com/linux-mkfifo-command/



  • Some of the frequently used shells in UNIX include the following:

    tcsh – enhanced C Shell
    zsh – Z SHell
    sh – Bourne shell
    csh – C SHell
    ksh – Korn SHell
    bash – Bourne Again Shell

Nohup is a distinctive command that is utilized to run the process in the background, yet it is marginally unique in relation to and which is typically utilized for putting a procedure in the background. A typical UNIX process that began with nohup will not stop regardless of whether the client has logged off from the framework. While the background process began with and will stop when the client logoff

https://www.onlineinterviewquestions.com/unix-interview-questions/   



  • Explain the difference between soft and hard links?

The most common difference between the soft and hard link is, a hard link is a direct reference to the file in UNIX. However, on the other hand, the soft link is the name refers to the file which means they point out files using their names.

Can you link directories to soft links?
Yes, you can link directories to the soft links as the file system structure supports them.

How do you know if you have the soft or hard link?
The soft link commonly known as the symbolic is the original copy of the file while the hard link is the perfect replica of the original file. So, if you delete the original copy from the system then the soft link won’t have any value but the case is right opposite in hard link.

How hard links are used in the UNIX?
What hard links do on an immediate basis is to break down the file system structure to process the information. Unlike soft links, hard links cannot be spanned across the file system.

Do hard and soft link shares the same Inode?
Yes, both hard link and soft link share the same inode.

Do hard link work even after deleting the soft link?
Yes, the hard will work even after you end up deleting the soft file. As they are a perfect mirror copy of soft link, you can access the file until the links to the file are do not end up on zero.

Can you link directories to hard links?
directories don’t fit into the system and essentially break it. So, in order to protect the file system structure, directories are avoided link with hard links.
https://www.gangboard.com/blog/unix-shell-scripting-interview-questions-and-answers/


  • Discuss the difference between swapping and paging?


Swapping – The complete process is moved to the main memory for execution. To provide the memory requirement, the process size must be less than the available main memory capacity. The implementation is easy but is an overhead to the system. Memory handling is not more flexible with swapping systems.
It the procedure of copying the entire process from main memory onto secondary memory.
For execution, the whole process is moved from swap device to the main memory.

Paging – Only the required memory pages are moved to the main memory for execution.

What do understand by Kernel?
Unix operating system is basically divided into three parts, namely, the kernel, the shell, and the commands and utilities

Explain Superblock in UNIX?
Each logical partitions in Unix are referred to as the File system and each file system contains, a ‘boot block’, a ‘superblock’, ‘inodes’, and data blocks’.
https://www.softwaretestinghelp.com/unix-interview-questions/