Sunteți pe pagina 1din 19

1) What is the description for Kernel?

It is the core & master program of UNIX operating system that controls the HW & SW resources of the computer. The resources

allocation to different users and tasks handle by this section. Kernel do not have direct communication with the user and it starts

separate interactive program call shell to each user when login to the system.

2) What is a single user system?

A personal computer with an operating system which was designed to operate by a single user at a given time. Single user system

becomes more popular since low-cost hardware and availability of wide range of software to perform difference tasks.

3) What are the main features of UNIX?

Machine independent Portability Multi-user operations Unix Shells Hierarchical file system Pipes and filters Background processors Utilities Development tools.

Portable(Multiplatform)

Multitasking

Multi User

Multiprocessor (SMP) Support

Multithreading Support

Virtual Memory

Hierarchical File System

Graphical User Interface (X Window System, Wayland)

 

Wide Hardware Support

Dynamically Linked Shared Libraries as well as Static Libraries

POSIX Compliant (Almost)

Multiple Virtual Consoles

Multitple Filesystem Support

Multiple Networking Protocols (TCP/IP, IPX/SPX, Appletalk,

AX.25)

Shell

Strong Security Model

Open Source

4) What is called Shell? -It’s a command line interpreter, interface between user and system. Shell accepts commands and set them to execute for user operations.

5) What are the responsibilities of a shell? Program Execution Input/output redirection Filename and variable substitution Pipeline hookup Environment control Integrated programming language

6) What is the general format of UNIX command syntax?

In general consideration, UNIX shell commands follows the following pattern

Command (-argument) (-argument) (-argument) (file name)

7) Describe the usage and functionality of the command “rm –r *” in UNIX? The command “rm –r *” is a single line command to erase all files in a directory with its subdirectories. “rm” – Is for deleting files.

-r”

Is to delete directories and subdirectories with files within.

“*”

Is indicate all entries.

8) Describe the term directory in UNIX?

A specialized form of a file that maintains a list of all the files which include in it is called a directory. Each file assigns to a directory.

9) Specify the difference between absolute path and related path? Absolute path refers to the exact path as defined from the root directory. Related path refers to the path related to the current locations.

10) What is the UNIX command to list files/folders in alphabetical order? The ‘ls –l’ command is used to list down files and folders in alphabetical order. When you use ‘ls –lt’ is list down files /folders sorted with modified time.

11) Describe links and symbolic links in UNIX?

Ans) The second name for a file called a Link. It is used to assign more than one name for a file. It is not valid to assign more than one name to a directory or to link filenames on different computers. General command ‘– ln filename1 filename2’

A symbolic link is the files that use to contain only the name of other files include in it. Directed to the files pointed by it is the

operation of the symbolic link. General command ‘– ln -s filename1 filename2’

Q #12) What is the FIFO?

Ans) FIFO (First In First Out) is also called named pipes and it is a special file for date transient. Data is read only in the written order. This is use to inter-process communications, where data write to one end and reads from another end of the pipe.

Q #13) Describe fork() system call?

Ans) The command use to create a new process from an existing process is called fork().The main process is called parent process and new process id called child process. The parent gets the child process id returned and the child gets 0. The returned values are used to check which process which code executed. The returned values are used to check which process which code executed.

Q 14) Explain the following sentence?

It is not advisable to use root as the default login. The root account is very important and it can leads to system damage easily with abusive usage. So that the securities that normally apply to user accounts are not applicable to the root account.

Q 15) What is mean by Super User?

The user with access to all files and commands within the system is called super user. Generally, super user login is to root and the

login is secured with the root password.

Q 16) What is process group?

A collection of one or more processes is called process group. There is a unique process id for each process group. The function

“getpgrp” returns the process group ID for the calling process.

Q 17) What are the different file types available with UNIX?

Regular files Directory files Character special files Block special files

FIFO

Symbolic links

Socket

Q 18) What is the behavioral difference between “cmp” and “diff” commands? Both commands for file comparison. Cmp Compare given two files with byte by byte and display the first mismatch. Diff Display changes that need to done to make both file identical.

Q 19) What are the duties of following commands?

Ans) chmod, chown, chgrp chmod Change the permission set of the file. chown Change ownership of the file.

chgrp Change group of the file.

Q #20) What is the command to find today’s date? Ans) The command “date” use to retrieve current date.

Ans) The command “date” use to retrieve current date. Q #21) What is the purpose of

Q #21) What is the purpose of the following command?

date. Q #21) What is the purpose of the following command? Ans) The command is to

Ans) The command is to display the first part of the file names README.txt which just fit as much as on one screen.

Q #22) Describe the zip/unzip command using gzip?

Ans) gzip command creates a zip file using given the filename in the same directory.

a zip file using given the filename in the same directory. gunzip command unzip the file.

gunzip command unzip the file.

in the same directory. gunzip command unzip the file. Q #23) Explain the method of changing

Q #23) Explain the method of changing file access permission?

Ans) There are three sections to consider when creating/changing file access permission.

File owner’s user ID File owner’s group ID File access mode to define These three parts arrange as follows.

(User permission) (Group permission) (other permission) Three types of permission can define.

r Reading permission

w Writing permission

x Execution permission

