Shell Scripting Q & A
Shell Scripting Q & A
3 ways
1. .sh scriptname.sh
2.If the script has execute permission - ./script.sh
3. If the script has execute permission and is stored in the directory listed in PATH ----script.sh or
.script.sh
what are special characters and explain how does text varies by the usage of single
quotes,double quotes and back quotes?
Special character like %,$,&,? are taken differently in unix as they have some different meaning and
use.
backslash (\) is used to mask the special meaning of these special character immidietly following it.
Double Quotes( "" )-- Anything enclose in double quotes removed meaning of that characters
(except \ and $).
How to create environment variables? What are the conditions for creating variables?
We can create environment variable by using export command.(Also by using set, but difference
between set and export is the which has been exported is being available in cloned/child shell.) export
VAR="Welcome"But problem is this variable has life span of session only. For making it permanent we
need to put that in login script.
How will you list only the empty lines in a file (using grep)?
grep "^$" filename
How would you print just the 25th line in a file using smallest shell script?
head -25 filename | tail -1
How would you replace the n character in a file with some xyz?
* sed 's/n/xyz/g' filename > new_filename
s -search and replace
n -character to be replaced
xyz - character to replace
g -global
* vi filename
:%s/n/xyz/g --search and replace
:w! ---save
What is MUTEX?
Mutex is a program object that allows multiple program threads to share the same resource, such as
file access, but not simultaneously. When a program is started a mutex is created woth a unique
name. After this stage, any thread
that needs the resource must lock the mutex from other threads while it is using the resource. the
mutex is set to unlock when the data is no longer needed or the routine is finished.
What is the difference between a shell variable that is exported and the one that is not exported?
the major difference between the shell variable that is exported and not is variable exported using
export command is global to all shell,where as the other shell variables are local to their respective
shells.
What is the difference between a 'thread' and a 'process'?
Process is a program in execution whereas thread is a separate path of execution in a program.
Process & Thread share almost all datastructure except (thread has it's own register & stack area).
Process is a program under execution,but thread is a light weight process which has seperate way of
execution.Threads are part of process.Single process can contain number of threads at a time.
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
command is as follows:
sqlplus <db_username>/<db_password> @test.sql
What is the basic difference you find between a shell script and perl?
Shell script is platform(OS) only for Unix
flavour,dependent whereas perl is OS independent
What are the three main forms of enabling debugging in a shell script?
set -x
set -v
set -n