Showing posts with label unix. Show all posts
Showing posts with label unix. Show all posts

January 15, 2020

Top 20 Unix interview Questions & Answers

Ques: 1. What is the Importance of writing Shell Scripts?

Ans: The points given below explain the importance of writing shell scripts. Shell script takes input from the user, file and displays it on the screen. Shell scripting is very useful in creating your own commands. It is helpful in automating some tasks of the day to day life. It is useful for automating system administration tasks. Mainly it saves time.


Ques: 2. What are the different Types of Shells available? 

Ans: There are mainly 4 important types of shells that are widely used. And they include:
  1. Bourne Shell (sh)
  2. C Shell (csh)
  3. Korn Shell (ksh)
  4. Bourne Again Shell (bash)

Ques: 3. What are the Advantages of C Shell over Bourne Shell?

Ans: The advantages of C Shell over Bourne Shell are:
  • C shell allows aliasing of commands i.e. a user can give any name of his choice to the command. This feature is mainly useful when a user has to type the lengthy command again and again. At that point of time, instead of typing a lengthy command a user can type the name that he has given.
  • C shell provides command history feature. C shell remembers the previously typed command. Thus, it avoids typing the command again and again.

Ques: 4. What are the two types of Shell Variables? Explain in brief.

Ans: The two types of shell variables are: 

1). Unix Defined Variables or System Variables – These are standard or shell defined variables. Generally, they are defined in CAPITAL letters. 

for example: SHELL – This is a Unix Defined or System Variable, which defines the name of the default working shell. 

2). User Defined Variables – These are defined by users. Generally, they are defined in lower letters. 

For example: $ a=10 –Here the user has defined a variable called ‘a’ and assigned value to it as 10.


Ques:5. What are positional parameters? Explain with an example.

Ans: Positional parameters are the variables defined by a shell. And they are used whenever we need to convey information to the program. And this can be done by specifying arguments at the command line.

There are totally 9 positional parameters present i.e. from $1 to $9.

Example: $ Test Indian IT Industry has grown very much faster

In the above statement, positional parameters are assigned like this.

$0 -> Test (Name of a shell program/script)
$1 ->Indian
$2 -> IT and so on.

Ques: 6. What are the three different security provisions provided by UNIX for a file or data?

Ans: Three different security provisions provided by UNIX for a file or data are:
  1. It provides a unique user id and password to the user, so that unknown or unauthorized person should not be able to access it.
  2. At file level, it provides security by providing read, write & execute permissions for accessing the files.
  3. Lastly, it provides security using file encryption. This method allows encoding a file in an unreadable format. Even if someone succeeds in opening a file, but they cannot read its contents until and unless it is decrypted.

Ques: 7. What are control instructions and how many types of control instructions are available in a shell? Explain in brief.

Ans: Control Instructions are the ones, which enable us to specify the order in which the various instructions in a program/script are to be executed by the computer. Basically, they determine a flow of control in a program.

There are 4 types of control instructions that are available in a shell:
  1. Sequence Control Instruction – This ensures that the instructions are executed in the same order in which they appear in the program.
  2. Selection or Decision Control Instruction – It allows the computer to take a decision as to which instruction is to be executed next. 
  3. Repetition or Loop Control Instruction – It helps a computer to execute a group of statements repeatedly. 
  4. Case-Control Instruction – This is used when we need to select from several alternatives.

Ques: 8. What are the two files of crontab command?

Ans: Two files of crontab command are:
  1. cron.allow – It decides which users need to be permitted from using crontab command. 
  2. cron.deny – It decides which users need to be prevented from using crontab command.

Ques: 9. What are the different commands available to check the disk usage?

Ans: There are three different commands available to check the disk usage. And they are:

  1. df – This command is used to check the free disk space.  
  2. du – This command is used to check the directory wise disk usage.  
  3. dfspace – This command is used to check the free disk space in terms of MB.

Ques: 10. What is Shebang in a shell script?

Ans: Shebang is a # sign followed by an exclamation i.e. !. Generally, this can be seen at the beginning or top of the script/program. Usually, a developer uses this to avoid repetitive work. Shebang mainly determines the location of the engine which is to be used in order to execute the script.

Here ‘#’ symbol is called as hash and ‘!’ is called a bang.

Example: #!/bin/bash

The above line also tells which shell to use.


Ques: 11. What is inode?

Ans. The inode is a data structure in a file system. Each inode is able to store the attributes and the location (s) of the disk block of the object’s data.


Ques: 12. What is the difference between Swapping and Paging?

Ans:

Swapping: Whole process is moved from the swap device to the main memory for execution. Process size must be less than or equal to the available main memory. It is easier to implementation and overhead to the system. Swapping systems does not handle the memory more flexibly as compared to the paging systems.

Paging: Only the required memory pages are moved to main memory from the swap device for execution. Process size does not matter. Gives the concept of the virtual memory. It provides greater flexibility in mapping the virtual address space into the physical memory of the machine. Allows more number of processes to fit in the main memory simultaneously. Allows the greater process size than the available physical memory. Demand paging systems handle the memory more flexibly.


Ques: 13. What is the difference between $* and $@?

Ans: $@ treats each quoted argument as separate arguments but $* will consider the entire set of positional parameters as a single string.


Ques: 14. Write down syntax for all loops in shell scripting ?
Ans: The syntax for all loops in unix:

for loop:

for i in $( ls ); do
echo item: $i
done

while loop:

#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]; do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
done

until loop:

#!/bin/bash
COUNTER=20
until [ $COUNTER -lt 10 ]; do
echo COUNTER $COUNTER
let COUNTER-=1
done


Ques: 15. Can you link directories to hard links?

Ans: You cannot link directories to hard links and the reason behind it is quite technical. For now, you can learn that 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.


Ques: 16. What are the security provisions provided by the Unix to protect the system?

Ans: In order to protect valuable information and files, Unix provides three key security provisions:
  1. It provides a unique ID and password to its every user so; no unauthorized access can be granted to anyone. 
  2. On each file, it offers read, writes and execute permission so, the user can grant permission he or she thinks is necessary to the third party. 
  3. Lastly the Unix offers security through the encryption which is one of the best ways to secure your file and data.

Ques: 17. Write down modes of operations in Vi editors and describe them?

Ans: Three significant modes of operation Vi include:
  • Insert mode: this mode will allow you to insert edit the existing text or add the new text in the command line. 
  • Command mode: in this mode, you can use the keyboard to make all the commands. 
  • The ex-command mode: with this mode, you can enter the command in the command line.

Ques: 18. If one process is inserting data into your MySQL database? How will you check how many rows inserted into every second?

Ans: Purpose of this Unix Command Interview is asking about "watch" command in UNIX which is repeatedly execute command provided with specified delay.


Ques: 19. What is break statement why do we use it?

Ans: The break refers to the command that is used whenever the user wants to directly jump out of the loop rather than following the path which leads back to the control command. Every single time break statement takes place in the program, the control automatically gets passed after the first line of command.


Ques 20: How will you run a process in background? How will you bring that into foreground and how will you kill that process?

Ans: For running a process in background use "&" in command line. For bringing it back in foreground use command "fg jobid" and for getting job id you use command "jobs", for killing that process find PID and use kill -9 PID command. This is indeed a good Unix Command interview questions because many of programmer not familiar with background process in UNIX.