Search This Blog

Unix Shell Scripting interview question and answers

1)what is the difference between grep and find commands?
GREP is for finding out the string pattern inside a file
FIND is an utility for searching file and folders based on size, access time, modification time.
2)how to check size of file?
du -sm <filename>   this will give file size in MB
du -sg  <filename>   this will give file size in GB
3)how to search recursively in all subdirectories
grep -r “pattern” *
4)what is $! will provide in scripting ?
$0 will give script name
$? will give status of last command.
$! will give process id of last background Job
$# will give no. of arguements passed to script.
5)How to get yesterday and tomorrow date.
> date
Sun Jun 9 18:11:21 BST 2013
> YESTERDAY=`TZ=IST+24 date `;echo $YESTERDAY
Sat Jun 8 17:11:27 IST 2013
> TOMORROW=`TZ=IST-24 date` ; echo $TOMORROW
Mon Jun 10 17:23:12 IST 2013
6)How to housekeep all .xml files which are created three months back?
find . -ctime +90 -name “*.xml” -exec rm {} \;
7)what is paste in unix shell scripting.
Paste is unix utility which is used to join files horizontally or parallel merging.
>cat names.txt
Mark Smith
Bobby Brown
Sue Miller
Jenny Igotit
>cat numbers.txt
555-1234
555-9876
555-6743
867-5309
>paste names.txt numbers.txt
Mark Smith 555-1234
Bobby Brown 555-9876
Sue Miller 555-6743
Jenny Igotit 867-5309
paste -s names.txt numbers.txt
Mark Smith          Bobby Brown                 Sue Miller         Jenny Igotit
555-1234               555-9876                       555-6734         867-5309
8)tell some basic operation of sed
1)delete
2)substitute
3)print
e.g. Pattern matching
tmp> cat test
abc abc
ram
daddy daddy
quest>find out all line with duplicated word
ans>cat test|sed -n ‘/\([a-z]*\) \1/p’
quest>how to remove duplicated word?
ans> cat test|sed ‘s/\([a-z]*\) \1/\1/g’
abc
ram
daddy

No comments:

Post a Comment