Sunteți pe pagina 1din 17

7/1/2017 10 Useful 'Interview Questions and Answers' on Linux Shell Scripting


Commands
Linux
A-Z
INTERVIEW QUESTIONS  20 I  TecMint :     

 BEGINNER'S GUIDE FOR LINUX 


10 Useful ‘Interview Start learning Linux in minutes 

Questions and
https://www.tecmint.com/interview-questions-on-shell-scripting/ 1/17
7/1/2017 10 Useful 'Interview Questions and Answers' on Linux Shell Scripting

Answers’ on Linux
Linux Foundation’s LFCS and LFCE Certi cation Preparation Guide
 Vi/Vim ($39)BEGINNER'S
Editor Get This Book
GUIDE   

Learn vi/vim as a Full Text Editor 


Shell Scripting
by Editor | Published: May 26, 2014 | Last Updated:
January 7, 2015  Linux Foundation Certi cation 
Exam Study Guide to LFCS and LFCE
Ads by Google Create a Logo How to Join

 Download Your Free eBooks NOW - 10 Free


Linux eBooks for Administrators | 4 Free Shell  
Scripting eBooks

How to Add Linux Host to Nagios Monitoring


Greeting of the day. The
Server Using NRPE Plugin
vastness of Linux makes it
possible to come up with a Nagios 4.2.0 Released – Install on RHEL/CentOS
unique post every time. We 7.x/6.x/5.x and Fedora 24-19

‘The-Tecmint-Team‘ works
Install Cacti (Network Monitoring) on
to provide our readers with RHEL/CentOS 7.x/6.x/5.x and Fedora 24-12
unique contents which is
useful for them from career Google Chrome 59 Released – Install on
RHEL/CentOS 7/6 and Fedora 25-20
perspective as well as
adding to the Knowledge How to Install Ubuntu 16.10/16.04 Alongside With
base. Here is an attempt Windows 10 or 8 in Dual-Boot
and it is on our readers to
Tecmint’s Guide to Red Hat RHCSA / RHCE
judge how far we succeed.
Certi cation Preparation Study Guide

SHARE


0
Questions on Shell Scripting

We have lots of tutorials on



Shell ScriptingPreparation 0
language for the LFCS (Linux Foundation Certi ed SysAdmin) Exam
https://www.tecmint.com/interview-questions-on-shell-scripting/ 2/17
7/1/2017 10 Useful 'Interview Questions and Answers' on Linux Shell Scripting

and Interview Questions for 


readers of all kind, here are
9
the links to those articles.

Shell Scripting Series
Interview Question and
Answer Series

Adding to the shell scripting


posts here, in this article we
will be going through
questions related to Linux
Shell from interview point of
view.

1. How will you abort a


shell script before it is
successfully executed?

Linux System Administrator Bundle


Answer : We need to use ‘exit’
with 7-Courses (96% off)
command to ful l the above

described situation. A ‘exit’


Add to Cart - $69
command when forced to
 Ending In: 3 days
output any value other than 0

(zero), the script will throw an


Computer Hacker Professional
error and will abort. The value 0
Certi cation Course (96% Off)
(zero) under Unix environment

shell scripting represents


Add to Cart - $59
successful execution. Hence
 Ending In: 4 days
putting ‘exit -1’, without quotes
before script termination will

abort the script. LINUX EBOOKS

Introducing Learn Linux In One Week and Go


For example, create a from Zero to Hero
following shell script as RedHat RHCE/RHCSA Certi cation Preparation
Guide
https://www.tecmint.com/interview-questions-on-shell-scripting/ 3/17
7/1/2017 10 Useful 'Interview Questions and Answers' on Linux Shell Scripting

‘anything.sh‘. Linux Foundations LFCS/LFCE Certi cation


Guide
Post x Mail Server Setup Guide for Linux
#!/bin/bash
echo "Hello" Ansible Setup Guide for Linux
exit -1
Django Setup Guide for Linux
echo "bye"
Awk Getting Started Guide for Beginners
Citrix XenServer Setup Guide for Linux

    

