Tuesday, October 26, 2010

Shell script job Interview Questions

1. How will you write a shell script to connect to MySQL and SQL database?
Connect to MySQL database:
----------------------------------------------------------------
#!/bin/bash
TABLE_NAME=sometable
USER_NAME=someuser
IP_ADDR=localhost
PASSWORD=somepassword
mysql -h $IP_ADDR -u $USER_NAME -p$PASSWD <
# connect to test database and query the EMP table
select * from $TABLE_NAME;
EOF

---------------------------------------------------------------------

Connect to SQL/Oracle database
--------------------------------------------------------------------
#!/bin/bash
sqlplus / << EOF
select sysdate from dual;
exit;
EOF
----------------------------------


2.What does UID and GID signify?

UID: User ID
GUI: Group ID

They are used for a first-cut security in Unix systems in that files and programs can be restricted to certain groups or to certain users. Groups are kept in the /etc/group file users and the group they belong to are kept in the /etc/passwd file.
To find a user's UID or GID in Unix use the id command.

To find a specific user's UID at the Unix prompt enter:

id -u username

To find a user's GID at the Unix prompt enter:

id -g username

If you wish to find out all the groups a user belongs to instead enter:

id -G username

3.In shell scripting How to indentify that the previous command was run successfully?
echo $?

if the output of command is 0 than succesfully executed
if the output is non -zero than not successfully executed.

4.What are the different security mechanisms available in UNIX?
Unix is having 3 ways of Security Mechanism:

a. By granting or revoking File permissions. Owner or Admin can change permissions to be given to group or others by using chmod command in Unix.

b. Login is restricted using login credetials ( User name and Password ).

c. Password is kept in encrypted format in the file /etc/passwd

5. How do you search the string for vowel's occurrence and number of occurrences of each vowel?
grep -io [aeiou] filename | wc -w

6.How to compare floating point number in shell scripting?
#!/bin/bash
x=3.1;
y=3.2;
Result=`echo "$x > $y" | bc`


if [ $Result -eq 1 ]
then
echo "x is greater"
else
echo "y is greater"
fi

7.How to delete a word from a file using shell scripting
For example delete EntityName from myfile.txt:
using awk:
awk '{gsub(/(EntityName):/,""); print}' myfile.txt
using sed:
sed -e 's/EntityName://g' myfile.txt
using perl:
perl -i.bck -ple's/(?:(?:EntityName)://g' myfile.txt

8. How to extract the second row of a text-file?
sed -n '2p' filename

9. How to find see the file which is created today?
find . -mtime -1 -print

10. What is the basic difference u find between a shell script and perl.
1) PERL scripts can be used on both UNIX and windows systems unless some OS specific commands are used. But the same case is not with Shell scripting.

2) PERL scripts are used for web based applications but shell scripts can not be used for the same purpose.

3) PERL modules gives PERL extra edge over Shell scripts. PERL modules are extensive and can be used for n number of purposes.

11: What is use of "cut" command ?Give some examples.
Cut is a powerful command in Unix Cutting fields we can use the command for example we would like to extraxct first 3 col

cut -c1-3 sample (Sample is a file name)

To extract first and 3rd col

cut -d;-f1 -f3 sample

12 What is the difference between process and thread.?
A process is a collection of threads that share the same virtual memory. A process has at least one thread of execution, and a thread always run in a process context.

Thread is used to allocate and distribute the CPU work scheduling, many programs a re assigned to different threads and they are most of the time independant of each other.

13. What do $# and $? stand for?
$# returns the number of parameters that are passed to a shell script
$? returns the exit code of the last executed command (0 : Successful, 1 or other: Failed)

14.  A Bourne-Again shell script to convert JPEG images to PNG images.
#!/bin/bash
# use $jpg in place of each filename given, in turn 
for jpg in "$@" ; do 

png="${jpg%.jpg}.png" 
# find the PNG version of the filename by replacing .jpg with .pngecho converting "$jpg" ... 
    
# output status info to the user running the script
 if convert "$jpg" jpg.to.png ; then 
    
# use the convert program (common in Linux) to create the PNG in a temp filemv jpg.to.png "$png" 
        mv jpg.to.png "$png"
# if it worked, rename the temporary PNG image to the correct name 
else 
    
# ...otherwise complain and exit from the script 
echo 'error: failed output saved in "jpg.to.png".' 1>&2
        exit 1
    fi 
        
# the end of the "if" test constructdone 
# the end of the "for" loopecho all conversions successful 
# tell the user the good news
15.   How do you schedule a command to run at 4:00 every morning? 
 
crontab -e
* 4 * * * <command> 

16. What are the different kinds of loops available in shell script?

Broadly categorised in 3
for
while
until

if and case are not loops but rather control structures

No comments:

Post a Comment