Shell Scripting Interview Questions and Answers
1. What is Shell Scripting?
Answer: Shell scripting is used to program the command line of an operating system. Shell Scripting is also used to program the shell which is the base for any operating system. Shell scripts often refer to programming UNIX. Shell scripting is mostly used to program operating systems of windows, UNIX, Apple, etc. Also, this script is used by companies to develop their operating system with their features.
2. What Are The Different Variables Present In Linux Shell?
Answer: Variables can be defined by the programmer or developer they specify the location of a particular variable in the memory. There are two types of shells they are System variables and user-defined variables. System variables are defined by the system and user-defined variables are to be defined by the user (small letters).
3. Explain About Debugging?
Answer: Shell can make your debugging process easier because it has lots of commands to perform the function. For example sh –n command helps you to perform debugging. It helps you to read the shell but not to execute it during the course. Similarly sh –x command helps you by displaying the arguments and functions as they are executed.
4. Explain the Linux Directory commands along with the description?
Answer: This is the most Frequently asked Hibernate Interview Questions in an interview. Enlisted below are the directory commands along with descriptions
pwd: It is a built-in command which stands for ‘print working directory’. It displays the current working location, the working path starting with / and directory of the user. It displays the full path to the directory you are currently in.
Is: This command list out all the files in the directed folder.
cd: This stands for ‘change directory’. This command is used to change to the directory you want to work from the present directory. We just need to type cd followed by the directory name to access that particular directory.
mkdir: This command is used to create an entirely new directory.
rmdir: This command is used to remove a directory from the system.
5. What are the different blocks of a file system? Explain in brief
Answer: Given below are the main 4 different blocks available on a file system.
Super Block: This block mainly tells about a state of the file system like how big it is, maximum how many files can be accommodated, etc.
Boot Block: This represents the beginning of a file system. It contains the bootstrap loader program, which gets executed when we boot the host machine.
Inode Table: As we know all the entities in a UNIX are treated as files. So, the information related to these files is stored in an Inode table.
Data Block: This block contains the actual file contents.
6. What is Continue statement and what is it used for?
Answer: Continue is a keyword and is used whenever we want to take the control to the beginning of the loop, bypassing the statements inside the loop which have not yet been executed.
When the keyword continues is encountered inside any loop in the program, control automatically passes to the beginning of a loop. Continue is generally associated with an if.
7. What are the three modes of operation of vi editor?
Answer: 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.
8. Enlist some Linux networking and troubleshooting commands?
Answer: A network administrator is a person who maintains a system network which includes networking configurations and troubleshooting. Mentioned below are few commands along with their description:
Hostname: To see the hostname of the machine and to set the current hostname.
Ping: To inspect if the machine is responding or not.
ifconfig: To visualize and manage routing and networking protocols. It displays network configuration. ‘ip’ is the replacement of ifconfig command.
netstat: It displays many network connections, routing tables, interface statistics. Currently, ‘ss’ is the replacement of netstat command which is used to get more information.
Traceroute: It is network troubleshoot utility which is used to determine the number of hops required for a particular packet to reach the destination.
Tracepath: It is similar to traceroute with a difference that it need not require root privileges.
Dig: This is the command that is used to query the DNS name servers for any task/operations related to DNS lookup services.
nslookup: To search for DNS related query.
Route: It shows the details of the route table files and IP routing table.
tr: This command combines ping and track path into a single command.
Ifplugstatus: This command is used to check network cable is plugged in or not.
9. What is a Break statement and what is it used for?
Answer: 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.
10. What are the three different security provisions provided by UNIX for a file or data?
Answer: Three different security provisions provided by UNIX for a file or data are:
It provides a unique user id and password to the user, so that unknown or unauthorized person should not be able to access it.
At the file level, it provides security by providing read, writes & executes permissions for accessing the files.
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.
11. Is it possible to use a shell script to determine if the directory exists or not?
Answer: Yes, this is possible and for this, the users are free to simply consider the UNIX test command. The option that is useful in the –d option. It is not always necessary that the information regarding the existence of a directory is displayed only when the directory is recognized by the system. Information regarding all the directories whether known to the system or not can be displayed with this command. Generally, directories are present in the variable $mydir.
12. How to debug the problems encountered in the shell script/program?
Answer: 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.
13. What are Shell Variables? Mention types of Shell Variables?
Answer: Shell variables provide information to execute scripts/commands. There are two types of shell variables: Unix defined and User Defined.
Unix defined variables are by default set by UNIX, to define properties. For example, SHELL is Unix variable that stored information about working shell set to default.
User-defined variables are defined by a developer to store/manage information. For example, $ mm = 2018 defined by used means, the value of 2018 is assigned to variable mm.
14. Difference between process and thread?
Answer: To put it in simple words, a thread is a small piece of code/instruction that needs to be executed in a process, whereas a process is a collection of one or more thread executions to complete one complete task.
Note: A process consists of multiple threads can share resources among them as they belong to the same process. And threads belong to different processes cannot share their resources.
15. In Shell scripting, how can you say that C Shell is better than Bourne Shell?
Answer: All the commands can be aliased simply with the C shell whereas the same is not possible in case of Bourne Shell
Lengthy commands can be used again and again in C shell whereas the Bourne doesn’t allow the same in all the cases
The command history can be accessed through the C shell but it cannot be accessed through the Bourne
There is no need to type the command again and again in case of C…
16. Tell the procedure of accessing the command line arguments from a script?
Answer: It can be done simply by executing the $ in the system. It will show all the arguments that have already passed from command line to a script. Sometimes you need to specify the argument’s numeric position and the users have to make sure that the things are done properly.
17. What according to you is the simplest method for copying a file from one system to another?
Answer: There are certain utilities with the help of which this can be done easily and without worrying about anything. The users can simply keep up the pace with them when it comes to the same. These utilities are FTP, SCP, and rsync. It is necessary to specify the hostname before these utilities are considered.
18. Suppose you execute a command using exec, what will be the status of your current process in the shell?
Answer: All the forked processes which are new get overlays when the exec is executed. The command simply gets executed without making any impact on the current process. Also, no new process will be created in this scenario.
19. When should shell programming/scripting not be used?
Answer: Generally, shell programming/scripting should not be used in the below instances.
When the task is very much complex like writing the entire payroll processing system.
Where there is a high degree of productivity required.
When it needs or involves different software tools.
20. What is the Importance of writing Shell Scripts?
Answer: 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 commands.
It helps automate some tasks of the day to day life.
It is useful for automating system administration tasks.
Mainly it saves time.
21. What does the? (dot) indicate at the beginning of a file name and how should it be listed?
Answer: A file name which begins with a. (dot) is called as a hidden file. Whenever we try to list the files it will list all the files except hidden file.
22. Is separate compiler required for executing a shell program?
Answer: A separate compiler is not required to execute a shell program. The shell itself interprets the command in the shell program and executes them.
23. What are the Advantages of C Shell over Bourne Shell?
Answer: 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 a command history feature. C shell remembers the previously typed command. Thus, it avoids typing the command again and again.
24. what are positional parameters?
Answer: 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.
25. What is LILO?
Answer: LILO is Linux Loader is a boot loader for Linux. It is used to load Linux into the memory and start the Operating system. LILO can be configured to boot other operating systems as well. LILO is customizable, which means that if the default configuration is not correct, it can be changed. Config file for LILO is lilo.conf.
LILO is also a code snippet which loads PC BIOS into the main memory at the time of starting the computer system. It handles the following tasks :
1. Locating the Linux kernel.
2. Identifying other supporting programs and loading them in memory.
3. Starting Kernel.
The selection of various kernel images and boot routines is supported by LILO. For this reason, it is known as a boot manager.
26. Explain About Stdin, Stdout, And Stderr
Answer: These are known as standard input, output, and error. These are categorized as 0, 1 and 2. Each of these functions has a particular role and should accordingly function for efficient output. Any mismatch among these three could result in a major failure of the shell.
27. State The Advantages Of Shell Scripting?
Answer: There are many advantages of shell scripting some of them are, one can develop their operating system with relevant features best suited to their organization than to rely on costly operating systems. Software applications can be designed according to their platform.
28. How Do You Remove A File?
Answer: “rm” removes each specified file argument (provided that it is a valid pathname). If you specify either . or .. as the final component of the pathname for a file, rm displays an error message, and moves onto the next file. If you specify a file you do not have write permission for, rm asks you for confirmation.
29. What Is The Use Of Continue Command In Shell Scripting?
Answer: The continue command is identical to break command except it causes the present iteration of the loop to exit, instead of the entire loop. Continue command is useful in some scenarios where an error has occurred but we still want to execute the next commands of the loop.
30. What is the default login shell and how to change the default login shell for a specific user?
Answer: In Linux like Operating system “/bin/bash” is the default login shell which is assigned while user creation. We can change the default shell using the “cash” command. An example is shown below.
31. What is Unix Shell?
Answer: A Unix shell is a command-line interpreter that provides a Unix-like command-line user interface.
32. How you will make the soft link inactive in the scripting?
Answer: The soft links can simply be made inactive in your delete the file and the same doesn’t have any impact on the hard link and i.e. the users can access the file copy.
33. What is the future of Application Express?
Answer: Well, this is important due to so many reasons and a few important ones are spotlighted below.
1. When it comes to creating customized commands or creating your own, shell scripting is very useful and powerful
2. There are a lot of tasks that can easily be automated in the process
3. The users are responsible for providing the input and this means there will be no errors in the output
4. It is good enough to be considered for time-saving
5. A lot of system administration tasks can be accomplished and can be automated.
34. How do I separate development from production environment?
Answer: Depending on requirements for the availability of the application, the size of the user population and other factors you can decide how to divide the development environment from the production environment.
For certain applications, it is acceptable to combine the development server with the deployment server, as long as the end users understand that sometimes the application is not available.
Other applications may require two (development and production) or even three (development, test, and production) servers.
In Application Express applications can be moved from environment to environment using an export and import facility for application definitions.
With one server available to run the database and Application Express, you can still separate the development version of an application from its production version by using two workspaces accessing separate schemas.
In this case, one workspace will be used by developers and the other will be the workspace in which the application is deployed in production.
35. How can you put separate soft and the hard link in shell scripting?
Answer: Well, shell scripting is a powerful approach. The links are used when it comes to creating shortcuts just like Windows. Soft links are those which generally related to the file name and they don’t have any specific location. They can be anywhere on the file system. On the other side, the hard links are related to the node and have a particular location which is fixed in most of the cases. They remain present on the same file system.
36. What do you know about the MBR in the shell scripting? How it is useful for the users?
Answer: It stands for Master Boot Record and is a small program whose prime function is to make sure that the kernel is loaded during the system boot set up. It is present in the boot block. It is nothing but the MBR that simply make sure that the users can perform the boot looping tasks accurately and reliably.
37. Tell something about the Super Block in Shell scripting?
Answer: It is a program that contains all the information regarding a specific file system. It reflects the block size that is used by its associated number, the size of the system in terms of data handling and programming. It also provides information regarding the free inodes and the data blocks which are currently associated with the system.
38. Explain what do you know about the term shell when it comes to scripting? Name a few shells that are commonly used.?
Answer: it acts as a direct link among the kernel and the user. The system can run multiple shells at the same time although there is always only one kernel that remains present. Thus, when the command is given by the user, the shell begins exchanging information with the kernel. The operations are then executed and the information is conveyed to the user.
The shells that are commonly used in the scripting are Ksh, bourne, bash, csh.
39. What command needs to be used to take the backup?
Answer: 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.
40. What are Metacharacters in a shell?
Answer: Metacharacters are special characters in a program or data field which provides information about other characters. They are also called as, regular expressions in a shell.
41. In Shell scripting, how you will put separate the grep and egrep?
Answer: Grep can easily be extended and the same can then be called as egrep. In other words, the egrep is an advanced version of grip. There are some added features in it and i.e. it can easily be considered for the additional occurrence of a previous character. This can also be considered when it comes to alternate matching.
42. What are the different communication commands available in Unix/shell?
Answer: Basically, there are 4 different communication commands available in Unix/shell. And they are mail, news, wall & motd.
43. What are the Special Variables set by Bourne shell for command-line arguments?
Answer: The following table lists the special variables set by the Bourne shell for command-line arguments.
44. What is the use of “$?” sign in a shell script?
Answer: While writing a shell script, if you want to check whether the previous command is executed successfully or not, then we can use “$?” with if statement to check the exit status of the previous command. The basic example is shown below.
45. How to put comments in your shell script?
Answer: Comments are the messages to yourself and for other users that describe what a script is supposed to do and how it works. To put comments in your script, start each comment line with a hash sign (#). An example is shown below.