10 Most Likely Asked Unix Shell Programming Interview Questions And Answers

10 Most Likely Asked Unix Shell Programming Interview Questions And Answers

Unix Shell Programming is one of the top requirements in most IT jobs today, be it an Informatica Developer, a System Administrator, an Ops Developer or an ETL Developer job opening. Prepare from the top 10 Unix Shell Programming Interview Questions highly asked in job interviews for such roles.

The below questions are picked from our popular book, UNIX Shell Programming Interview Questions You’ll Most Likely Be Asked

In the book, the questions are sorted into the following chapters –

Chapter 1: C Shell – Beginner

Chapter 2: C Shell – Intermediate

Chapter 3: C Shell – Advanced

Chapter 4: Bash – Beginner

Chapter 5: Bash – Intermediate

Chapter 6: Bash – Advanced

Chapter 7: Basics

Chapter 9: Variables and Arrays

Chapter 10: Special Shell Variables

Chapter 11: Operators and Shell Substitutions

In this blog, you will find answers to the following 10 questions most definitely asked in Unix Shell programming interviews –

1: What will the output of the following commands be? Explain.

     set names =(Kathrin Chris Jacob)

     shift names

     echo $#names

2: What do the following lines do? Explain the differences.

     ls > filename

     ls >! Filename

3: Use awk mathematical functions to calculate the area of a circle (area= PI*rad2)

4: How would the shell interpret the command “echo ??a”?

5: Describe the “and” list execution.

6: Explain whiptail, man and grep.

7: Explain env.

8: Does = and -eq function similarly?

9: What are the functions of echo_style()?

10: What are the file test operators in shell?

1: What will the output of the following commands be? Explain.

     set names =(Kathrin Chris Jacob)

     shift names

     echo $#names

Answer:

The output will be 2.

shift command gets rid of the first element of the array names. So,the echo command will display 2 as the number of elements of the array.

2: What do the following lines do? Explain the differences.

     ls > filename

     ls >! Filename

Answer:

In both forms, the output of ls command is redirected to filename.If filename does not exist, it will be created, otherwise it will be truncated. When the first form is used, if shell parameter noclobber is set and filename is an existing file, an error results. The ‘!’ in the second form is used to suppress that check.

3: Use awk mathematical functions to calculate the area of a circle (area= PI*rad2)

Answer:

alias mathcalc ‘ awk “BEGIN{ print \!* }” ‘

set rad = 1.3

set circle_area = `mathcalc atan2(0,-1)*${rad}*${rad}`

Explanation: The alias defines a kind of mathematical calculator called mathcalc, which uses the “one-liner” awk to perform calculations (limited to the set of mathematical functions available

in awk). Then, this calculator is used to calculate the circle’s area. It uses the arctangent function atan2 that evaluates PI.

4: How would the shell interpret the command “echo ??a”?

Answer:

Before starting the execution of echo, the shell will replace the two ??. For the shell, the ? represents any one character in a filename. But the first dot in a filename cannot be substituted by any character and should be given literally. This command would output any files and subdirectories in the current directory, which have a name comprising of three characters, except the ones starting with a dot.

5: Describe the “and” list execution.

Answer:

An “and” list is a way of chaining together commands. The syntax is:

command 1 && command 2 && … command-n

Each command is executed in turn, provided the previous one has given an exit value of TRUE. At the first FALSE return value, the chain terminates, and the remaining commands do not get executed.

 

 

 

GRAB YOUR COPY

 

 

6: Explain whiptail, man and grep.

Answer:

A whiptail in shell script lets you show user-friendly dialogue boxes to display information or to take user input. The man command is like the help and info commands used to seek help. The difference is that man displays the roffhelp (roff being the first UNIX program with text formatting). The grep is a utility that’s used to search for information in pipes or files. It is used for text search along with other commands, such as the ls command. Suppose you are looking for a file with mynm in its name, you can redirect the output of ls command to a grep command with the text to search for and you will get the file which has it.

7: Explain env.

Answer:

The env command is used to change a few environment variables only for running a particular script without changing the system environment. You can set the env in the shebang line. For example, you can set the scripting language to Perl for a particular script only using env instead of changing the environment settings.

8: Does = and -eq function similarly?

Answer:

The = is usually used as the assignment operator, that is, to assign values to a variable. The == is used for comparison. The -eq command is also used for comparison. The difference is that while == does a lexical or string comparison, -eq does a numeric comparison.

9: What are the functions of echo_style()?

Answer:

a) When set to ‘bsd’, if the first argument is ‘-n’, new line will not be displayed

b) When set to ‘sysv’, it interprets backslash as escape sequence characters

c) When set to ‘bsd’, it accepts both ‘-n’ and escape sequence

10: What are the file test operators in shell?

Answer:

The file test operators in shell are used to check the files for specific properties. Here are some:

a) -b returns TRUE if the file is a block special file

b) -c returns TRUE if the file is a character file special

c) -d returns TRUE if it is a directory

d) -f returns TRUE if it is a normal file

e) -r returns TRUE if it has readable permission

f) -w returns TRUE if it has writable permission

g) -x returns TRUE if it has executable permission

h) -e returns TRUE if it exists either as a file or folder

Kickstart your career in UNIX Shell programming by clearing all job interviews you sit for.

All the best!