Saturday, June 6, 2009

UNIX interview questions and best practices.....

Q1.Write a shell script in Linux to shift all characters in a file forward by five characters. (Thus "a" becomes "f'").
Solution: cat test tr '[a-z A-Z]' '[f-za-e F-ZA-E]'

Q2.I have 2 files:-
file1 and file2

file1
SEED
RPTT
TST8

file2
SEED:db:Y
RPTT:db:Y
SED8:db:N
SEED:db:N
TST8:db2:Y
TRN8:db3:N
CNV8:db4:Y

I have to change third field of file2 to "y" for every entry in file1 matches first filed of file 2
and rest to N

I want file 2 like :-
as file 1 has (SEED,RPTT,TST8) so in file2 I want to do third field "y" for those entries(which is first field in file2).
In rest I want to convert to "N"
SEED:db:Y
RPTT:db:Y
SED8:db:N
SEED:db:Y
TST8:db2:Y
TRN8:db3:N
CNV8:db4:N

Solution: while IFS=: read a b _ ; do echo -n "$a:$b:" ; ([[ $(grep -w $a file1.txt) ]] && echo Y) echo N ; done <>

Q3. How u convert string "hi manu how are you?" to "Hi Manu How Are You?"
Solution: echo "hi manu how are you"sed -e 's/ [a-z]/\U&/g'\;'s/^[a-z]/\U&/g'

OR

echo "hi manu how are you" sed 's/\<[a-z]*\>/\u&/g'

Q4. write a shell script that counts a number of unique word contained in the file and print them in alphabetical order line by line?
Solution: $ rm -f res res1 ; while read line ; do cat uniqtest grep -wc $line >>res1 ; echo "$line :-> Count=" >>res ;done final_temp ; cat final_tempsort -u >final ; rm -f res res1 final_temp1
(Check file "final" for output)

Q5. How to rename all the files in a folder having specific extension? Example: I have some files with extension (.txt) in a folder name 'Test'. I have to rename all the .txt files in a test and its subdirectories to .my extension.
Solution: for file in `ls` ; do NEW=`echo $file sed 's/.txt/.my/g'` ; mv $file $NEW ; done

Q6. write a shell script to identify the given string is palindrome or not?
Solution:
#! /usr/bin/sh
len=0
i=1
tag=0
echo -n "Enter a String: "
read str
len=`echo $str wc -c`
len=`expr $len - 1`
halfLen=`expr $len / 2`

while [ $i -le $halfLen ]

do
c1=`echo $strcut -c$i`
c2=`echo $strcut -c$len`
if [ $c1 != $c2 ] ; then
i=$halfLen
tag=1
fi
i=`expr $i + 1`
len=`expr $len - 1`
done


if [ $tag -eq 0 ]

then
echo "String is Palindrome"
else
echo "String is not Palindrome"

fi

Q7. one file is a.txt which has 4 fields/columns, where as each field has separated by space and that field contains some data like below

ram usa 105 25
rag uk 115 26
pat ind 234 23
sah 425 24
tat usa 344 28
rat brz 536 29

now i want only 2nd field data, that should be like this

usa
uk
ind

usa
brz

(i.e. It should print blank line for the second field if it doesnt have country defined)
Solution: awk 'NF < 4 {$2=""} { print $2 }' a.txt

Q8. How to change all .my files to .txt in current directory??
Solution: for i in `ls *.my` ; do NEW=`echo $ised -e 's/.my/.txt/g'` ; mv $i $NEW ; done

Q9. How to change all .extn files to .my in entire directory recursively??
Solution:
find ./ -type f grep ".extn$" >files.txt;for i in `cat files.txt`;do NEW=`echo $ised 's/.extn/.my/g'`;mv "${i}" "${NEW}";done;rm -f files.txt

Q10: What is umask??
Answer: umask is used to set default permission level in entire unix system. It can be set in .kshrc/.bashrc/.cshrc or init (So that to load at the time of login into shell)

Q11. What is inode??
Answer : inode represents any file/directory residing in your unix system with a numner known as inode number. It is unique for every file/dir. It is basically a reference that kernel use for any work related to that entity. "ls -i filename" will give you the inode number of that particu;ar file.

Q12. Explain general commands like ps,df, export, env, ufsdump, tar, cron, system.
Answer: EASY ONE

Q13. Explain the make utility in unix?? what are the parameters of a makefile and how to use them. What are macros in makefile.
Answer: Unix make utility is used to build a software package. It requires a makefile where all the bunch of the scripts/codes/programs are declared/defined. The main parameters of the make file are variable declaration, Macro declaration, Phony Target declaration and Clean-up block. Macros in a makefile represents the order in which the scripts would get executed.

