Unix Shell Scripting Interview Questions

What is UNIX?

It is a portable operating system that is designed for both efficient multi-tasking and multi-user functions. Its portability allows it to run on different hardware platforms. It was written in C and lets the user do processing and control under a shell.

What is Shell?

A shell acts as an interface between the user and the system. As a command interpreter, the shell takes commands and sets them up for execution.

What is Zombie process in UNIX? How do you find Zombie process in UNIX?

When a program forks and the child finishes before the parent, the kernel still keeps some of its information about

the child in case the parent might need it for example, the parent may need to check the child’s exit status. To be

able to get this information, the parent calls ‘wait()’; In the interval between the child terminating and the parent

calling ‘wait()’, the child is said to be a ‘zombie’ (If you do ‘ps’, the child will have a ‘Z’ in its status field to indicate this.)

What is nohup in UNIX?

Nohup is a special command which is used to run process in background, but it is slightly different than & which is normally used for putting a process in background. An UNIX process started with nohup will not stop even if the user who has stared log off from system. While background process started with & will stop as soon as user logoff.

What type of buffer is supported by Unix.

  • Fully Buffered.
  • Line Buffered.
  • Un-Buffered.

What are signals?

Signals are software interupts . Signal provide a way of handling asynchronous events: a user at a terminal typing the iterrupt key to stop a program or the next program in the pipeline termnating prematurely.

What is session?

A Session is a collection of one or more process group. A process establish a new session by calling setsid function. This function return process groupid ok.

What is ‘ps’ command for?

Answer: The ps command prints the process status for some or all of the running processes. The information given are the process identification number (PID),the amount of time that the process has taken to execute so far etc.

How are devices represented in UNIX?

All devices are represented by files called special files that are located in/dev directory. Thus, device files and other files are named and accessed in the same way. A ‘regular file’ is just an ordinary data file in the disk.

What are the different type of variables used in a Shell Script ?

In a shell script we can use two types of variables :

  • System-defined variables
  • User-defined variables

System-defined variables are defined or created by Operating System(Linux) itself. These variables are generally defined in Capital Letters and can be viewed by “set” command.

How to use Argument in a script?

First argument: $1,

Second argument : $2

Example : Script will copy file (arg1) to destination (arg2)

./copy.sh file1.txt /tmp/

cat copy.sh

#!/bin/bash

cp $1 $2

How to check if previous command run successful?

$?

How to check if file exist on filesystem?

if [ -f /var/log/messages ]

then

echo “File exists”

What it means #!/BIN/SH OR #!/BIN/BASH at beginning of every script?

That line tells which shell to use. #!/bin/bash script to execute using /bin/bash. In case of python script there there will be #!/usr/bin/python.

What is the first symbol in the bash script file?

#

What difference between & And && ?

  • & – we using it when want to put script to background
  • && – when we want to execute command/script if first script was finished successfully.

What would be the output of command: ECHO ${NEW:-VARIABLE}?

  • Variable

How to redirect STDOUT and STDERR Streams to LOG.TXT file from the script inside?

Add “exec >log.txt 2>&1” as the first command in the script.

How to get HOME_DIR with echo command only if string variable=”USER:123:321:/HOME/DIR” is given?

echo ${variable#*:*:*:}

or

echo ${variable##*:}

How to list users which UID less than100(awk)?

awk -F: ‘$3<100’ /etc/passwd

How to change standard field separator to“:” in bash shell?

IFS=”:”

How to print all arguments provided to the script?

echo $*

or

echo $@

How to define array in bash?

array=(“Hi” “my” “name” “is”)

How will you pass and access arguments to a script in Linux?

Arguments can be passed as:

scriptName “Arg1” “Arg2”….”Argn” and can be accessed inside the script as $1 , $2 .. $n.

How do you write the contents of 3 files into a single file?

cat file1 file2 file3 > file

Write a command to find the sum of bytes (size of the file) of all files in a directory?

ls -l | grep ‘^-‘| awk ‘BEGIN {sum=0} {sum = sum + $5} END {print sum}’

Write a command to select only those lines containing “july” as a whole word?

grep -w july filename

The ‘-w’ option makes the grep command to search for exact whole words. If the specified pattern is found in a string, then it is not considered as a whole word. For example: In the string “mikejulymak”, the pattern “july” is found. However “july” is not a whole word in that string.

Write a command to duplicate each line in a file?

sed ‘p’ < filename

Write a command to list the files in ‘/usr’ directory that start with ‘ch’ and then display the number of lines in each file?

wc -l /usr/ch*

Another way is

find /usr -name ‘ch*’ -type f -exec wc -l {} \;

Write a command to remove the prefix of the string ending with ‘/’?

The basename utility deletes any prefix ending in /. The usage is mentioned below:

basename /usr/local/bin/file

This will display the only file.

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Unix Shell Scripting Interview Questions"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Support


    Please leave a message and we'll get back to you soon.
    Three + 6