Q #24) How to display the last line of a file?

Ans) This can perform using either “tail” or “sed” commands. The easiest way is to use “tail” command.

commands. The easiest way is to use “tail” command. In the above example code, the last

In

the above example code, the last line of the README.txt is displayed.

Q

#25) What are the various IDs in UNIX processes?

Ans) Process ID is a unique integer that UNIX uses to identify each process. The process executes to initiate other processes is called

parent process and its ID is defined as PPID (Parent Process ID). getppid() Is to retrieve PPID Every process is associated with a specific user and is called the owner of the process. The owner has all the privileges over the

process. The owner is also the user who executes the process. Identification for a user is User ID. The process also associated with Effective User ID which determines the access privileges to accessing resources like files. getpid() Retrieve process id getuid() Retrieve user id geteuid() Retrieve effective user id

Q #26) How to Kill a process in UNIX?

Ans) The Kill command accepts process ID (PID) as an in a parameter. This is applicable only for the processes own by the command

executor. Syntax kill PID

Q #27) Explain the advantage of executing processes in the background?

Ans) The general advantage is to execute processes in the background is to get the possibility to execute some other process without waiting for the previous process to get completed. The symbol “&” at the end of the process tells to the shell to execute given a command in the background.

Q #28) What is the command to find maximum memory taking process on the server?

The command top displays the CPU usage, process id, and other details.

Command

the CPU usage, process id, and other details. Command Output Q #29) What is the command

Output

the CPU usage, process id, and other details. Command Output Q #29) What is the command

Q #29) What is the command to find hidden files in the current directory?

And) ‘ls –lrta’ is to display hidden files in current directory.

Command

is to display hidden files in current directory. Command Output Q #30) What is the command

Output

to display hidden files in current directory. Command Output Q #30) What is the command to

Q #30) What is the command to find the currently running process in Unix Server?

Ans) “ps –ef” command is used to find current running process. Also “grep” with a pipe can use to find specific process.

Command

with a pipe can use to find specific process. Command Output Q #31) What is the

Output

with a pipe can use to find specific process. Command Output Q #31) What is the

Q #31) What is the command to find remaining disk space in UNIX server?

Ans) The command “df -kl” use to get a detail description on disk space usage. Command

server? Ans) The command “df - kl” use to get a detail description on disk space

Output

Q #32) What is the UNIX command to make a new directory? Ans) “mkdir directory_name”

Q #32) What is the UNIX command to make a new directory?

Ans) “mkdir directory_name” is used to create a new directory.

Command

is used to create a new directory. Command – Output – Q #33) What is the

Output

is used to create a new directory. Command – Output – Q #33) What is the

Q #33) What is the UNIX command to confirm a remote host is alive or not?

Ans) Either “ping” or “telnet” command can use to confirm a remote host alive or not.

Q #34) What is the method to see command line history?

Ans) The “history” command display all the command used previously within the session.

Command

the command used previously within the session. Command – Output – Q #35) Discuss the difference

Output

used previously within the session. Command – Output – Q #35) Discuss the difference between swapping

Q #35) Discuss the difference between swapping and paging?

Ans) Swapping Complete process is moved to main memory for execution. To provide the memory requirement, process size must be less than the available main memory capacity. The implementation is easy but is an overhead to the system. Memory handling is not more flexible with swapping systems.

Paging Only the required memory pages are moved to the main memory for execution. The size of the process does not a matter

of execution and it no needs to be less than available memory size. Allow a number of processes to load to main memory

simultaneously.

Q #36) What is the command to find weather system is 32 bit or 64 bit?

Ans) “arch” or “uname -a” can use for this process. Command and outcome (Note: Click on the image for enlarged view)

and outcome ( Note: Click on the image for enlarged view) Q #37) Explain ‘nohup’ in

Q #37) Explain ‘nohup’ in UNIX?

Ans) “nohup” is a special command that is available to run a process in the background. The process started with ‘nohup’ command

is not terminating even the user started to log off from the system.

Q #38) What is the UNIX command to find how many days the server is up?

Ans) “uptime” command returns the number of dates that the server is up.

command returns the number of dates that the server is up. Q #39) What is the

Q #39) What is the mode that fault handler executes?

Ans) At the Kernel mode.

Q #40) What is the purpose of “echo” command?

Ans) “echo” command is similar to “ls” command and it displays all the files in current directory.

Q #41) What is the explanation for protection fault?

Ans) When the process access a page, which do not have access permission is refers as protection fault. Also when a process

attempt to write on a page whose copy on write bit was set during the fork() system call is incurred for protection fault.

Q #42) What is the method to edit a large file without opening it in UNIX?

Ans) The “sed” command is available for this process ‘.sed’ stands for a team editor. Example

process ‘.sed’ stands for a team editor. Example – Above code will replace from the README.txt

Above code will replace from the README.txt file.

– Above code will replace from the README.txt file. Q #43) Describe the concept “Region”? Ans)

Q #43) Describe the concept “Region”?

Ans) Continuous area of processes address space (text, data, and stack) is identifying as a region. Regions are shareable among the

processes.

Q #44) What is mean by user area (u-area, u-block)?

Ans) The area is only manipulated by the kernel and it contains the private data. This is unique to the process and each process

