Shell Scripting Interview Questions

Shell Scripting Interview Questions

Following are some of the most frequently asked Shell Scripting interview questions in the interview, here are the answers for them.

What is a shell?

Shell is a interface between user and the kernel. Even though there can be only one kernel ; a system can have many shell running simultaneously . Whenever a user enters a command through 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 on 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”

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.

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

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

How will you copy file from one machine to other?

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

Eg: Using ftp:

ftp hostname

>put file1

>bye

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

Write down syntax for all loops in shell scripting ?

for loop :

for i in $( ls ); do
echo item: $i
done

while loop :

#!/bin/bash
COUNTER=0
while [ $COUNTER -lt 10 ]; do
echo The counter is $COUNTER
let COUNTER=COUNTER+1
done

until loop :

#!/bin/bash
COUNTER=20
until [ $COUNTER -lt 10 ]; do
echo COUNTER $COUNTER
let COUNTER-=1
done

What it means by #!/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 command “export” do ?

Makes variable public in subshells

How to run script in background ?

add “&” to the end of script

What “chmod 500 script” do ?

Makes script executable for script owner

What “>” do ?

Redirects output stream to file or another stream.

What difference between & and &&

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

When we need “if” before [ condition ] ?

When we need to run several commands if condition meets.

What would be the output of the command: name=John && echo ‘My name is $name’

My name is $name

Which is the symbol used for comments in bash shell scripting ?

#

What would be the output of command: echo ${new:-variable}

variable

What is a boot block?

This block contains a small program called “Master Boot record”(MBR) which loads the kernel during system boot up.

What is a super block?

Super block contains all the information about the file system like size of file system, block size used by it,number of free data blocks and list of free inodes and data blocks.

What is an inode block?

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

How can I send a mail with a compressed file as an attachment?

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

Email content

EOF

How do we create command aliases in shell?

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

What difference between ‘ and ” quotes ?

we use it when do not want to evaluate variables to the values
” – all variables will be evaluated and its values will be assigned instead.

How to print all arguments provided to the script ?

echo $*
or
echo $@

What difference between [ $a == $b ] and [ $a -eq $b ]

[ $a == $b ] – should be used for string comparison
[ $a -eq $b ] – should be used for number tests

 What difference between = and ==

= – we using to assign value to variable
== – we using for string comparison

Write the command to test if $a greater than 12 ?

[ $a -gt 12 ]

Write the command to test if $b les or equal 12 ?

[ $b -le 12 ]

How to redirect stdout and stderr streams to log.txt file from script inside ?

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

How to get part of string variable with echo command only ?

echo ${variable:x:y}
x – start position
y – length
example:
variable=”My name is Petras, and I am developer.”
echo ${variable:11:6} # will display Petras

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 get “User” from the string above ?

echo ${variable%:*:*:*}
or
echo ${variable%%:*}

How to list users which UID less that 100 (awk) ?

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

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

    3 + 5