Top Shell Scripting Interview Questions & Answers

Author Avatar
Junyangz 1月 02, 2019
  • What is a shell?
Shell is an interface between the user and the kernel. Even though there can be only one kernel; a system can have many shell running simultaneously. So, whenever a user enters a command through the keyboard, the shell communicates with the kernel to execute it and then display the output to the user.
  • What are the different types of commonly used shells on a typical Linux system?
csh,ksh,bash,Bourne. The most commonly used and advanced shell used today is "Bash".
  • What is the equivalent of a file shortcut that we have a window on a Linux system?
Shortcuts are created using "links" on Linux. There are two types of links that can be used namely "soft link" and "hard link".
  • What is the difference between soft and hard links?
Soft links are link to the file name and can reside on different filesytem as well; however hard links are link to the inode of the file and have to be on the same filesytem as that of the file. Deleting the original file makes the soft link inactive (broken link) but does not affect the hard link (Hard link will still access a copy of the file)
  • What is the significance of $#?
$# shows the count of the arguments passed to the script.
  • What is the difference between $* and $@?
$@ treats each quoted arguments as separate arguments but $* will consider the entire set of positional parameters as a single string.(for arg in $@)
  • Given a file, replace all occurrence of word “ABC” with “DEF” from 5th line till end in only those lines that contains word “MNO”
sed –n '5,$p' file1|sed '/MNO/s/ABC/DEF/'
  • How will you find the 99th line of a file using only tail and head command?
tail +99 file1|head -1
  • Print the 10th line without using tail and head command.
sed –n '10p' file1
  • Explain about “s” permission bit in a file?
"s" bit is called "set user id" (SUID) bit. "s" bit on a file causes the process to have the privileges of the owner of the file during the instance of the program.For example, executing "passwd" command to change current password causes the user to writes its new password to shadow file even though it has "root" as its owner.
  • I want to create a directory such that anyone in the group can create a file and access any person’s file in it but none should be able to delete a file other than the one created by himself.
We can create the directory giving read and execute access to everyone in the group and setting its sticky bit "t" on as follows:

```bash
mkdir direc1
chmod g+wx direc1
chmod +t direc1
```
  • What is the difference between $$ and $!?
$$ gives the process id of the currently executing process whereas $! Shows the process id of the process that recently went into the background.
  • What are zombie processes?
These are the processes which have died but whose exit status is still not picked by the parent process. These processes even if not functional still have its process id entry in the process table.
  • I want to monitor a continuously updating log file, what command can be used to most efficiently achieve this?
We can use tail –f filename. This will cause only the default last 10 lines to be displayed on std o/p which continuously shows the updating part of the file.
  • I have 2 files and I want to print the records which are common to both.
We can use "comm" command as follows:
`comm -12 file1 file2` ... 12 will suppress the content which are unique to 1st and 2nd file respectively.
  • What are the 3 standard streams in Linux?
0 - Standard Input 1 - Standard Output 2 - Standard Error
  • I want to read all input to the command from file1 direct all output to file2 and error to file 3, how can I achieve this?
command <file1 1>file2 2>file3
  • What will happen to my current process when I execute a command using exec?
"exec" overlays the newly forked process on the current process; so when I execute the command using exec, the command gets executed on the current shell without creating any new processes.
  • How will you emulate wc –l using awk?
awk 'END {print NR}' fileName
  • What is the difference between grep and egrep?
`egrep` is Extended grep that supports added grep features like "+" (1 or more occurrence of a previous character),"?"(0 or 1 occurrence of a previous character) and "|" (alternate matching)
  • How to set an array in Linux?
A=(element1 element2 element3 …. elementn)
  • What is the significance of $?
The command `$?` gives the exit status of the last command that was executed.
  • How do we delete all blank lines in a file?
sed '/^$/d' file
  • Write a command sequence to find all the files modified in less than 2 days and print the record count of each.
find . –mtime -2 –exec wc –l {} \;
  • What are the four fundamental components of every file system on Linux?
Bootblock, super block, inode block and Datablock are found fundamental components of every file system on Linux. Boot block contains a small program called "Master Boot record"(MBR) which loads the kernel during system boot up. Super block contains all the information about the file system like the size of file system, block size used by its number of free data blocks and list of free inodes and data blocks. Inode block contains the inode for every file of the file system along with all the file attributes except its name.
  • What is the < /dev/null & used for?
`< /dev/null` is used to instantly send EOF to the program, so that it doesn't wait for input (/dev/null, the null device, is a special file that discards all data written to it, but reports that the write operation succeeded, and provides no data to any process that reads from it, yielding EOF immediately). & is a special type of command separator used to background the preceding process.