allocated to u-area.

Q #45) What is called piping?

Ans) “piping” is used to combine two or more commands together. The output of the first command work as the input of the second

command, and so on. Pipe character ( | ) is represent piping.

Q #46) What is the process to count the number of characters and line in a file?

Ans) “wc – c filename” command can use to retrieve the number of characters in a file and “wc –l filename” command can use to retrieve the number of lines in a file.

command can use to retrieve the number of lines in a file. Above command returns the

Above command returns the number of characters in README.txt file.

command returns the number of characters in README.txt file. Above command returns the number of characters

Above command returns the number of characters in README.txt file.

UPDATE: Added more commonly asked Unix questions.

Q #47) What do you understand by UNIX shell?

UNIX shell serves as an environment to run commands, programs, and shell scripts and also acts as an interface between the user

and the Unix operating system. Shell issues “$” as the command prompt, which reads input and determines the command to execute. For example: $date This command will display current date and time. Some of the most famous shells available with most of Unix variants are Bourne Shell, Korn shell, C Shell.

Q #48) Explain the term filter.

A Filter is described as a program, which takes input from the standard input, and displays results to the standard output by performing some actions on it.

Standard Input could be text typed on the keyboard, input from other file or output of other file serving as input. Standard output is

y default the display screen.

The most popular example of Unix filter id grep command. This program look for a certain pattern in a file or list of files and only

those lines are displayed on the output screen which contains the given pattern. Syntax: $grep pattern file(s) Some of the options that are used along with grep command are enlisted below:

-v: prints line that does not match the pattern. -n: print matched line and line number.

-l: print file names with matching lines.

-c: prints only the count of matching lines.

-i:

matches either uppercase or lowercase.

Q

#49) Write a command to erase all files in the current directory including all its subdirectories.

“rm –r*” is the command used to erase all files in the current directory including all its subdirectories. rm: this command is used for deleting files.

-r : this option will erase all files in directories and sub-directories. ‘*’: this represents all entries.

Q #50) What do understand by Kernel?

Unix operating system is basically divided into three parts, namely, the kernel, the shell and the commands and utilities. Kernel

serves as the heart of the Unix operating system which does not deal directly with the user but rather act as a separate interactive program for users logged in.

It

performs the following functions:

a)

Interacts with the hardware

b)

Perform tasks like memory management, file management and task scheduling.

c)

Control computer resources

d)

Help allotting resources to different tasks and users.

Q

#51) Describe key features of Bourne shell.

Bourne shell is referred to as the standard shell. The default prompt here is ‘$’ character.

The key features of Bourne shell includes:

a)

Input/ Output redirection.

b)

Use of Metacharacters for file name abbreviations.

c)

Using shell variables for the customizing environment.

d)

Creation of programs using built-in the command set.

Q

#52) Enlist the key features of Korn Shell.

The Korn shell is the most advanced as well as an extension to the Bourne Shell which is backward- compatible.

Some of the features of Korn shell are listed below:

a)

Perform command line editing.

b)

Maintains command history so that the user can check the last command executed if required.

c)

Additional flow control structures.

d)

Debugging primitives who help programmers debug their shell code.

e)

Support for arrays and arithmetic expressions.

f)

Ability to use aliases which is defined as the shorthand names for command.

Q

#53) What do you understand by shell variables?

A

variable is defined as a character string to which a value is assigned, where values could be the number, text, filename, etc. The

shell maintains the set of internal variables as well as enables deletion, assignment, and the creation of variables. Thus the shell variables are a combination of identifiers and assigned values that exist within the shell. These variables are local to the shell in which they are defined as well as they work in a particular way. They may have default value or values can be assigned manually by using appropriate assignment command.

To define a shell variable, ‘set’ command is used.

To delete a shell variable, ‘unset’ command is used.

Q #54) Describe responsibilities of Shell in brief.

Apart from analyzing the input line as well as initiating the execution of the program entered by the user, Shell also serves various responsibilities. Find below a brief description:

a) The shell is responsible for the execution of all the programs by analyzing the line and determining the steps to be performed and

then initiate the execution of the selected program.

b) Shell allows you to assign values to the variables when specified on the command line. It also performs Filename substitution.

c) To take care of input and output redirection.

d) Performs pipeline hook-up by connecting the standard output from the command preceding the ‘|’ to the standard input of the

one following ‘|’.

e)

Provides certain commands to customize and control environment.

f)

Has its own built-in integrated programming language which is typically easier to debug and modify.

Q

#55) Explain file system in UNIX.

A Filesystem in Unix is referred to as a functional unit or a logical collection of files, where the disk is set aside to store files and inode

entries. This file system consists of the files that are organized into a multi-level hierarchy called a directory tree.

In other words, the file system is a collection of files and directories and has few below-mentioned features:

a) The very top of the file system is defined as the single directory called ‘root’ that contains other files and directories and is

represented by slash (/).

b) These are self-independent and have no dependencies on other file systems.

c) Every file and directory are uniquely identified by:

Name

Directory in which it resides

A

unique identifier

d)

All files are organized into a multi-level directory known as ‘Directory tree’.

Q

#56) What do you understand by command substitution?

Command substitution is the method which is performed every time the commands that are enclosed in backquotes are processed y

