Important Shell Scripting Questions with Answers


What is $*?
Its mainly used for showing up all params. This show all parameter values passed in shell script

What does $# stand for?
# will return the number of parameters that are passed as the command-line arguments.

What does $? Return?
$? will return exit status of command .0 if command gets successfully executed ,non-zero if command failed.

What are Different types of shells?
sh : the oldest shell
csh : C shell
ksh : Korn Shell
bash : bourne again shell 

How do you read arguments in a shell program – $1, $2?
Shell script accepts parameters in following format…
$1 would be the first command line argument, $2 the second, and so on
$0 is the name of the script or function
If your script has more than 9 params then accept in following way…
${12} : 12th param
${18} : 18th param

What are the different kinds of loops available in shell script?
for, if, while, case

What is the difference between a shell variable that is exported and the one that is not exported?
The Shell variable which is exported would available to all the programs outside the Shell also. And the shell variable which is not exported, would available for that shell or for the shell program only, in which the variable is declared.

Export LANG=C
will make the variable LANG the global variable, put it into the global environment. All other processes can use it.

LANG=C
will change the value only in the current script. 

  
If you have a string “one two three”, which shell command would you use to extract the strings?
echo $string | cut -d” ” -f1
echo $string | cut -d” ” -f2
echo $string | cut -d” ” -f3 

How will you list only the empty lines in a file (using grep)?
grep “^$” filename.txt

How would you get the character positions 10-20 from a text file?
cat filename.txt | cut -c 10-20
or
cut -c10-20

How would you replace the n character in a file with some xyz?
sed ‘s/n/xyz/g’ filename > new_filename

We can replace n characters by using the following command:
1,$s/./xyz/g
where 1 shows that the search string will start searching patterns from first line of the file.
           ‘.’ for any character.
            g for global replacemet.


What is the difference between a ‘thread’ and a ‘process’?
A process is a collection of virtual memory space, code, data, and system resources. A thread is code that is to be serially executed within a process. A processor executes threads, not processes, so each application has at least one process, and a process always has at least one thread of execution, known as the primary thread. A process can have multiple threads in addition to the primary thread
Thread – is stream of executable code within process. They are light weight process. All thread with in a process  share process instruction,code & data segment,open file descriptor,signal handler,userID and GroupID. Thread has its own set of register including program counter,stack pointer 

What is this line in the shell script do ?#!/bin/ksh?
To invoke the shell indirectly this line is added as the first line in the file.This particular line invokes korn shell
  
What is use of “cut” command? Give some examples.
Cut – Utility used to cut/Strip out the required data/text from the source.
Cut can be used in three modes,
  Stripping by Character
     cut -c 1-3
  Striping by Byte length
     cut -b -1-72
Stripping by delimiter and fields.
     cut -d “|” -f1
Where   
-d “|” -> Delimiter used in input text to separate columns
-f1 -> Field/Column number  
While processing huge input files, Cut’s performance is far better than awk

Write a command to find all of the files which have been accessed within the last 30 days.
find / -type f -atime -30 > December.files
This command will find all the files under root, which is ‘/’, with file type is file. ‘-atime -30′ will give all the files accessed less than 30 days ago. And the output will put into a file call December.files.

Comments

  1. Nice Blog . . Really Very Helpful.

    -Krish

    ReplyDelete

Post a Comment

Popular posts from this blog

How to create Oracle stored Procedures using TOAD for Oracle

Create Tables in Oracle Database using TOAD

How to Create Packages in Oracle Database using TOAD for Oracle