UNIX SHELL SCRIPTING Interview Questions for freshers and experienced :-
1. What is a shell?
The shell can be described as the command line user interface for the UNIX operating system. As the development experts define “shell is both command and scripting language that is basically used to control the execution of the system.
2. Describe the shell types in the UNIX?
In the UNIX there are basically two primary types of shells:
- Bourne shell
- C shell
3. Who wrote the first UNIX shell in history?
Stephen R. Bourne was the person who first wrote original UNIX shell back in 1970 when he was working as an employee at the AT&T.
4. What is shell scripts?
The shell scripts are the list of commands that are organized in a way to execute a program in UNIX. Generally, shell scripts contain; comments, followed by the # sign and further describing the execution steps.
5. How do you create shortcuts in the Linux?
It is done through the built-in links in the Linux which are also known as:
- Hard links
- Soft links
6. 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.
7. Can you link directories to soft links?
Yes, you can link directories to the soft links as the file system structure supports them.
8. 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.
9. 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.
10. How will you find out which command you are currently using?
It can be easily done using the “echo$SHELL” in the scripting language.
11. Do hard and soft link shares the same Inode?
Yes, both hard link and soft link share the same inode.
12. 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.
13. Can you link directories to hard links?
No, 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.
14. What are the benefits of using shell scripting?
The key benefits users will receive from shell scripting are:
- You can create your own commands for the program
- You can put some tasks on automatic execution
- You can automate administration tasks
- You can save time and money
15. Do you need to use a special compiler for executing shell program?
No, there is no need to use the personal compiler to execute the shell program as the shell itself can interpret the command and executes them.
16. What is the base of the Unix shell?
The Unix shells work upon the principle that it cannot only accept the commands from the keyboards but also from the files.
17. Can you modify the file permissions?
Yes, you can easily modify the file permission using the umask.
18. Describe the shell variables?
Shell variables play a significant role in the shell scripting or programming by storing and manipulating the information within the shell program.
19. Write down the types of shell variables?
There are basically two types of shell variables known as:
- Unix defined variables: these are known as standard variables that are defined in Capital letters.
- User-defined variables: These are user variables and usually defined in lower letters.
20. What is the average lifespan of a variable in the shell?
The lifespan of a variable is very small and is valid until the execution.
21. Can you make a variable unchangeable? If yes, then explain?
Yes, a variable can be made unchangeable and this can be accomplished using readonly. For instance, if you wish to keep the value of the variable a, 10 then you can do it with readonly.
22. How you can completely remove variables from the shell?
You can completely remove all the variables using the unset command in the shell.
23. What are the security provisions provided by the Unix to protect the system?
In order to protect valuable information and files, Unix provides three key security provisions:
- It provides a unique ID and password to its every user so; no unauthorized access can be granted to anyone.
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.
- Lastly the Unix offers security through the encryption which is one of the best ways to secure your file and data.
24. What are the top three significant editors in Unix?
Ed, vi and ex are the three significant editors that are available in almost every version of UNIX.
25. Write down modes of operations in Vi editors and describe them?
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.
26. Describe the role of echo in the shell scripting?
Echo is the built-in command of the shell that is used to write arguments in standard output.
27. What is the alternative command for the echo?
tput is used as an alternative command for the echo in the shell scripting.
28. What IFS denotes in the shell?
The term IFS stands for the Internal field separator and it is one of the most crucial variables of the shell scripting. It has space, tab, and line as the default value.
29. What is break statement why do we use it?
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.
30. Which command you will use to find out the run time?
Uptime is used to find out how long the system has been running.
31. What are the three modes of operation of vi editor? Explain in brief.
The three modes of operation of vi editors are,
(i) Command Mode: In this mode, all the keys pressed by a user are interpreted as editor commands.
(ii) Insert Mode: This mode allows for insertion of a new text and editing of an existing text etc.
(iii) The ex-command Mode: This mode allows a user to enter the commands at a command line.
32. What is the alternative command available to echo and what does it do?
tput is an alternative command to echo.
Using this, we can control the way in which the output is displayed on the screen.
33. How to find out the number of arguments passed to the script?
The number of arguments passed to the script can be found as shown below.
echo $ #
34. What are control instructions and how many types of control instructions are available in a shell? Explain in brief.
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.
- Sequence Control Instruction – This ensures that the instructions are executed in the same order in which they appear in the program.
- Selection or Decision Control Instruction – It allows the computer to take a decision as to which instruction is to be executed next.
- Repetition or Loop Control Instruction – It helps a computer to execute a group of statements repeatedly.
- Case-Control Instruction – This is used when we need to select from several alternatives.
35. What are Loops and explain three different methods of loops in brief?
Loops are the ones, which involve repeating some portion of the program/script either a specified number of times or until a particular condition is being satisfied.
3 methods of loops are:
- For loop – This is the most commonly used loop. For loop allows specifying a list of values which the control variable in the loop can take. The loop is then executed for each value mentioned in the list.
- While loop – This is used in a program when we want to do something for a fixed number of times. While loop gets executed till it returns a zero value.
- Until loop – This is similar to while loop except that the loop executes until the condition is true. Until loop gets executed at least once till it returns a non-zero value.
36. What is IFS?
IFS stands for Internal Field Separator. And it is one of the system variables. By default, its value is space, tab, and a new line.
It signifies that in a line where one field or word ends and another begins.
37. What is a Break statement and what is it used for?
The break is a keyword and is used whenever we want to jump out of a loop instantly without waiting to get back to the control command.
When the keyword break is encountered inside any loop in the program, control will get passed automatically to the first statement after a loop. A break is generally associated with an if.
38. What is Continue statement and what is it used for?
Continue is a keyword and is used whenever we want to take the control to the beginning of the loop, by passing the statements inside the loop which have not yet been executed.
When the keyword continue is encountered inside any loop in the program, control automatically passes to the beginning of a loop. Continue is generally associated with an if.
39. How many fields are present in a crontab file and what does each field specify?
The crontab file has six fields. The first five fields tell cron when to execute the command: minute(0-59), hour(0-23), day(1-31), month(1-12), and day of the week(0-6, Sunday = 0).
And the sixth field contains the command to be executed.
40. What are the two files of crontab command?
Two files of crontab command are:
cron.allow – It decides which users need to be permitted from using crontab command.
cron.deny – It decides which users need to be prevented from using crontab command.
41. What command needs to be used to take the backup?
tar is the command which needs to be used to take the backup. It stands for tape archive. The tar command is mainly used to save and restore files to and from an archive medium like tape.
42. What are the different commands available to check the disk usage?
There are three different commands available to check the disk usage.
And they are:
df – This command is used to check the free disk space.
du – This command is used to check the directory wise disk usage.
dfspace – This command is used to check the free disk space in terms of MB.
43. What are the different communication commands available in Unix/shell?
Basically, there are 4 different communication commands available in Unix/shell. And they are mail, news, wall & motd.
44. How to find out the total disk space used by a specific user, say for example username is John?
The total disk space used by John can be found out as shown below.
45. What is Shebang in a shell script?
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.
The above line also tells which shell to use.
46. What is the command to be used to display the shell’s environment variables?
Command to be used to display the shell’s environment variables is env or printenv.
47. How to debug the problems encountered in shell script/program?
Though generally it depends on the type of problem encountered. Given below are some common methods used to debug the problems in the script.
Debug statements can be inserted in the shell script to output/display the information which helps to identify the problem.
Using “set -x” we can enable debugging in the script.
48. How to know the variable length?
Variable length can be checked as shown below
49. What is the difference between = and ==?
= -> This is used for assigning value to the variable.
== -> This is used for string comparison.
50. How to open a read-only file in Unix/shell?
Read-only file can be opened as shown below:
51. How can the contents of a file inside jar be read without extracting in a shell script?
The contents of the file inside a jar can be read without extracting in a shell script as shown below.
tar –tvf .tar
52. What is the difference between diff and cmp commands?
diff – Basically, it tells about the changes which need to be made to make files identical.
cmp – Basically it compares two files byte by byte and displays the very first mismatch.