the shell. This process replaces the standard output and displays on the command line. Command substitution can perform the following tasks:

Invoke subshell Result in word splitting Remove trailing new lines By using ‘redirection’ and ‘cat’ command, allows setting a variable to the content of the file. Allows setting a variable to the output of the loop

Q 57) Define inode.

Whenever a file is created inside a directory, it accesses the two attributes, namely, file name and Inode number. The file name is first mapped with Inode number stored in the table and then this Inode number serves as a medium to access Inode. Thus Inode can be defined as an entry created and set aside on a section of the disk for a file system. Inode serves as a data structure and nearly stores each information that is required to be known about a file. This information includes:

a) File location on the disk

b) Size of the file

c) Device Id and Group Id

d) File mode information

e)

File protection flags

f)

Access privileges for owner, group.

g)

Timestamps for file creation, modifications, etc.

Q

58) Enlist common shells with their indicators.

Enlisted below are the common shells with their indicators:

Shell

Indicators

Bourne Shell

sh

C Shell

csh

Bourne Again shell Bash

Enhanced C shell

tcsh

Shell

Indicators

Z

Shell

zsh

Korn Shell

ksh

Q #59) Enlist some commonly used network commands.

Some commonly used networking commands in Unix are enlisted below:

a) telnet: it is used for remote login as well as for communication with another hostname.

b) ping: it is defined as an echo request for checking network connectivity.

c) su: derived as a user switching command.

d) hostname: determines the Ip address and domain name.

e) nslookup: performs DNS query.

f) xtraceroute: method to determine the number of hoops and response time required to reach the network host.

g) netstat: it provides a lot of information like ongoing network connection on the local system and ports, routing tables, interfaces

statistics, etc.

Q #60) How is cmp command different from diff command?

‘cmp’ command is basically used for byte by byte comparison of two files to determine the first mismatch byte. This command does not use the directory name and displays the first encountered mismatched byte.

Whereas, ‘diff’ command’ determines the changes that are to be performed on the files in order to make the two files identical. In this case, directory names can be used.

Q #61) What is the role of the superuser?

There are basically three types of accounts in Unix operating system:

Root account System accounts User accounts ‘Root account’ is basically referred to as a ‘Super user’. This user has completely open access or says control on all files and commands on a system. This user can also be assumed as a system administrator and thus has the ability to run any command without any restriction. It is protected by the root password.

Q #62) Define piping.

When two or more commands are required to be used at the same time as well as run them consecutively, ‘piping’ process is used. Here two commands are connected so that, the output of one program serves as the input for another program. It is denoted by the symbol ‘|’. Enlisted below are few commands where piping is used:

grep command: searches files for certain matching pattern. sort command: arranges lines of text alphabetically or numerically.

Q #63) Explain the types of path names that can be used in UNIX.

In a file system for any operating system, there exists the hierarchy of directories, there ‘Path’ is defined as the unique location to a file/ directory to access it. There are basically two types of the path that are used in Unix. These can be defined as follows:

There are basically two types of the path that are used in Unix. These can be defined as follows:

a) Absolute Pathname: It defines a complete path specifying the location of a file/ directory from the very start of the actual file system i.e. from the root directory (/). Absolute pathname addresses system configuration files that do not change location. It defines a complete path specifying the location of a file/ directory from the very start of the actual file system i.e. from the root directory (/). Absolute pathname addresses system configuration files that do not change location. b) Relative Pathname: It defines the path from the current working directory where the user is i.e. the present working directory (pwd). Relative pathname signifies current directory, parent directory as well as also refers to file that are either impossible or inconvenient to access. It defines the path from the current working directory where the user is i.e. the present working directory (pwd). Relative pathname signifies current directory, parent directory as well as also refers to file that are either impossible or inconvenient to access.

Q #64) Explain Superblock in UNIX?

Each logical partitions in Unix are referred to as File system and each file system contains, a ‘boot block’, a ‘super block’, ‘inodes’, and ‘data blocks’. Super block is created at the time of creation of file system. It describes the following: Super block is created at the time of creation of file system.

It describes the following:

State of the file system Total size of the partition Block size Magic number Inode number of the root directory Count of the number of files, etc There are basically two types of superblocks:

a) Default superblock: It has its existence always as a fix offset from the beginning of the system’s disk partition.

b)

Redundant superblock: It is referenced when the default superblock is affected by system crash or some errors.

Q

#65) Enlist some filename manipulation commands in UNIX.

Some Filename manipulation commands along with their description is enlisted below in table:

Command

cat filename

cp source destination

Description

Displays contents of the file

Used to copy source file into the destination

mv

old name new name

Move/rename and old name to the new name

rm

filename

Remove/delete filename

Touch filename

In [-s] old name new name Creates soft link on old name

Is F

Changing modification time

Displays information about file type

Q #66) Explain links and symbolic links.

Links are defined as a second name which is used to assign more than one name to a file. Although links are referred to as a pointer to another file it cannot be used to link filenames on different computers.

A Symbolic link is also known as the soft link is defined as a special type of file that contains links or references to another file or

directory in the form of absolute or relative path. It does not contain the data actually in the target file but the pointer to another entry in the file system. Symbolic links can also be used to create a file system. The following command is used to create a symbolic link:

