Shell Scripting Interview Questions & Answers

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.

csh,ksh,bash,Bourne . The most commonly used and advanced shell used today is “Bash” .

Shortcuts are created using “links” on Linux. There are two types of links that can be used namely “soft link” and “hard link”.

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)

Arguments can be passed as:

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

$# shows the count of the arguments passed to the script.

$@ treats each quoted arguments as separate arguments but $* will consider the entire set of positional parameters as a single string.

if cat fille
ABCD
EFGH

Then O/p should be

EFGH
ABCD

sed '1! G; h;$!d' file1

Here G command appends to the pattern space,

h command copies pattern buffer to hold buffer

and d command deletes the current pattern space.

tr –s  "(backslash)040" <file1|tr –s  "(backslash)011"|tr "(backslash)040 (backslash)011" "(backslash)012" |uniq –c
where "(backslash)040" is octal equivalent of "space"

“(backslash)011” is an octal equivalent of “tab character” and

“(backslash)012” is an octal equivalent of the newline character.

In bash shell, we can create “.profile” file which automatically gets invoked as soon as I log in and write the following syntax into it.

export PS1='$ `pwd`:`hostname`>' .File1

Here File1 is the file containing the user-defined functions and “.” invokes this file in current shell.

“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.

We can create the directory giving read and execute access to everyone in the group and setting its sticky bit “t” on as follows:

mkdir direc1
 
chmod g+wx direc1
 
chmod +t direc1

We can find this by using the command “uptime”.

$$ gives the process id of the currently executing process whereas $! Shows the process id of the process that recently went into the background.

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.

We can use utilities like “ftp,” “scp” or “rsync” to copy a file from one machine to other.

E.g., Using ftp:

FTP hostname

>put file1

>bye

Above copies, file file1 from the local system to destination system whose hostname is specified.

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.

We can use ssh to do this:

ssh username@serverIP -p sshport

Example

ssh root@122.52.251.171 -p 22

Once above command is executed, you will be asked to enter the password

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.

  
#!/bin/sh
a=1
b=1
echo $a
echo $b
for I in 1 2 3 4 5 6 7 8
do
c=a
b=$a
b=$(($a+$c))
echo $b
done

We can use isql utility that comes with open client driver as follows:

isql –S serverName –U username –P password

0 – Standard Input1 – Standard Output2 – Standard Error

“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.

E.g., Executing “exec ls” on command prompt will execute ls and once ls exits, the process will shut down

awk ‘END {print NR} fileName’

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)

/etc/shadow file has all the users listed.

awk –F ‘:’ ‘{print $1} /etc/shadow’|uniq -u

Syntax in ksh:

Set –A arrayname= (element1 element2 ….. element)
In bash
A=(element1 element2 element3 …. elementn)

Syntax:

for  iterator in (elements)
do
execute commands
done

du -s /home/user1 ….where user1 is the user for whom the total disk space needs to be found.

Syntax

If  condition is successful
then
execute commands
else
execute commands
fi

The command $? gives the exit status of the last command that was executed.

sed  '^ [(backslash)011(backslash)040]*$/d' file1

where (backslash)011 is an octal equivalent of space and

(backslash)040 is an octal equivalent of the tab

We can use:

umask 777

This will set default rwx permission for every file which is created for every user.

We can use “ps –p ProcessId”

Bootblock, super block, inode block and Datablock are found fundamental components of every file system on Linux.

This 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.

This block contains the inode for every file of the file system along with all the file attributes except its name.

zip file1.zip file1|mailx –s “subject” Recipients email id

Email content

EOF

alias Aliasname=”Command whose alias is to be created”.

“c ” and “b” permission fields are generally associated with a device file. It specifies whether a file is a special character file or a block special file.

Shebang line at the top of each script determines the location of the engine which is to be used to execute the script.