Never Miss Any Linux Tutorials,


Save the le and execute it. Guides, Tips and Free eBooks
Join Our Community Of 150,000+ Linux Lovers
# sh anything.sh and get a weekly newsletter in your inbox
Hello
exit.sh: 3: exit: Illegal number: -1 YES! SIGN ME UP

From the above script, it is


clear that the execution
went well before exit -1
command.

2. How to remove the


headers from a le using
command in Linux?

Answer : A ‘sed’ command

comes to rescue here, when we


need to delete certain lines of a
le.

Here it the exact command


to remove headers from a
le (or rst line of a le).

https://www.tecmint.com/interview-questions-on-shell-scripting/ 4/17
7/1/2017 10 Useful 'Interview Questions and Answers' on Linux Shell Scripting

# sed '1 d' file.txt

The only problem with


above command is that, it
outputs the le on standard
output without the rst line.
In order to save the output
to le, we need to use
redirect operator which will
redirects the output to a le.

# sed '1 d' file.txt > new_file.txt

Well the built in switch ‘-i‘ for


sed command, can perform
this operation without a
redirect operator.

# sed -i '1 d' file.txt

3. How will you check the


length of a line from a text
le?

Answer : Again ‘sed’ command


is used to nd or check the
length of a line from a text le.

A ‘sed –n ‘n p’ le.txt‘,
where ‘n‘ represents the line
number and ‘p‘ print out the

https://www.tecmint.com/interview-questions-on-shell-scripting/ 5/17
7/1/2017 10 Useful 'Interview Questions and Answers' on Linux Shell Scripting

pattern space (to the


standard output). This
command is usually only
used in conjunction with the
-n command-line option. So,
how to get the length count?
Obviously! we need to
pipeline the output with ‘wc‘
command.

# sed –n 'n p' file.txt | wc –c

To get the length of line


number ‘5’ in the text le
‘tecmint.txt‘, we need to run.

# sed -n '5 p' tecmint.txt | wc -c

4. Is it possible to view all


the non-printable
characters from a text le
on Linux System? How will
you achieve this?

Answer : Yes! it is very much

possible to view all the non-


printable characters in Linux. In

order to achieve the above said


scenario, we need to take the

help of editor ‘vi’.

https://www.tecmint.com/interview-questions-on-shell-scripting/ 6/17
7/1/2017 10 Useful 'Interview Questions and Answers' on Linux Shell Scripting

How to show non-printable


characters in ‘vi‘ editor?

Open vi editor.
Go to command mode
of vi editor by pressing
[esc] followed by ‘:’.
The nal step is to type
execute [set list]
command, from
command interface of
‘vi’ editor.

Note: This way we can see


all the non-printable
characters from a text le
including ctrl+m (^M).

5. You are a Team-Leader


of a group of staffs
working for a company xyz.
The company ask you to
create a directory ‘dir_xyz’,
such that any member of
the group can create a le
or access a le under it,
but no one can delete the
le, except the one created
it. what will you do?

Answer : An interesting scenario


to work upon. Well in the above

said scenario we need to


implement the below steps

which is as easy as cake walk.

https://www.tecmint.com/interview-questions-on-shell-scripting/ 7/17
7/1/2017 10 Useful 'Interview Questions and Answers' on Linux Shell Scripting

# mkdir dir_xyz
# chmod g+wx dir_xyz
# chmod +t dir_xyz

The rst line of command


create a directory (dir_xyz).
The second line of
command above allow
group (g) to have
permission to ‘write‘ and
‘execute‘ and the last line of
the above command – The
‘+t‘ in the end of the
permissions is called the
‘sticky bit‘. It replaces the ‘x‘
and indicates that in this
directory, les can only be
deleted by their owners, the
owner of the directory or the
root superuser.

6. Can you tell me the


various stages of a Linux
process, it passes through?

Answer : A Linux process

normally goes through four


major stages in its processing

life.

Here are the 4 stages of


Linux process.

Waiting: Linux Process


waiting for a resource.