Ln s target link_name Here, path is ‘target’ Name of the link is represented by link_name.

Q #67) Explain alias mechanism.

To avoid typing long commands or to improve efficiency, the alias command is used to assign another name to a command. Basically, it acts as a shortcut to the larger commands which can be typed and run instead. For creating an alias in Unix, following command format is used:

alias name=’command you want to run’ Here, replace the ‘name’ with your shortcut command and replace ‘command you want to run’ with the larger command of which you want to create an alias of.

For example:

alias dir ‘Is –sFC’

here, in the above example ‘dir’ is the another name for the command ‘Is-sFC’. Thus user now simply is required to remember and use the specified alias name and the command will perform the same task as to be performed by the long command.

Q #68) What do you know about wildcard interpretation?

Wildcard characters are some special kind of characters that represent one or more other characters. Wildcard interpretation comes into picture when a command line contains these characters. In this case, when the pattern matches the input command, these characters are replaced by sorted list of files. Asterisk(*) and Question mark(?) are usually used as wildcard characters to set up a list of files while processing. Q #69) What do you understand by terms ‘system calls’ and ‘library functions’ with respect to UNIX command? System calls: As the name implies, system calls are defined as an interface which basically is used in the kernel itself. Although, they may not be fully portable but these calls request the operating system to perform tasks on behalf of user programs. The system calls appear as a normal C function. Whenever a system call is invoked within the operating system, the application

program performs context switch from user space to kernel space.

Library functions: The set of common functions that are not part of the kernel but is used by the application programs are known as ‘Library functions’. As compared to system calls, library functions are portable and can perform certain tasks only in ‘kernel mode’. Also, it takes lesser time for execution as compared to the execution of system calls.

Q #70) Explain pid.

A pid is used to denote unique process id. It basically identifies all the processes that run on the Unix system. It does not matter

whether the processes are running in the foreground or in the background.

Q #71) What are the possible return values of kill() system call?

Kill() system call is used to send signals to any processes. This method returns the following return values:

a) Returns 0 : It implies that process exists with the given pid and system allows sending signals to it.

b) Return -1 and errno==ESRCH: It implies that there is no existence of the process with specified pid. There may also exist some

security reasons which is denying the existence of the pid.

c)

Return -1 and errno==EPERM: It implies that there is no permit available for the process to be killed. The error also detects

whether the process is present or not.

d)

EINVAl : it implies invalid signal.

Q

#72) Enlist the various commands that are used to know about the user information in UNIX.

The various commands that are used for displaying the user information in Unix are enlisted below:

a)

Id: displays the active user id with login and group.

b)

Last: displays the last login of the user in the system.

c)

Who: determines who is logged onto the system.

d)

groupadd admin: this command is used to add group ‘admin’.

e)

Usermod a: user to add existing user to the group.

Q

#73) What do know about tee command and its usage?

‘tee’ command is basically used in connection with pipes and filters. This command basically performs two tasks:

a)

Get data from standard input and send it to the standard output.

b)

Redirects a copy of the input data to the specified file.

Q

#74) Explain mount and unmount command.

Mount command: As the name suggests, the mount command mounts a storage device or file system onto an existing directory and thus making it accessible to users.

Unmount command: This command unmounts the mounted file system by safely detaching it. It also the task of this command to inform the system to complete any pending read and write operations. Q #75) What is “chmod” command? Chmod command is used to change file or directory access permission and is the most frequently used command in Unix. According to mode, chmod command changes the permission of each given file. The syntax of chmod command is:

Chmod [options] mode filename. Here in the above format, options could be:

-R: recursively change the permission of file or directory. -v: verbose, i.e. output a diagnostic for every file processed. -c: report only when the change is made. Etc.

Q #76) Differentiate Swapping and Paging.

The difference between Swapping and Paging can be seen in the below table:

Swapping

It the procedure of copying the entire process from main memory onto secondary memory.

For execution, the whole process is moved from swap device to the main memory.

The than main memory.process size must be equal to or less

It cannot handle the memory flexibly.

Best Shell Scripting Interview Questions

Paging

It is a memory allocation technique where the process is allocated memory wherever available.

For execution, only the required memory pages are moved from swap device to the main memory.

The process size does not matter in this case.

It can handle the memory more flexibly.

Q #1) What is Shell?

Ans: Shell is a command interpreter, which interprets the command which the user gives to the kernel. It can also be defined as an

interface between a user and operating system.

Q #2) What is Shell Scripting?

Ans: Shell scripting is nothing but series or sequence of UNIX commands written in a plain text file. Instead of specifying one job/command at a time, in shell scripting we give a list of UNIX commands like a to-do list in a file to execute it.

Q #3) What is the Importance of writing Shell Scripts?

Ans: 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 own commands.

It is helpful in automating some tasks of the day to day life.

It is useful for automating system administration tasks. Mainly it saves time.

Q #4) List some of the common and most widely used UNIX commands.

Ans: Given below is a list of widely used UNIX Commands.

Command

Example/Usage of Command

1. $ ls

 

ls

2. $ ls lrt or $ ls -ltr

1. $ cd

 

cd

2. $ cd test

3. $ cd