Q14. What are the benefits of makefile over shell script??
Answer: Using makefile one can run the different scripts or programs or codes in one go. This can also be done by making a wrapper shell script which can execute the different scripts in one go. But then it would not be termed as a package because when we say a package it means that we have one installer (.exe) which can execute the bunch more efficiently and also this .exe will be in encrypted format. So in order to maintain efficiency and security makefile is given precedence over wrapper shell scripts.

Q15. What are hard links in UNIX??
Answer: A hard link is a reference to a file or directory that appears just like a file or directory, not a link. Hard links only work within a filesystem. In other words, don't use hard links between mounted filesystems. A hard link is only a reference to the original file, not a copy of the file. If the original file is deleted, the information will be lost.
To create a hard link of the file /export/home/fred/stuff to /var/tmp/thing, use:

ln /export/home/fred/stuff /var/tmp/thing

The syntax for creating a hard link of a directory is the same. To create a hard link of /var/www/html to /var/www/webroot, use:

ln /var/www/html /var/www/webroot

Q16. What are the soft/symbolic links in unix ??
Answer: A symbolic link is a pointer to another file or directory. It can be used just like the original file or directory. A symbolic link appears in a long listing (ls -l) with a reference to the original file/directory. A symbolic link, as opposed to a hard link, is required when linking from one filesystem to another and can be used within a filesystem as well.
To create a symbolic link, the syntax of the command is similar to a copy or move command: existing file first, destination file second. For example, to link the directory /export/space/common/archive to /archive for easy access, use:

ln -s /export/space/common/archive /archive

To link the runtime control script /etc/init.d/httpd to /etc/rc2.d/S77httpd, use:

cd /etc/rc2.d
ln -s ../init.d/httpd S77httpd

Q17. What is a command to kill the last background job??
Answer: kill $!

Q18. Bring a job to foreground by specifying its job number after the percent sign.
Answer: fg %jobnumber
(Note: jobnumber can be obtained by using jobs -l command)

Q19. Explain top command.
Answer: top provides an ongoing look at processor activity in real time. It displays a listing of the most CPU-intensive tasks on the system, and can provide an interactive interface for manipulating processes. It can sort the tasks by CPU usage, memory usage and runtime. can be better configured than the standard top from the procps suite. Most features can either be selected by an interactive command or by specifying the feature in the personal or system-wide configuration file.

Q20. Write a script which give you those records which are having CPU utilization more than 80%.
Answer: df -k awk 'NR==1{print $1"\t"$5"\t"$6}sub("%","",$5){if($5 >= 80) print $1"\t"$5"%\t"$NF}'

Q21. How to pass arguments in an awk script??Answer: Using, cat a.txtawk -v awk_var="$shell_var" '{print $1 "\t" awk_var}'
e.g. #! /usr/bin/sh
var=$*
cat ussd.txt >offpeak_input.txt
cat offpeaknawk -v var1="$var" '{print "\"1-"$1"\"""""\""$2"\"""""1006"""""$3"""0""""-1""""-1""""\""var1" .Account balance is Rs..Bonus minutes.\""}'>>offpeak_input.txt


!!!!!!!!!! CHEEEEEEEEEEEERRRRRRRRRZZZZZZZZZZZZZZZZZZZZZZZZZ !!!!!!!!!!

4 comments:

Anonymous said...

Hi

Tks very much for post:

I like it and hope that you continue posting.

Let me show other source that may be good for community.

Source: Top interview questions

Best rgs
David

Unknown said...

It was very nice article and it is very useful to Linux learners.We also provide Linux online training

Unknown said...

Hello There,


Great piece on UNIX Best Practices, I’m a fan of the ‘flowery’ style Looking forward to more long form articles

Unix popularized a syntax for regular expressions that found widespread bash and shell scripts use in Unix. But there are so many automation tools came in recently that pretty much automates all the batch job and scripts, so does it means the Unix scripts won't be used anymore?

But nice Article Mate! Great Information! Keep up the good work!


Obrigado,
Abhiram

Manu Swami said...

Thanks Abhiram.

About your comment "does it means the Unix scripts won't be used anymore?". I would say it differently, regex using awk, sed etc have opened up more minds globally to complete a task faster than never. They are putting up logics here and leaving Excel for coloring the tables. So people are imbibing it.

As far as coding, there are better languages in the market which is easy to use when writing applications. Such as Python.

Ciao, Manu

 

Blogger news

Blogroll