https://www.tecmint.com/interview-questions-on-shell-scripting/ 8/17
7/1/2017 10 Useful 'Interview Questions and Answers' on Linux Shell Scripting

Running : A Linux
process is currently
being executed.
Stopped : A Linux
Process is stopped
after successful
execution or after
receiving kill signal.
Zombie : A Process is
said to be ‘Zombie’ if it
has stopped but still
active in process table.

7. What is the use of cut


command in Linux?

Answer : A ‘cut’ is a very useful

Linux command which proves to


be helpful when we need to cut

certain speci c part of a le and


print it on standard output, for

better manipulation when the


eld of the le and le itself is

too heavy.

For example, extract rst 10


columns of a text le
‘txt_tecmint‘.

# cut -c1-10 txt_tecmint

To extract 2nd, 5th and 7th


column of the same text le.

https://www.tecmint.com/interview-questions-on-shell-scripting/ 9/17
7/1/2017 10 Useful 'Interview Questions and Answers' on Linux Shell Scripting

# cut -d;-f2 -f5 -f7 txt_tecmint

8. What is the difference


between commands ‘cmp’
and ‘diff’?

Answer : The command ‘cmp’

and ‘diff’ means to obtain the


same thing but with different

mindset.

The ‘diff‘ command reports


the changes one should
make so that both the les
look the same. Whereas
‘cmp‘ command compares
the two les byte-by-byte
and reports the rst
mismatch.

9. Is it possible to
substitute ‘ls’ command
with ‘echo’ command?

Answer : Yes! the ‘ls’ command

can be substituted by ‘echo’


command. The command ‘ls’

lists the content of le. From the


point of view of replacement of

above command we can use

‘echo *’, obviously without


quotes. The output of both the

commands are same.

https://www.tecmint.com/interview-questions-on-shell-scripting/ 10/17
7/1/2017 10 Useful 'Interview Questions and Answers' on Linux Shell Scripting

10. You might have heard


about inodes. can you
describe inode brie y?

Answer : A ‘inode’ is a ‘data-

structure’, which is used for le

identi cation on Linux. Each le


on an Unix System has a

separate ‘inode’ and an ‘Unique’


inode Number.

That’s all for now. We will be


coming up with another
interesting and
knowledgeable Interview
questions, in the next
article. Till then Stay tuned
and connected to
Tecmint.com. Don’t forget
to provide us, with your
valuable feedback in the
comment section below.

If You Appreciate
What We Do Here On
TecMint, You Should
Consider:
1. Stay Connected to: Twitter |
Facebook | Google Plus

https://www.tecmint.com/interview-questions-on-shell-scripting/ 11/17
7/1/2017 10 Useful 'Interview Questions and Answers' on Linux Shell Scripting

2. Subscribe to our email

updates: Sign Up Now


3. Get your own self-hosted

blog with a Free Domain at

($3.95/month).
4. Become a Supporter - Make
a contribution via PayPal

5. Support us by purchasing
our premium books in PDF
format.
6. Support us by taking our

online Linux courses

We are thankful for your never


ending support.

Tags: linux interview

PREVIOUS NEXT STORY

STORY Install
RainLoop

https://www.tecmint.com/interview-questions-on-shell-scripting/ 12/17
7/1/2017 10 Useful 'Interview Questions and Answers' on Linux Shell Scripting

Installing Webmail (A
LAMP (Linux, Web Based 

Apache, Email Client)
MySQL/MariaDB, using ‘Nginx
and and Apache’ in
PHP/PhpMyAdmin) Arch Linux
in Arch Linux

 YOU MAY ALSO LIKE...

7  17 4

Practical 10 Linux 10 Useful


Interview Interview Interview
Questions Questions Questions
and and on Linux
Answers on Answers Services
Linux Shell for Linux and
Scripting Beginners Daemons
7 JUN, 2014
– Part 3 10 MAR, 2014
7 DEC, 2013

20 RESPONSES

 Comments 5  Pingbacks 0

zen  April 25, 2016 at 8:59 am