(after cd space needs to be given before

entering two dots.)

mkdir

$ mkdir test

 

$ rmdir test1

rmdir

CAUTION: Be careful while using this command.

1. $ cp file1 test

cp

2. $ cp file1 file1.bak

$

rm file1

rm

CAUTION: Be careful while using this command.

mv

$ mv file1 file2

more

$ more

 

touch

$ touch test

1. $ cat File1

cat

2. $ cat test1 > test2

compress

$ compress file1

$ date

 

date

e.g. Output:

Tuesday, September 12, 2017 06:58:06 AM MDT

diff

$

diff file1 file2

find

$

find . –name ‘*.t’ -print

finger

$ finger

 

who

$ who

1.$ grep Hello file1

grep

2.$ grep c Hello file1

kill

 

kill

$

kill 1498

lpr

1.$ lpr Pprinter1 test 2.$ lp file1

man

$ man ls

 

passwd

$ passwd

$ pwd

pwd

e.g. Output: /u/user1/Shell_Scripts_2017

$

ps

e.g. Output:

 

ps

PID TTY TIME COMMAND

1498

3b 0:10 sh

1500

3b 0:05 sh

talk

$

talk user1

$

wc file1

wc

e.g. Output:

 

4 6 42 file1

Description

1. It lists files in the current directory.

2. It lists files in the long format.

1. It changes directory to your home directory.

2. It changes directory to test.

3. It moves back to one directory or to the parent directory of your

current directory.

It creates a directory called test.

It removes directory test1.

1. It copies file1 to test directory.

2. It takes backup of file1.

It removes or deletes a file1.

It moves or renames file1 to file2.

It checks or display one page at a time.

It creates an empty file called test.

1. It displays contents of File1.

2. It creates a new file test2 with the contents of test1.

It reduces the size of file1 and creates a compressed file called file1.z and deletes file1.

It displays current date and time.

It displays line by line difference between file1 and file2.

It searches in the current directory and in all its subdirectories for files ending with .t, and writes their names in the output.

It displays information about user.

It lists the users those who are logged in on the machine.

1. It searches for the lines containing Hello in file1.

2. It gives count or number of lines that contains Hello in file1.

It kills the process which is having PID as 1498.

1. It sends file test to print it on printer1.

2. It prints file1.

It displays online manual or help about ls command.

It is used to change the password.

It displays present working directory.

It displays the list of processes which are currently running on the machine.

It is used to talk to the user1 who is currently logged into the same machine.

It counts the number of lines, words and characters in file1.

Command

Example/Usage of Command

chmod

$ chmod 744 file1

gzip

$ gzip file1

gunzip

$ gunzip file1.gz

history

$ history

$ logname

Description

It changes the permissions of file1 & assigns this permission rwxr--

r--

It compresses the file1. After compression file1 should look like this, file1.gz

It uncompresses the file1.gz. After uncompression file1.gz should look like this, file1

It lists all the commands which are recently used.

logname

e.g. Output:

It prints log name of the user.

user1

$ uname

uname

e.g. Output:

It gives information about unix system which you are using.

SunOS

$ tty

tty

e.g. Output:

/dev/pts/1

sort

$ sort file1

head

$ head -15 file1

tail

$ tail -15 file1

It displays the device name of your terminal.

This will sort the contents of file1 and displays sorted output on the screen.

It displays first 15 lines of the file.

It displays last 15 lines of the file.

Q #5) Shell programs are stored in which file?

Ans: Shell programs are stored in a file called sh.

Q #6) What are the different Types of Shells available?

Ans: There are mainly 4 important types of shells that are widely used. And they include:

Bourne Shell (sh)

C Shell (csh)

Korn Shell (ksh)

Bourne Again Shell (bash)

Q #7) What are the Advantages of C Shell over Bourne Shell?

Ans: 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 command history feature. C shell remembers the previously typed command. Thus, it avoids typing the command again and again.

Q #8) In a typical UNIX environment how many kernels and shells are available?

Ans: In a typical UNIX environment, only one kernel and many shells are available.

Q #9) Is separate compiler required for executing a shell program?

Ans: A separate compiler is not required to execute a shell program. The shell itself interprets the command in the shell program and executes them.

Q #10) How many shell scripts come with UNIX operating system?

Ans: There are approximately 280 shell scripts that come with the UNIX operating system.

Q #11) When should shell programming/scripting not be used?

Ans: 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.

Q #12) Basis of shell program relies on what fact?

Ans: The basis of shell programming relies on the fact that UNIX shell can accept commands not just only from the keyboard but also from a file.

Q #13) What are the default permissions of a file when it is created?

Ans: 666 i.e. rw-rw-rw- is the default permission of a file when it is created.

Q #14) What can be used to modify File permissions?

Ans: File permissions can be modified using umask.

Q #15) How to accomplish any task via shell script?

Ans: Any task can be accomplished via shell script at the dollar ($) prompt and vice versa.

Q #16) What are Shell Variables?

Ans: Shell variables are the main part of shell programming or scripting. They mainly provide the ability to store and manipulate information within a shell program.

Q #17) What are the two types of Shell Variables? Explain in brief.

Ans: The two types of shell variables are:

