Time To Earn

Saturday, November 30, 2013

Unix Job Interview Questions

Adding 30 days to current date using PERL inside SHELL

#! /usr/bin/ksh

dt1=`perl -l -e '$date=(localtime(time + (30 * 24 * 60 *60))); print $date;'`

dt=`echo $dt1|awk '{if($3<10) print $1" "$2" 0"$3" "$4" "$5;else print $0}'`

echo $dt|awk '{print $5"-"$2"-"$3"23:59:59"}'|sed -e 's/Jan/01/g'\;'s/Feb/02/g'\;'s/Mar/03/g'\;'s/Apr/04/g'\;'s/May/05/g'\;'s/Jun/06/g'\;'s/Jul/07/g'\;'s/Aug/08/g'\;'s/Sep/09/g'\;'s/Oct/10/g'\;'s/Nov/11/g'\;'s/Dec/12/g'





Print 3 lines before pattern match using sed and awk ..

Q: How do you display your running kernel version? (Solaris, AIX, Linux)
A: Linux # uname –r , Solaris # showrev

Q: Which command do you use to display a table of running processes? (Solaris, AIX, Linux)
A: Linux # ps –ef and top , Solaris # prstat

Q: Which file do you modify to configure a domain name resolver? (Solaris, AIX, Linux)
A: Linux # /etc/resolv.conf , Solaris # /etc/resolv.conf

Q: Which file contains a list of locally defined hostnames and corresponding IP addresses? (Solaris, AIX, Linux)
A: Linux # /etc/hosts , Solaris # /etc/hosts and linked file /etc/inet/hosts

Q: How do you display a routing table? (Solaris, AIX, Linux)
A: Linux # ip route show or #netstat –nr or #route –n and Solaris # netstat –nr and #route -n

Q: Which command would you use to view partitions and their sizes on Solaris?
A: # df -kh

Q: Which OpenBoot command would you use to print/view OpenBoot environment variables on a SUN server?
A: #printenv

Q: What does "ypwhich" command do? (Solaris, AIX, Linux)
A: # Will display NIS server to which client is connected to and which NIS Server is master for particular map specified with this command

Q: which command would you use to create an OS user on Solaris and Linux?
A: Linux # useradd and Solaris #useradd

Q: Which file contains passwords for local users on Solaris, Linux and on AIX?
A: Linux #/etc/shadow and Solaris # /etc/shadow

Q: Which command would you use to list partitions on Linux?
A: Linux # mount –l or # df -kh

Q: Which command/commands would you use to manage installed packages on RedHat Linux?
A: Linux # rpm

Q: What is the default port for SSH server?
A: 22

Q: Which command/commands would you use to manage installed packages on Solaris?
A: #pkginfo #pkgrm # pkgadd #pkgchk

Q: What command would you use to install an OS patch on Solaris?
A: #showrev –p and #patchadd -p

Q: Which Veritas command would you use to display a list of Veritas volumes?
A: # vxprint

Q: Which Veritas command would you use to display a list of disks on a system?
A: # vxdx list

Q: What is the main system configuration utility in AIX?
A:

Q: Which file has a list of filesystems to be mounted at boot time on Solaris, Linux and AIX?
A: Linux # /etc/fstab and Solaris #/etc/vfstab

Q. 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

Q. 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

Q: 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)

Q. 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.

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

Q. 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.

Q. 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.

Q. 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

Q. 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

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

Q. 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)

Q. 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.

Q. 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}'

Q. 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

1 comment:

  1. Hi admin,
    First of all I need to thank for sharing this blog. It is really helpful to me, keep sharing more like this.
    Unix Training in Chennai | Unix shell scripting Training in Chennai | Unix course in Chennai

    ReplyDelete