Linux Administration Certification Training C ...
- 15k Enrolled Learners
- Weekend/Weekday
- Live Class
Linux has started to expand its market rapidly since the past few years and Shell Scripting in Linux is one of the Top 10 occurring IT job-requirements. So, we thought of making your job easier by making an ensemble of the most commonly asked Shell Scripting Interview Questions which will get you ready for any job interview that you wish to appear.
The questions have been segregated into 3 parts;
The Shell is a Command Line Interpreter. It translates commands entered by the user and converts them into a language that is understood by the Kernel. The shell interprets a command typed in at the terminal, and calls the program that you want.
A shell script is a command-containing text-file that contains commands in order of their execution. Typical operations performed by shell scripts include printing text, file manipulation, and program execution.
Following are the two main advantages of shell scripting:
A shell script has two types of variables :
There are primarily two kinds of shells in Linux OS, namely, Bourne Shell and C-Shell. Examples of derivative from each are as follows;
This can be done with the help of links present in Linux OS.
Hard Link: Hard links are linked to the inode of the file and have to be on the same file system as of the file. Deleting the original file does not affect the hard link.
Soft Link: Soft links are linked to the file name and can reside on a different file system as well. Deleting the original file makes the soft link inactive.
A Super Block is essentially a program that contains a record of specific file systems.
Characteristics such as the block size, the empty and the filled blocks and their respective counts, the size and location of the inode tables, the disk block map, and usage information, and the size of the block groups are available in a superblock.
GUI is used for controlling a computer and its applications. GUI scripting supports different applications. It mostly depends on the operating system.
A Linux process generally passes through four stages:
The Shebang line is present at the top of the script,e.g. #!/bin/sh. It simply provides information regarding the location where the engine is placed. The engine is the one that executes the script.
This video covers the types of Linux Shell Scripting Interview Questions for candidates of beginners through an advanced level of expertise in Shell Scripting. Each segment comes with ten theory questions and 10 demos, each run on the Linux terminal for a better understanding of the viewers.
#!/bin/sh
ct $1
question 11 – Shell Scripting Interview Questions – Edureka
#!/bin/sh
cp $1 $2
question 12 – Shell Scripting Interview Questions – Edureka
#!/bin/sh
echo "Number of Parameters passed:$#"
question 13 – Shell Scripting Interview Questions – Edureka
!/bin/sh
echo "Script Name:$0"
question 14 – Shell Scripting Interview Questions – Edureka
#!/bin/sh
var=$?
if var=0
then
echo "Script was Run successfully"
else
echo "Script was unsuccessful"
fi
question 15 – Shell Scripting Interview Questions – Edureka
tail -1 <filename>
head -1 <filename>
#!/bin/sh
awk '{print $3}' $1
question 18 – Shell Scripting Interview Questions – Edureka
#!/bin/sh
function example {
echo "Hello Learner"
}
For Loop:
for var in word1 word2 ... wordN
do
Statement(s) to be executed for every word.
done
while command
do
Statement(s) to be executed if command is true
done
Until Loop:
until command
do
Statement(s) to be executed until command is true
done
C is a more preferable option in the following cases:
The test command is used to compare the text strings. The test command compares text strings by comparing each character in each string.
The two methods to redirect standard output and standard error to the same location are the following;
It is not advisable to use Shell scripting in the following cases;
The lifespan of a variable inside shell script is only until the end of execution.
The file system is a collection of files which contain information related to the files.
On Linux and other Unix-like operating systems, new files are created with a default set of permissions. The umask or user mask command is used to determine the default permissions for newly created files. It is a 4-digit Octal number which is set and expressed using symbolic values. The default permission of a file when it is created is 664 i.e. rw-rw-r-. The table for file permissions is given below;
0 | 0 | No permissions |
1 | 1 | execute |
2 | 2 | write |
3 | 1+2 | execute + write |
4 | 4 | read |
5 | 1+4 | execute + read |
6 | 2+4 | write + read |
7 | 1+2+4 | execute + write + read |
A script may specify #!/bin/bash on the first line, meaning that the script should always be run with bash, rather than another shell. /bin/sh is an executable representing the system shell. Actually, it is usually implemented as a symbolic link pointing to the executable for whichever shell is the system shell.
$@ treats each quoted arguments as separate arguments but $* will consider the entire set of positional parameters as a single string.
question 31 – Shell Scripting Interview Questions – Edureka
question 32 – Shell Scripting Interview Questions – Edureka
q33 – Shell Scripting Interview Questions – Edureka
#!/bin/sh
echo ${variable:x:y}
#x - start position
#y - length
variable="My name is Upasana, and I work at Edureka."
echo ${variable:11:7} # will display Upasana
q34 – Shell Scripting Interview Questions – Edureka
q35 – Shell Scripting Interview Questions – Edureka
#!/bin/bash
for i; do
echo $i
done
q36 – Shell Scripting Interview Questions – Edureka
Q37. How to print PID of the current shell?
#!/bin/sh
for PID in $$
do
echo $PID
done
q37 – Shell Scripting Interview Questions – Edureka
!/bin/sh
array=("This" "is" "Shell" "Scripting")
echo ${array[@]}
echo ${!array[@]}
q38 – Shell Scripting Interview Questions – Edureka
#!/bin/sh
array=("This" "is" "Shell" "Scripting" )
echo ${array[0]}
q39 – Shell Scripting Interview Questions – Edureka
Crontab stands for cron table because it uses the job scheduler cron to execute tasks. The crontab is a list of commands that you want to run on a regular schedule, and also the name of the command used to manage that list.
The schedule is called the crontab, which is also the name of the program used to edit that schedule.
The crontab file has six fields.
The first five fields contain information on when to execute the command and they are as follows;
The sixth field contains the command to be executed.
The two files of crontab command are:
The tar command is used to take the backup. It stands for tape archive. The command is mainly used to save and restore files to and from an archive medium like tape.
There are three different commands available to check the disk usage.
There are four different communication commands available in Shell.
The total disk space used by Edureka can be found out as shown below.
du –s/home/Edureka
Given below are some common methods used to debug the problems in the script.
A read-only file can be opened using the below command:
vi –R <File Name>
The contents of the file inside a jar can be read without extracting as shown below.
tar –tvf <File Name>.tar
#!/bin/sh
echo "Hello, $LOGNAME"
echo "Today's date is `date`"
echo "Username is `who i am`"
echo "Current directory is `pwd`"
q51 – Shell Scripting Interview Questions – Edureka
find . -type f -mtime -3 -exec ls -l {} ; > last3days.txt
#!/bin/sh
# The Shebang
if [ $# -ne 2 ]
# If two Inputs are not received from Standard Input
then
# then execute the below statements
echo "Usage - $0 x y"
# print on standard output, how-to use the script (Usage - ./1.sh x y )
echo " Where x and y are two nos for which I will print sum"
# print on standard output, “Where x and y are two nos for which I will pri$
exit 1
# Leave shell in Error Stage and before the task was successfully carried o$
fi
# print on standard output, how-to use the script (Usage - ./1.sh x y )
echo " Where x and y are two nos for which I will print sum"
# print on standard output, “Where x and y are two nos for which I will pri$
exit 1
# Leave shell in Error Stage and before the task was successfully carried o$
fi
# End of the if Statement.
echo "Sum of $1 and $2 is `expr $1 + $2`"
# If the above condition was false and user Entered two numbers as a command$
Case 1: When parameters are not passed
q52.1 – Shell Scripting Interview Questions – Edureka
Case 2: When parameters are correctly passed
q52.2 – Shell Scripting Interview Questions – Edureka
#!/bin/sh
if [ $# -ne 1 ]
then
echo "Usage: $0 number"
echo " Reverse of the given number will be printed"
echo " For eg. $0 0123, 3210 will be printed"
exit 1
fi
n=$1
rev=0
sd=0
while [ $n -gt 0 ]
do
sd=`expr $n % 10`
rev=`expr $rev * 10 + $sd`
n=`expr $n / 10`
done
q54.1 – Shell Scripting Interview Questions – Edureka
Case 2: When the parameter is correctly passed
q54.2 – Shell Scripting Interview Questions – Edureka
q55 – Shell Scripting Interview Questions – Edureka
q56 – Shell Scripting Interview Questions – Edureka
du -sh ~
q57 – Shell Scripting Interview Questions – Edureka
#!/bin/sh
if [ -d $mydir ]
then
echo "Directory exists"
fi
q58 – Shell Scripting Interview Questions – Edureka
#!/bin/sh
#set -x
i=1
while [ $i -lt 6 ]
do
print "in loop iteration: $i"
((i+=1))
done
exit
q59.1 – Shell Scripting Interview Questions – Edureka
#!/bin/sh
set -x
i=1
while [ $i -lt 6 ]
do
print "in loop iteration: $i"
((i+=1))
done
exit
q59.2 – Shell Scripting Interview Questions – Edureka
All the forked processes which are new get overlays when the exec is executed. The command simply gets executed without making any impact on the current process. Also, no new process will be created in this scenario.
These were all the questions we could think of, on the various aspects of Shell Scripting in Linux, that would cover you for any interview you’d appear for, in the shortest possible time frame. If you have any interesting questions that have been asked to you in an interview you’ve appeared before, feel free to drop them at the comments bar and we shall answer them for you. You could also refer to this video which explains the solutions to these problems in depth.
If you wish to learn Linux Administration and build a colorful career, then check out our Linux Administration Training which comes with instructor-led live training and real-life project experience. This training will help you understand Linux Administration in depth and help you achieve mastery over the subject.
edureka.co