YouTube Icon

Interview Questions.

Top 100+ Linux Shell Scripting Interview Questions And Answers - May 31, 2020

fluid

Top 100+ Linux Shell Scripting Interview Questions And Answers

Question 1. What Is Shell Script And Why It Is Required?

Answer :

A Shell Script is a textual content report that includes one or greater commands. As a system administrator we often want to problem quantity of commands to perform the venture, we will upload those all commands collectively in a textual content report (Shell Script) to finish day by day recurring assignment.

Question 2. What Is The Default Login Shell And How To Change Default Login Shell For A Specific User?

Answer :

In Linux like Operating system “/bin/bash” is the default login shell which is assigned while person creation. We can alternate default shell using the “chsh” command.

Example is proven below:

# chsh <username> -s <new_default_shell>

# chsh linuxtechi -s /bin/sh

Perl Scripting Interview Questions
Question three. What Are The Different Type Of Variables Used In A Shell Script?

Answer :

In a shell script we can use  styles of variables:

System defined variables
User described variables
System defined variables are described or created through Operating System (Linux) itself. These variables are normally defined in Capital Letters and may be viewed through “set” command.

User defined variables are created or defined by device customers and the values of variables may be regarded by way of the use of the command “echo $<Name_of_Variable>”.

Question four. How To Redirect Both Standard Output And Standard Error To The Same Location?

Answer :

There  strategies to redirect std output and std errors to the equal vicinity:

Method:1 2>&1 (# ls /usr/share/doc > out.Txt 2>&1 )

Method:2 &> (# ls /usr/proportion/doc &> out.Txt )

Perl Scripting Tutorial
Question five. What Is The Syntax Of “nested If Statement” In Shell Scripting?

Answer :

Basic Syntax is shown underneath:

if [ Condition ]

then

command1

command2

…..

Else

if [ condition ]

then

command1

command2

….

Else

command1

command2

…..

Fi

fi

Shell Scripting Interview Questions
Question 6. What Is The Use Of “$?” Sign In Shell Script?

Answer :

While writing a shell script, in case you want to check whether preceding command is achieved effectively or not, then we will use “$?” with if statement to check the go out fame of previous command.

Basic instance is proven beneath:

root@localhost:~# ls /usr/bin/shar

/usr/bin/shar

root@localhost:~# echo $

zero

If go out status is zero , then command is finished efficaciously

root@localhost:~# ls /usr/bin/share

ls: can't get right of entry to /usr/bin/proportion: No such record or listing

root@localhost:~# echo $

2

If the exit repute is other than zero, then we will say command is not performed correctly.

Question 7. How To Compare Numbers In Linux Shell Scripting?

Answer :

Test command is used to examine numbers in if-then statement.

Example is shown under:

#! /bin/bash

x=10

y=20

if [ $x -gt $y ]

then

echo “x is more than y”

else

echo “y is greater than x”

fi

Shell Scripting Tutorial Linux Interview Questions
Question eight. What Is The Use Of Break Command?

Answer :

The break command is a simple manner to escape out of a loop in development. We can use the wreck command to exit out from any loop, such as whilst and until loops.

Question 9. What Is The Use Of Continue Command In Shell Scripting?

Answer :

The keep command is identical to break command except it causes the present iteration of the loop to exit, as opposed to the complete loop. Continue command is useful in some scenarios wherein error has occurred but we nevertheless need to execute the next commands of the loop.

Linux Embedded systems Interview Questions
Question 10. Tell Me The Syntax Of “case Statement” In Linux Shell Scripting?

Answer :

The basic syntax is shown under:

case word in

value1)

command1

command2

…..

Last_command

!!

Value2)

command1

command2

……

last_command

;;

esac

Linux Tutorial
Question 11. What Is The Basic Syntax Of While Loop In Shell Scripting?

Answer :

Like the for loop, the even as loop repeats its block of commands some of instances. Unlike the for loop, but, the at the same time as loop iterates till its even as situation is now not actual.

The simple syntax is :

even as [ test_condition ]

do

commands…

done

Linux File Systems Interview Questions
Question 12. How To Make A Shell Script Executable?

Answer :

Using the chmod command we can make a shell script executable.

