UNIX / Shell Programming Basic – Beginner Questions 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 <filename.txt>

 

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

 

  

thanks – shivmohan purohit 

 

 

 

 

 

 

 

About these ads

9 responses to “UNIX / Shell Programming Basic – Beginner Questions Answers

  1. excellent

  2. Pingback: Top Referred Articles on KnowOracle | Shivmohan Purohit's Oracle Applications Blog

  3. Pingback: Top Articles in Dec – 2010 | Shivmohan Purohit's Oracle Applications Blog

  4. Pingback: KnowOracle – Top Documents – Most Read articles « Oracle Applications – Business & Technology

  5. please send me info about unix-scripting how it

    will used in sql,pl/sql

  6. Gr8 article.At what scenario do we need to go for awk?

  7. that was a very helful one …. :)

  8. It’s really helpful

  9. now that’s a great article

    keep it up =)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s