How will you get the last executed command in Unix?

We can use history command to get the list commands that were executed in Unix. Since we are only interested in the last executed command we have to use tail to get the last entry.

Exact command would be as follows:
% history | tail -2

What is the difference between a Zombie and Orphan process in Unix?

Zombie is a defunct child process in Unix that still has entry in process table.

Sometimes a child process is terminated in Unix, but the parent process still waits on it.

A Zombie process is different from an Orphan process. An orphan process is a child process whose parent process had died. Once a process is orphan it is adopted by init process. So effectively it is not an orphan.

Therefore if a process exits without cleaning its child processes, they do not become Zombie. Instead init process adopts these child processes.

Zombie processes are the ones that are not yet adopted by init process.

How does alias work in Unix?

We use alias in Unix to give a short name to a long command. We can even use it to combine multiple commands and give a short convenient name.

E.g. alias c=’clear’

With this alias we just need to type c for running clear command.

In bash we store alias in .bash_profile file.

To get the list of all active alias in a shell we can run the alias command without any argument on command line.

% alias
alias h=’history’
alias ki=’kill -9′
alias l=’last’

How can you redirect I/O in Unix?

In Unix we can redirect the output of command or operation to a file instead of command line interface (CLI). For this we sue redirection pointers. These are symbols > and >>.

If we want to write the output of ls –lrt command to a file we use following:
% ls –lrt > fileList.txt

If we want to copy one file to another file we use following:
% cat srcFile > copyFile

If we want to append the contents of one file at the end of another file we use following:
% cat srcFile >> appendToFile

What are the main steps taken by a Unix Shell for processing a command?

A Unix Shell takes following main steps to process a command:

  • Parse: First step is to parse the command or set of commands given in a Command Line Interface (CLI). In this step multiple consecutive spaces are replaced by single space. Multiple commands that are delimited by a symbol are divided into multiple individual actions.
  • Variable: In next step Shell identifies the variables mentioned in commands. Generally any word prefixed by $ sign is a variable.
  • Command Substitution: In this step, Shell executes the commands that are surrounded by back quotes and replaces that section with the output from the command.
  • Wild Card: Once these steps are done, Shell replaces the Wild card characters like asterisk * with the relevant substitution.
  • Execute: Finally, Shell executes all the commands and follows the sequence in which Commands are given in CLI.

What is a Sticky bit in Unix?

A Sticky bit is a file/directory permission feature in Unix.

Sometimes when we give write permission to another user then that user can delete the file without the owner knowing about it. To prevent such an accidental deletion of file we use sticky bit.

When we mark a file/directory with a sticky bit, no user other than owner of file/directory gets the privilege to delete a file/directory.

To set the sticky bit we use following command:

% chmod +t filename

When we do ls for a file or directory, the entries with sticky bit are listed with letter t in the end of permissions.

E.g. % ls –lrt

-rwxrwxrwt 5 abc abc 4096 Jan 1 10:10 abc.txt

To remove the sticky bit we use following command:
% chmod –t filename

What are the different outputs from Kill command in Unix?

Kill command in Unix can return following outputs:

  • 0: It means Kill command was successful
  • -1: When we get -1 from Kill command it shows that there was some error. In addition to -1 we get EPERM or ESRCH in output.

EPERM denotes that system does not permit the process to be killed.

ESRCH denotes that process with PID mentioned in Kill command does not exist anymore. Or due to security restrictions we cannot access that process.

How will you customize your environment in Unix?

In Unix, almost all the popular shells provide options to customize the environment by using environment variables. To make these customizations permanent we can write these to special files that are specific to a user in a shell.

Once we write our customizations to these files, we keep on getting same customization when we open a new shell with same user account.

The special files for storing customization information for different shells at login time are:

  • C shell: /etc/.login or ~/.cshrc
  • TC shell: /etc/.login or ~/.tshrc
  • Korn shell: ~etc/ksh.kshrc
  • Bash: ~/.bash_profile

What are the popular commands for user management in Unix?

In Unix we use following commands for User Management:

  • id: This command gives the active user id with login and groups to which user belongs.
  • who: This command gives the user that is currently logged on system. It also gives the time of login.
  • last: This command shows the previous logins to the system in a chronological order.
  • adduser: We use this command to add a new user.
  • groupadd: We use this command to add a new group in the system.
  • usermod: We user usermod command to add/remove a user to a group in Unix.

How will you debug a shell script in Unix?

A shell script is a program that can be executed in Unix shell. Sometimes a shell script does not work as intended. To debug and find the problem in shell script we can use the options provided by shell to debug the script.

In bash shell there are x and v options that can be used while running a script.

% bash –xv

With option v all the input lines are printed by shell. With option x all the simple commands are printed in expanded format. We can see all the arguments passed to a command with –x option.