#1) Unix Defined Variables or System Variables These are standard or shell defined variables. Generally, they are defined in CAPITAL letters. Example: SHELL This is a Unix Defined or System Variable, which defines the name of the default working shell. #2) User Defined Variables These are defined by users. Generally, they are defined in lower letters Example: $ a=10 –Here the user has defined a variable called ‘a’ and assigned value to it as 10.

Q #18) How are shell variables stored? Explain with a simple example.

Ans: Shell variables are stored as string variables. Example: $ a=10 In the above statement a=10, the 10 stored in ‘a’ is not treated as a number, but as a string of characters 1 and 0.

Q #19) What is the lifespan of a variable inside a shell script?

Ans: The lifespan of a variable inside shell script is only until the end of execution.

Q #20) How to make variables as unchangeable?

Ans: Variables can be made unchangeable using readonly. For instance, if we want variable a value to remain as 10 and not to be changed then we can achieve this using readonly.

Example:

$ a=10

$ readonly a

Q #21) How variables can be wiped out?

Ans: Variables can be wiped out or erased using the unset command.

Example:

$ a =20

$ unset a

Upon using the above command the variable ‘a’ and its value 20 get erased from shell’s memory. CAUTION: Be careful while using this unset command.

Q #22) What are positional parameters? Explain with an example.

Ans: 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. There are totally 9 positional parameters present i.e. from $1 to $9. Example: $ Test Indian IT Industry has grown very much faster

In the above statement, positional parameters are assigned like this.

$0 -> Test (Name of a shell program/script)

$1 ->Indian $2 -> IT and so on.

Q #23) What does the. (dot) indicate at the beginning of a file name and how should it be listed?

Ans: 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. But it will be present in the directory. And to list the hidden file we need to use a option of ls. i.e. $ ls a.

Q #24) Generally, each block in UNIX is how many bytes?

Ans: Generally, each block in UNIX is of 1024 bytes.

Q #25) By default, a new file and a new directory which is being created will have how many links?

Ans: New file contains one link. And a new directory contains two links.

Q #26) Explain about file permissions.

Ans: There are 3 types of file permissions as shown below:

Permissions Weight

r

read

4

w write

2

x

- execute

1

The above permissions are mainly assigned to owner, group and to others i.e. outside the group. Out of 9 characters first set of 3 characters decides/indicates the permissions which are held by the owner of a file. The next set of 3 characters indicates the permissions for the other users in the group to which the file owner belongs to.

And the last 3 set of characters indicate the permissions for the users who are outside the group. Out of the 3 characters belonging to each set, the first character indicates the “read” permission, the second character indicates “write” permission and the last character indicates “execute” permission. Example: $ chmod 744 file1 This will assign the permission rwxrrto file1.

Q #27) What is a file system?

Ans: The file system is a collection of files which contain related information of the files.

Q #28) What are the different blocks of a file system? Explain in brief.

Ans: Given below are the main 4 different blocks available on a file system.

File System

Block No.

Name of the Block

1st Block

Boot Block

2nd Block

Super Block

3rd Block

Inode Table

4th Block

Data Block

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 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 are stored in an Inode table. Data Block: This block contains the actual file contents.

Q #29) What are the three different security provisions provided by UNIX for a file or data?

Ans: 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 file level, it provides security by providing read, write & execute 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

Q #30) What are the three editors available in almost all the versions of UNIX?

Ans: The three editors are ed, ex & vi.

Q #31) What are the three modes of operation of vi editor? Explain in brief.

Ans: 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.

Q #32) What is the alternative command available to echo and what does it do?

Ans: tput is an alternative command to echo. Using this, we can control the way in which the output is displayed on the screen.

Q #33) How to find out the number of arguments passed to the script?

Ans: The number of arguments passed to the script can be found as shown below.

echo $ #

Q #34) What are control instructions and how many types of control instructions are available in a shell? Explain in brief.

Ans: 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.

Q #35) What are Loops and explain three different methods of loops in brief?

Ans: 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.

Q #36) What is IFS?

Ans: 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.

Q

#37) What is a Break statement and what is it used for?

Ans: 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.

Q #38) What is Continue statement and what is it used for?

Ans: 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.

Q #39) What are Metacharacters in a shell? Explain with some examples.

Ans: 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.

Example:

ls s* – It lists all the files beginning with character ‘s’.

Execution over Shell Interpreter/Editor

Output:

$ cat script1 > script2 Here output of cat command or script1 will go to a script2.

Execution over Shell Interpreter/Editor

Output:

$ ls; who This will execute ls first and then who. Execution over Shell Interpreter/Editor

Output:

Q #40) How to execute multiple scripts? Explain with an example.

Ans: In a shell, we can easily execute multiple scripts i.e. one script can be called from the other. What we have to do is, we need to mention the name of a script to be called when we want to invoke it. Example: In the below program/script upon executing the first two echo statements of script1, shell script executes script2. Once after executing script2, the control comes back to script1 which executes a pwd command and then terminates. Code for script1 Code for script2

and then terminates. Code for script1 Code for script2 Execution of script1 over Shell Interpreter/Editor Output

Execution of script1 over Shell Interpreter/Editor

script2 Execution of script1 over Shell Interpreter/Editor Output displayed on the Editor upon executing script1 Q

