Search This Blog

Monday 4 April 2016

Unix Interview questions

1.      How do we kill a process group and all children of that process group with single command?
Ans: If we want to kill a process group, you can supply negation of group number to kill as “kill –term -1234”, where 1234 is process group id i.e. PGID. If we just use ps -ef, we will not get PGID, instead we get PPID and PPID can not be used to kill process group. To identify the PGID, use PS with -o option.
2.      Using the regular expression in grep, how do we retrieve all the lines which have A as 6th character?
Ans : grep “^.\{5\}A"
Explanation: .(dot) will tell grep that 1 or more characters and \{5\} tells that total 5 characters. Then A is 6th character.
3.      How do we debug the unix script?
Ans:  use –x while running script using ksh or code “set –x” in the script to print all the lines in script
4.      Set –x prints commands after variable substitution. How do you print or echo commands before variable substitution?
Ans:  set -v
5.      If we want to refer a variable or input parameter of parent process in the child process, how do we call the child process?
Ans:  Child process within unix script could be invoked as internal process or external process. If we want to refer input parameters or variables of parent process in the child process, then we need to invoke the child process as internal process. (this can be done as “. Script_full_path” )
For variable usage in the internal process, we need to export the variable.
Input parameters of parent process could be used in the internal child process same way as they used in parent process (i.e. using $1, $2 etc)
6.      How do we know what all processes currently running in the system?
Ans: using ps command.
7.      How could we print 11th line in a file containing 20 records?
Ans: we could use either HEAD and TAIL combination or SED
Using HEAD and TAIL :  head -11 file_name | tail -1
Using SED :   sed –n ’11 p’ file_name
8.      How do we print first 3 words in a record?
Ans: we could use awk, cut to print words treating each word as a field separated by space.
Using cut:  cut –f1-3 –d’ ‘ file_name
Using awk:  awk –FS=” “ ‘{print $1,$2,$3}’ file_name
9.      How do we erase all files in the current directory, including all its sub-directories, using only one command?
Ans: rm –r *
We have another command called "shred" used to overwrite a file number of times (default 25) to make it unrecoverable, not exactly same as rm. Files removed using rm also not recoverable , but file remains at disk (only inode link removed using rm) so sophisticated recoverable 3rd party tool could read therefore shred must be used to shred sensitive information. 
10.   How do we find out history of all commands executed?
Ans : using history command
11.   Differentiate cmp command from diff command?
Ans: The cmp command is used to find out if both file are same (byte by byte) or not. The diff command is used to indicate the changes that is to be made in order to make the two files identical to each other.
12.   If I am owner of a file and I want to change the file owner, how do I do that?
Ans: Only super user or root user can execute change owner command. The file owners can’t change the owner of a file.
In order to change the file owner or group, we can use CHOWN and CHGRP commands.
13.   If I want to echo the data to standard output as well as write to a file in single command, how do I do that?
Ans: using tee command
14.   How do mark end of the command in unix script?
Ans: Normally we use enter or return key to end the line. This end of line character act as signpost. But we could also use ; (semicolon) as command end mark.
15.   What is inode and how do we list them?
Ans: Unix file is stored into two different parts of the disk.  Data Blocks and INODEs
Data blocks contain the actual data
Directories  are tables that contain the link between the file name and inode
Inode contains the file related information like owner , permissions, size, last accessed, last updated, number of hard links
To list inodes we can use –I option in ls.  For example “ls –i” will list all Inodes
16.   what is difference between whois and finger?
ans: whois gives basic information about user logged in.  finger give more details/personal information about the users who are not even logged in. finger could also be used to get information about the remote host users.
17.   What is PS1?
Ans: command prompt string is stored in PS1. We can reset prompt string by changing the PS1 variable value
18.   What are the different unix standard streams?
Ans: stdin (file descriptor 0), stdout (file descriptor 1) and stderr (file descriptor 2)
Echo “abc “ > /dev/null 2>&1 will print nothing because
standard output with file descriptor 1 is directed to /dev/null/ (null or blackhole in disk) and standard error with file descriptor 2 is redirected to file descriptor 1 that is further redirected to /dev/null/
19.   Difference between command piping and grouping?
Ans: piping is the way of using output from one command as input to other command
Grouping is grouping multiple commands together and printing combined output
Piping Examples:  ls -al | more    -- this print one page of listing and pause and print more on enter
Grouping example: (date; cal; date) > out.txt          -- writes all date, calender  information to out.txt file
20.   What is xargs and how its used?
Ans:
If you combine two commands with | without using xargs, the first command output will be used as input data to second command, i.e. second command operate on first command output
But with xargs, first command output will be used as input parameter to second command i.e. second command will operate using first command output
By default xargs runs the command /bin/echo. We can tell xargs to run the command we want.
For example  
Find . –name *.sh | grep “CREATE TABLE”
                     This command will list all the .sh file and grep if any of the .sh file has name containing “CREATE TABLE”..
Find . –name *.sh | xargs grep “CREATE TABLE”
                     This command will list all the .sh file and grep if each and every .sh file if the content of the file has “CREATE TABLE” string
21.   How do we run the jobs as background task ?
Ans: using &
If we want to capture stdout then we can use nohup
background job can be brought foreground using "fg %jobnum", get jobnum using "jobs" command
cntrl+z can be used to interrupt a job and stop temporary and resume job using fg or bg commands (These commands are part of job monitor functions)
22.   How do we schedule a job in unix?
Ans: using crontab or "at"
Edit the crontab (crontab –e ) then add an entry
23.   Can I specify optional arguments for a script and if yes, how do we process optional arguments?
Ans: yes, we can specify using getopts
The getopts utility can be used to retrieve options and option-arguments from a list of parameters
Optional input parameters should be prefixed with -.
For example when running any unix command we give optional parameters right … for rm command, the positional parameter is file name and optional parameters are –f, -r etc..
We need to use while command to process all optional parameters.. example
While getopts ….alloptions, ex: abcdc …..  name
Do
      Case $name in
                a)
                 b)
      esac
done

1 comment:

  1. Marhaba,

    11/10!! Your blog is such a complete read. I like your approach with Unix Interview questions . Clearly, you wrote it to make learning a cake walk for me.

    I read in articles Linux and BSD . are increasingly filling the market needs traditionally served by proprietary Unix operating systems, as well as expanding into new markets such as the consumer desktop and mobile and embedded devices. Is it killing the Windows device market?

    Awesome! Thanks for putting this all in one place. Very useful!

    Cheers,
    Irene Hynes

    ReplyDelete