isn’t command ls used to list les in a
directory?
Reply

Zool  August 20, 2016 at 2:49 pm


It meant to say “less” rather
than “ls”. As it is, yeah. It’s
wrong.

https://www.tecmint.com/interview-questions-on-shell-scripting/ 13/17
7/1/2017 10 Useful 'Interview Questions and Answers' on Linux Shell Scripting

Reply

Warron French
 February 20, 2017 at 7:36 am
The way the question was
asked wasn’t very clearly stated
to accomplish the use of the
command ls and echo to
answer it in the a rmative and
explain.

The question should have been,


“Is it possible to list the le and
directory names of a given
‘parent directory’ using a
command other than ‘ls?'”
Yes, the use of “echo *” will
perform the equivalent of a
plain “ls.”
Reply

jagan  March 8, 2016 at 7:40 pm


Hi,

I need to execute a script on remote as


a sudo user or root user from my local
VM. It is not password less login. we
need to pass password thorugh
command line.

#!/bin/bash
sshpass -p “xxxx” ssh -o
StrictHostKeyChecking=no
user@abc.com <<EOF
echo "password" | 'sudo su -'
sh script.sh
EOF

Note : there is no sshpass in remote


server and we cannot install it for
security reason.

I am able to login to remote user as user


but i can't login as a sudo user , Please
help me how to achieve this. i have seen
many quires but none one helped me
-Thanks
Jagan
https://www.tecmint.com/interview-questions-on-shell-scripting/ 14/17
7/1/2017 10 Useful 'Interview Questions and Answers' on Linux Shell Scripting

Reply

Ravi Saive
  March 9, 2016 at 10:49 am
@Jagan,

In CentOS, there isn’t any sudo


user by default and the all the
programs run with root
privileges as root user, if you
want to run enable sudo for
user, you shoud use visudo
command to enable it as
shown:
# visudo
And add this entry to enable
sudo privileges to your user.
yourusername ALL=(ALL) ALL
Reply

« Older Comments

GOT SOMETHING TO SAY? JOIN THE


DISCUSSION.

Comment

Name * Email *

Website

Notify me of followup comments via e-


mail. You can also subscribe without
commenting.

Post Comment

https://www.tecmint.com/interview-questions-on-shell-scripting/ 15/17
7/1/2017 10 Useful 'Interview Questions and Answers' on Linux Shell Scripting

LINUX MONITORING TOOLS LINUX INTERVIEW QUESTIONS OPEN SOURCE TOOLS

Icinga: A Next Generation Basic Linux Interview 12 Open Source Cloud


Open Source ‘Linux Server Questions and Answers – Storage Software to Store
Monitoring’ Tool for Part II and Sync Your Data Quickly
RHEL/CentOS 7.0 and Safely
10 Useful ‘ls’ Command
How to Add Windows Host Interview Questions – Part 2 15 Best Linux Photo/Image
to Nagios Monitoring Server Editors I Discovered in 2015
10 Advance VsFTP Interview
Linux Performance Questions and Answers – 6 Best Email Clients for
Monitoring with Vmstat and Part II Linux Systems
Iostat Commands
15 Interview Questions on 13 Best File Managers for
Dstat – A Resourceful Tool Linux “ls” Command – Part Linux Systems
to Monitor Linux Server 1
Performance in Real-Time 6 Best Mail Transfer Agents
Nishita Agarwal Shares Her (MTA’s) for Linux
httpstat – A Curl Statistics Interview Experience on
Tool to Check Website Linux ‘iptables’ Firewall
Performance


https://www.tecmint.com/interview-questions-on-shell-scripting/ 16/17
7/1/2017 10 Useful 'Interview Questions and Answers' on Linux Shell Scripting

Tecmint: Linux Howtos, Tutorials & Guides © 2017.


All Rights Reserved.     
This work is licensed under a (cc) BY-NC
The material in this site cannot be republished
either online or o ine, without our permission.

https://www.tecmint.com/interview-questions-on-shell-scripting/ 17/17

S-ar putea să vă placă și