24 Jul 2008

UNIX Interview questions

I recently helped prepare a friend with some interview questions. I thought they might be helpful to someone to get some rust off.

Here they are:
  1. whats $# in a shell script?
    >> Answer: Count of arguments provided to a script
  2. whats a $* in shell script?
    >> Answer: All arguments provided to the script as a single word
  3. Explain nohup with an example
    >> Answer: A UNIX command that allows a command to continue running even after the user is logged out. Basically it prevents the handler for the HUP (hangup) signal from executing. For example,

    $nohup ls -lR >a_very_long_running_list_command.txt &

    Note that & at the end. Execute this command-line without the nohup prefixed and the job will get killed the moment you logout.
  4. If $1 is the first parameter, whats $0?
    >> Answer: $0 (dollar zero) represents the command name. For example:

    $my_script.sh param1

    here $0 = my_script.sh and $1 = param1. Also, note that $# will be 1 (since there's only one command line parameter provided)
  5. How do you see the return code of the last executed command?
    >> Answer: By checking the value of $? (Dollar Question mark)
  6. Explain Cron with an example
    >> Answer: Cron is the UNIX scheduler component (or daemon as it is more appropriately called). It can be used to schedule tasks for run at a specific time or times. It is driven by a crontab file (usually /etc/crontab). See more info on Cron at wikipedia.org.
  7. What is rsync
  8. Whats the advantage of using ssh over telnet?
  9. Have you used vi? provide example of searching an expression across multiple files (say *.sh) from within vi
  10. How can i find all /tmp/*.zip and move them to, say, /home/to_examine (assuming the target already exists?)
  11. Whats the kill command used for? Whats the difference between kill and kill -9
  12. Whats the "nice" command used for?
    >> Answer: It is used to change the priority of a running process. This utility is particularly useful in taming processes that demand higher CPU time with the scheduler. See nice on wikipedia.
  13. Provide a regular expression for searching email addresses within a script file
    >> Answer: In it's simplest form, the regular expression would look like this:

    [\w-]+@([\w-]+\.)+[\w-]+

    Tip: Try looking here for more comprehensive options. There's also a Firefox addon for this or simply use a web version here.
  14. Compare /etc/fstab and /etc/mtab
    >> Answer: Both are used by UNIX to maintain information on filesystems and mounting-related information. The only difference is that fstab contains the master list which is used by the mount command, whereas mtab contains a list of filesystems that are currently mounted.
  15. What do you know of inodes?
  16. Compare Unix FS with, say, Windows/DOS FS
  17. Does UNIX FS require defraging? If yes, whats the command. If no, why? ;-)
  18. How do you redirect std error to std output?
  19. whats the command line for listing the names of the 10 biggest files under /home/
Like them? Hate them? Have corrections or have new ones? Post them as comments. I will try my best to answer them. Anyone else is welcome to try, of course!

Update: (article originally authored in June 2006) It seems these questions have started making the rounds of forums. But I didnt see any answers (yet) So i have answered some ;-)

See also: Combine commands in DOS - like you do in UNIX.