Output displayed on the Editor upon executing script1

Output displayed on the Editor upon executing script1 Q #41) Which command needs to be used

Q #41) Which command needs to be used to know how long the system has been running?

Ans: uptime command needs to be used to know how long the system has been running. Example: $ uptime Upon entering the above command at shell prompt i.e. $ uptime, the output should look something like this. 9:21am up 86 day(s), 11:46, 3 users, load average: 2.24, 2.18, 2.16 Execution over Shell Interpreter/Editor

2.24, 2.18, 2.16 Execution over Shell Interpreter/Editor Output: Q #42) How to find the current shell

Output:

2.24, 2.18, 2.16 Execution over Shell Interpreter/Editor Output: Q #42) How to find the current shell

Q #42) How to find the current shell which you are using?

Ans: We can find the current shell what we are using with echo $SHELL. Example: $ echo $SHELL Execution over Shell Interpreter/Editor

$ echo $SHELL Execution over Shell Interpreter/Editor Output: Q #43) How to find all the available

Output:

echo $SHELL Execution over Shell Interpreter/Editor Output: Q #43) How to find all the available shells

Q #43) How to find all the available shells in your system?

Ans: We can find all the available shells in our system with $ cat /etc/shells. Example: $ cat /etc/shells Execution over Shell Interpreter/Editor

$ cat /etc/shells Execution over Shell Interpreter/Editor Output: Q #44) How to read keyboard inputs in

Output:

/etc/shells Execution over Shell Interpreter/Editor Output: Q #44) How to read keyboard inputs in shell scripts?

Q #44) How to read keyboard inputs in shell scripts?

Ans: Keyboard inputs can be read in shell scripts as shown below,

Script/Code

can be read in shell scripts as shown below, Script/Code Execution over Shell Interpreter/Editor Output: Q

Execution over Shell Interpreter/Editor

below, Script/Code Execution over Shell Interpreter/Editor Output: Q #45) How many fields are present in a

Output:

Script/Code Execution over Shell Interpreter/Editor Output: Q #45) How many fields are present in a crontab

Q #45) How many fields are present in a crontab file and what does each field specify?

Ans: 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.

Q #46) What are the two files of crontab command?

Ans: 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.

Q #47) What command needs to be used to take the backup?

Ans: 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.

Q #48) What are the different commands available to check the disk usage?

Ans: 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.

Q #49) What are the different communication commands available in Unix/shell?

Ans: Basically, there are 4 different communication commands available in Unix/shell. And they are mail, news, wall & motd.

Q #50) How to find out the total disk space used by a specific user, say for example username is John?

Ans: The total disk space used by John can be found out as shown below.

du s/home/John

Q #51) What is Shebang in a shell script?

Ans: 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. Example: #!/bin/bash The above line also tells which shell to use. Q #52) What is the command to be used to display the shell’s environment variables?

Ans: Command to be used to display the shell’s environment variables is env or printenv.

Q #53) How to debug the problems encountered in shell script/program?

Ans: 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.

Q #54) How to know the variable length?

Ans: Variable length can be checked as shown below

$ {#variable}

Q #55) What is the difference between = and ==?

Ans: = -> This is used for assigning value to the variable. == -> This is used for string comparison.

Q #56) How to open a read-only file in Unix/shell?

Ans: Read-only file can be opened as shown below:

vi

R <File Name>

Q

#57) How can the contents of a file inside jar be read without extracting in a shell script?

Ans: The contents of the file inside a jar can be read without extracting in a shell script as shown below. tar tvf <File Name>.tar

Q #58) What is the difference between diff and cmp commands?

Ans: 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.

Q #59) Explain in brief about sed command with an example.

Ans: sed stands for stream editor. And it is used for editing a file without using an editor. It is used to edit a given stream i.e. a file or

input from a pipeline. Syntax: sed options file

Example:

Execution over Shell Interpreter/Editor

file Example: Execution over Shell Interpreter/Editor Here ‘s’ command present in sed will replace string

Here ‘s’ command present in sed will replace string Hello with Hi.

Output:

present in sed will replace string Hello with Hi. Output: Q #60) Explain in brief about

Q #60) Explain in brief about awk command with an example.

Ans: awk is a data manipulation utility or command. Hence, it is used for data manipulation.

Syntax: awk options File Name

Example:

Script/Code

Syntax: awk options File Name Example: Script/Code awk utility/command assigns variables like this. $0 ->
Syntax: awk options File Name Example: Script/Code awk utility/command assigns variables like this. $0 ->

awk utility/command assigns variables like this. $0 -> For whole line (e.g. Hello John) $1 -> For the first field i.e. Hello $2 -> For the second field Execution over Shell Interpreter/Editor

For the second field Execution over Shell Interpreter/Editor The above script prints all the 5 lines

The above script prints all the 5 lines completely.

Output:

Execution over Shell Interpreter/Editor The above script prints only first word i.e. Hello from each

Execution over Shell Interpreter/Editor

Execution over Shell Interpreter/Editor The above script prints only first word i.e. Hello from each line.

The above script prints only first word i.e. Hello from each line. Output:

Execution over Shell Interpreter/Editor The above script prints only first word i.e. Hello from each line.