Top 21 Linux Shell Scripting Interview Questions
Q1. What Is The Default Login Shell And How To Change Default Login Shell For A Specific User?
In Linux like Operating device “/bin/bash” is the default login shell that's assigned whilst user advent. We can trade default shell the use of the “chsh” command.
Example is proven below:
# chsh <username> -s <new_default_shell>
# chsh linuxtechi -s /bin/sh
Q2. How To Put Comments In Your Shell Script?
Comments are the messages to your self and for different customers that describe what a script is supposed to do and how its works. To placed comments to your script, start every comment line with a hash signal (#).
Example is proven under:
#!/bin/bash
# This is a command
echo “I am logged in as $USER”
Q3. How To Compare Numbers In Linux Shell Scripting?
Test command is used to compare numbers in if-then statement.
Example is proven underneath:
#! /bin/bash
x=10
y=20
if [ $x -gt $y ]
then
echo “x is greater than y”
else
echo “y is more than x”
fi
Q4. What Is The Use Of Break Command?
The spoil command is a simple way to escape out of a loop in development. We can use the destroy command to exit out from any loop, which include at the same time as and until loops.
Q5. How To Test Files In A Shell Script?
Test command is used to perform exclusive test at the documents. Basic test are indexed under:
Test: Usage
-d file_name: Returns real if the record exists and is a listing.
-e file_name: Returns authentic if the file exists.
-f file_name: Returns proper if the report exists and is a normal file.
-r file_name: Returns real if the record exists and feature study permissions.
-s file_name: Returns actual if the report exists and is not empty.
-w file_name: Returns authentic if the report exists and feature write permissions.
-x file_name: Returns proper if the record exists and feature execute permissions.
Q6. What Is The Syntax Of For Loop In Shell Script?
Basic Syntax of for loop is given below:
for variables in list_of_items
do
command1
command2
….
Last_command
completed
Q7. Basic Syntax Of Do-even as Statement?
The do-even as statement is similar to the at the same time as announcement however performs the statements earlier than checking the circumstance declaration.
The following is the format for the do-even as statement:
do
statements
even as (situation)
Q8. How To Make A Shell Script Executable?
Using the chmod command we will make a shell script executable.
Example is proven under :
# chmod a+x myscript.Sh
Q9. How To Debug A Shell Script?
A shell script may be debug if we execute the script with ‘-x’ option ( sh -x myscript.Sh). Another manner to debug a shell script is via the usage of ‘-nv’ option (sh -nv myscript.Sh).
Q10. How To Use Bc (bash Calculator) In A Shell Script?
Use the beneath Syntax to apply bc in shell script.
Variable=`echo “options; expression” desk lists the unique variables set by the Bourne shell for command line arguments.
Special Variables: Holds
$zero: Name of the Script from the command line
$1: First Command-line argument
$2: Second Command-line argument
…..
…….
$nine: Ninth Command line argument
$#: Number of Command line arguments
$*: All Command-line arguments, separated with spaces
Q12. How Compare The Strings In Shell Script?
Test command is used to evaluate the textual content strings. The check command compares textual content strings by using evaluating each person in each string.
Q13. What Is Shell Script And Why It Is Required?
A Shell Script is a text report that carries one or greater instructions. As a machine administrator we regularly want to difficulty quantity of commands to accomplish the assignment, we will upload these all instructions collectively in a textual content record (Shell Script) to finish every day recurring undertaking.
Q14. How To Perform Arithmetic Operation?
There are two ways to perform mathematics operations:
Using expr command (# expr 5 + 2).
Using a dollar sign and square brackets ( $[ operation ] ) Example : test=$[16 + 4] ; check=$[16 + 4].
Q15. How To Redirect Both Standard Output And Standard Error To The Same Location?
There strategies to redirect std output and std blunders to the equal vicinity:
Method:1 2>&1 (# ls /usr/proportion/doc > out.Txt 2>&1 )
Method:2 &> (# ls /usr/proportion/document &> out.Txt )
Q16. How To Get Input From The Terminal For Shell Script?
‘study’ command reads in information from the terminal (the usage of keyboard). The examine command takes in whatever the person sorts and locations the textual content into the variable you name.
Example is shown beneath:
# vi /tmp/check.Sh
#!/bin/bash
echo ‘Please input your call’
study name
echo “My Name is $name”
# ./check.Sh
Please enter your name
LinuxTechi
My Name is LinuxTechi
Q17. What Is The Basic Syntax Of While Loop In Shell Scripting?
Like the for loop, the whilst loop repeats its block of commands some of times. Unlike the for loop, however, the even as loop iterates till its while condition is not real.
The simple syntax is :
even as [ test_condition ]
do
commands…
finished
Q18. How To Define Functions In Shell Scripting?
A characteristic is surely a block of of code with a call. When we supply a name to a block of code, we can then name that name in our script, and that block could be executed.
Example is proven under:$ disk utilization () df -h ;
Q19. What Are The Different Type Of Variables Used In A Shell Script?
In a shell script we will use forms of variables:
System defined variables
User described variables
System described variables are defined or created by Operating System (Linux) itself. These variables are normally defined in Capital Letters and can be regarded by “set” command.
User described variables are created or described by means of system customers and the values of variables may be regarded by means of using the command “echo $<Name_of_Variable>”.
Q20. How To Unset Or De-assign Variables?
‘unset’ command is used to de-assign or unset a variable.
Syntax is proven under:
# unset <Name_of_Variable>
Q21. What Is The Use Of Continue Command In Shell Scripting?
The hold command is equal to interrupt command except it causes the prevailing new release of the loop to go out, as opposed to the entire loop. Continue command is beneficial in a few eventualities in which error has happened however we still need to execute the subsequent instructions of the loop.