Example is shown below :

# chmod a+x myscript.Sh

Perl Scripting Interview Questions
Question 13. What Is The Use Of “#!/bin/bash” ?

Answer :

#!/bin/bash is the first of a shell script , known as shebang , where # symbol is called hash and ‘!’ is known as as bang. It shows that command to be performed through /bin/bash.

Linux Embedded structures Tutorial
Question 14. What Is The Syntax Of For Loop In Shell Script?

Answer :

Basic Syntax of for loop is given under:

for variables in list_of_items

do

command1

command2

….

Last_command

achieved

Question 15. How To Debug A Shell Script?

Answer :

A shell script can be debug if we execute the script with ‘-x’ option ( sh -x myscript.Sh). Another manner to debug a shell script is with the aid of the usage of ‘-nv’ choice (sh -nv myscript.Sh).

Advanced Linux Interview Questions
Question sixteen. How Compare The Strings In Shell Script?

Answer :

Test command is used to compare the text strings. The take a look at command compares textual content strings via comparing each character in each string.

Unix/Linux Tutorial
Question 17. What Are The Special Variables Set By Bourne Shell For Command Line Arguments?

Answer :

The following desk lists the unique variables set via the Bourne shell for command line arguments.

Special Variables: Holds

$0: Name of the Script from the command line
$1: First Command-line argument
$2: Second Command-line argument
…..
…….
$9: Ninth Command line argument
$#: Number of Command line arguments
$*: All Command-line arguments, separated with areas
Unix/Linux Interview Questions
Question 18. How To Test Files In A Shell Script?

Answer :

Test command is used to carry out distinctive check on the documents. Basic test are indexed under:

Test: Usage

-d file_name: Returns genuine if the file exists and is a listing.
-e file_name: Returns authentic if the record exists.
-f file_name: Returns genuine if the report exists and is a ordinary report.
-r file_name: Returns true if the document exists and feature read permissions.
-s file_name: Returns true if the file exists and isn't empty.
-w file_name: Returns authentic if the file exists and feature write permissions.
-x file_name: Returns true if the record exists and have execute permissions.
Shell Scripting Interview Questions
Question 19. How To Put Comments In Your Shell Script?

Answer :

Comments are the messages to yourself and for other users that describe what a script is supposed to do and the way its works. To put feedback to your script, start every remark line with a hash signal (#).

Example is proven beneath:

#!/bin/bash

# This is a command

echo “I am logged in as $USER”

Question 20. How To Get Input From The Terminal For Shell Script?

Answer :

‘read’ command reads in facts from the terminal (the use of keyboard). The study command takes in regardless of the person types and places the textual content into the variable you name.

Example is proven below:

# vi /tmp/check.Sh

#!/bin/bash

echo ‘Please enter your call’

read name

echo “My Name is $call”

# ./test.Sh

Please enter your name

LinuxTechi

My Name is LinuxTechi

Linux Device Drivers Interview Questions
Question 21. How To Unset Or De-assign Variables?

Answer :

‘unset’ command is used to de-assign or unset a variable.

Syntax is shown below:

# unset <Name_of_Variable>

Question 22. How To Perform Arithmetic Operation?

Answer :

There are two approaches to carry out arithmetic operations:

Using expr command (# expr 5 + 2).
Using a dollar signal and rectangular brackets ( $[ operation ] ) Example : check=$[16 + 4] ; check=$[16 + 4].
Question 23. Basic Syntax Of Do-whilst Statement?

Answer :

The do-whilst announcement is much like the at the same time as announcement but plays the statements before checking the circumstance statement.

The following is the layout for the do-whilst announcement:

do

statements

 while (circumstance)

Unix Shell Scripting Interview Questions
Question 24. How To Define Functions In Shell Scripting?

Answer :

A feature is really a block of of code with a name. When we provide a name to a block of code, we will then call that name in our script, and that block could be accomplished.

Example is shown underneath:$ disk usage ()  df -h ; 

Linux Interview Questions
Question 25. How To Use Bc (bash Calculator) In A Shell Script?

Answer :

Use the beneath Syntax to use bc in shell script.

Variable=`echo “alternatives bc`.




CFG