Common UNIX Shell Scripting Interview Questions
UNIX based systems are used all over the world today, in large companies, in Universities and in government organizations. The most popular non certified version of UNIX is Linux, while the most popular certified version is Apple’s OS X – and who hasn’t heard of Linux or the very popular Mac OS X? UNIX based systems have been around since the late 1970s. They’re still going strong today, despite stiff competition from Windows.
If you’re looking to get hired to write UNIX shell scripts or to maintain existing shell scripts, chances are you will have a lot of interviews lined up; there is always a lot of demand for UNIX scripters out there. We’ve created a list of questions that you are likely to be asked at an interview if you’re just starting out, or if have a couple of years of experience under your belt. Nothing beats true knowledge, however, so we recommend that you don’t rely on just these questions to get you through. Instead, try to get a thorough understanding of all the basic concepts of UNIX shell scripting. We offer a comprehensive course covering Unix OS fundamentals and scripting, to help you brush up your knowledge. We are sure it can help you crack any interview, so don’t hesitate to sign up!
Let’s get started with the questions:
What is the UNIX shell?
The UNIX shell is the user interface. It is the program that accepts your input and changes it into a format understandable by the kernel. The kernel then communicates with the computer hardware and gets the requested task done. The UNIX shell is the layer between the user and the kernel, just like the kernel is the layer between the shell and the system hardware.
What is shell scripting?
Shell scripting is writing scripts (code or programs) for the UNIX shell. These scripts help you perform programming tasks, like setting a schedule, opening a file or running an application. UNIX shell scripting also sometimes refers to reprogramming an existing operating system. This is sometimes done by large companies, so that they can have their own operating systems with customized features.
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:
OLDPWD=/ HOME=/var/root TERM=vt100 PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin PWD=/var/root /var/root #
What is the command to see how long the system has been running?
Typically, this command will show you path, shell version and user name, among other common variables. You might be asked questions about these variables, so we recommend that you learn what they mean. This Linux course is a great place to cover these.
The command to see how long the system has been running is “uptime”. The output should look something like this:
02:40:37 up 1 min, load average: 0.00, 0.00, 0.00
How do you find out if a certain directory exists on the system?
You can use the –d command, along with test commands, to see if a specified directory exists on the UNIX system. Consider the following snippet of code:
if [-d $directory] then execute this test command fi
Here, you can use the variable $directory to store the name of the directory you’re looking for. The –d command will then get UNIX to execute a specified test command if the directory exists. Finally, fi will be executed last. To get a list of permissible test commands, you can type “$ man test”. If you’re wondering what “fi” is, it’s how you terminate an “if” statement. It’s “if” spelled backwards. You may want to check out other Linux/Unix examples with this course.
What is the FOR loop in UNIX? Write the syntax for it.
The FOR loops executes a command(s) if a condition is true. It will continue to execute the command(s) until the condition turns false. In UNIX, it will operate on items in a list, or a set of files, for example, and execute certain commands until it runs out of those items.
The syntax for the FOR loop is as follows:
for var in a, b , c …. do execute this command(s) done
To learn more about looping constructs, how to break out of them and how to use them efficiently, you can take this course to master the commands. You will probably be asked about conditional loops in UNIX, so it’s a good idea to read up about them. You may also be asked to write a sample script or two to show the interviewer you know what you’re talking about.
What is swapping and paging?
UNIX based operating systems automatically handle swapping and paging. Swapping and paging are processes that occur when the operating system is managing system memory. Every process in UNIX based systems has individual segments. An area of the disk is treated as the swap space where these segments are stored temporarily as long as a process needs them. This is done to conserve memory, because multiple processes are usually occurring at once and there isn’t enough physical memory to store all of them.
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.
What are arrays? Write a sample array in UNIX.
Arrays are a special variable that can store multiple, individual bits of data. Each data element stored in an array is assigned a unique index number, which makes it possible to find and retrieve that data element later.
Here is an example of an array:
examplearray = (a b c d e)
Here, the array named “examplearray” has 5 elements. The first element a will have an index number of 0, while the last element e has an index number of 4.
How do you prepare a shell script for execution from the command line prompt?
You need to prepare a shell script before it becomes executable with the chmod command. The chmod command sets the access mode for an executable or a file.
Here is an example of the chmod command:
chmod 755 abc chmod u=x
This will give the current user (u) permission to execute (x) the process named “abc”.
Some of the other questions you might be asked include how to copy files, how to operate a server remotely or how to kill processes. The only way to make sure that you get through the interview is to be prepared. It’s not sufficient to just be an expert – you also need to make sure the interviewer feels that way about you as well! This course can help you learn how to master the interview mindset, and other strategies that will make you stand out.
Top courses in Shell Scripting
Shell Scripting students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.