User:Spoonbender/Temp/The Shell.doc

From WikiEducator
Jump to: navigation, search

The Shell: The Command Processor

The shell is an interactive program that provides an interface to the operating system. The shell interprets every command we typed in the keyboard. It is separated from the operating system to allow users to change the shell if they are not satisfied with the features it provides. The shell allows every user to type commands and passes these commands to the kernel for command execution.


The following are activities performed by the shell:

  • The shell issues the $ prompt and waits for the user to enter commands.
  • It scans the command line for special characters after the user has entered the command.
  • It then passes the simplified command to the kernel for execution.
  • After the command has been executed, the shell will again issue the $ prompt and sleeps until the user has typed again the command.

After the user has logged in, a shell prompt will be displayed and will wait for the commands to be typed. Once the return key is pressed, the shell processes these commands. When finished, the shell re-displays the prompt. This process continues until the user exits the shell.


Commonly Used UNIX Shells

  • /usr/bin/sh POSIX shell
  • /usr/bin/ksh Korn shell
  • /usr/old/bin/sh Bourne shell
  • /usr/bin/bash Bourne Again Shell
  • /usr/bin/csh C shell
  • /usr/bin/keysh A context-sensitive softkey shell
  • /usr/bin/rksh Restricted Korn shell
  • /usr/bin/rsh Restricted Bourne shell


Some of the shell features are the following:

  • Command History – this is a feature wherein commands are stored in a buffer for reuse and modification purposes. Executed commands can be saved to a file that could be searched later. This can also be helpful in trying to detect mistakes that might have been committed.
  • Command Line Editing – this is a feature where you can edit or modify currently or previously used commands by means of vi editor.
  • Job Control – this is the ability of the shell to switch programs in the background. Commands that do not require input from the keyboard or display output will bring in the background as separate processes or tasks.
  • File Name Completion – this is the ability to finish typing commands automatically in a command line. Filename completion permits you to type in a partial file name, and then by pressing a key the shell attempts to complete the name of the file for you.
  • Command Aliasing – this is a feature where you can abbreviate long commands, rename commands and automatically include options. Basically aliasing allows you to set one command to run something else.
  • Restricted shell – ability of the shell to provide an environment with limited capabilities.


Aliasing

You know that an alias is a new name for something so we will use alias to rename a command. Aliasing is a method of creating a new name for commands, abbreviating long commands or making other commands to perform other tasks by the use of alias.


Syntax:

alias name=string

where name is the alias for the command contained in the string. If the string contains many spaces, you need to enclose the string inside the quote. If you will use alias you will save time in typing very long commands, prevent typing error and create new commands.


An alias is similar to other commands that you entered. The shell will interpret the alias that you make then pass it to the kernel for execution. The aliases you make are available until you log out.

Be sure not to use the same aliases in different commands because this will generate an error in the execution of the commands. If an alias is used without any arguments this will report all the aliases you made. To turn off the aliases, you may use the command unalias. The syntax for this command is


unalias name


Examples:

$ alias copy=cp

$ alias list=’ls –l’

$ alias dir=/home/usr/tree

$ alias list dir

-rwxrwxrwx1lecyprof23Oct 4 11:45 hello

-rw-r-xrwx2owenprof34Oct 5 10:20 hi

-r-xrwx--w1gayadm12Oct 6 03:32 note1

$ unalias list


Command File Name Completion

File name completion is important especially when accessing long file name. The system will automatically finish typing command if you press ESC ESC after you type the starting character of the file. If the file that begins with the characters you typed does not exist, the shell will beep at you, and will not complete the filename. If more than one file begins with the characters you typed, the shell will complete the name up to the point where the names differ form the others. Then you can type additional letters to zero-in on the file you want.

If there is more than one file containing the same characters you typed, you can view the list by typing ESC =. After viewing the list, you may now type the next character of the file you want to access then press ESC ESC to get the desired filename you want to access.


Examples:

$ more exp ESC ESC

$ more explosion

$ more acd ESC ESC

$ more acdfg ESC =


1) acdfgXmanip

2) acdfgYmanip


$ more acdfgX ESC ESC

$ cd /tr ESC ESC fr ESC ESC l

$ cd /tree/fruits/apple


Command History

The shell keeps or stores commands that you enter in a buffer and allows you to use these commands in the future. The files stored in the buffer are maintained across login sessions. To display the entered commands you need to use the history command.

History helps you get a list of the commands that you have previously executed. Executed commands are saved to a file that can be searched for later use. This is very helpful for repeating commands with long hairy syntax. This can also be helpful in trying to detect mistakes you have made.

Syntax:


history [-n| a z]


By default the history command will display the last 16 commands you have entered and a command number precedes these commands. You can also display more than or less than the default display of commands by appending –n, where n is the number of commands you want to display.


The variable name that gives the number of commands you can access is the HISTSIZE variable. By default, only the last 128 commands you entered can be accessed. And the HISTFILE is the variable that specifies the text file created and will store the commands you have entered. By default the HISTFILE is .sh_history. This is where you can modify the HISTSIZE to access more than 1287 commands.


Examples:

$ history –3Display the last 3 commands

125 cp

126 cd

127 pwd

$ history 1 3Display the command numbers 1 through 3

1 ls lecy

2 more Frankenstein

3 mail


Recalling and Reentering Commands

The shell provides a mechanism where you can recall previously typed commands and executes these commands without retyping. You can also edit these commands by using the vi editor. This feature will help you save time in retyping long commands. In order to use vi, be sure that you have a variable EDITOR set in your environment. If this variable is not present in your environment type the following command:


$ EDITOR = /usr/bin/vi

$ export EDITOR


This will tell the shell that you want to use vi to recall and modify the previous commands that you typed. If you already have this variable you can now recall commands. To recall commands, type ESC. When you type escape you actually enter into a vi editor. The character j is used to scroll the cursor forward and k is used to scroll the cursor backward. If you already see the command you want to execute again, press Return or Enter.


If you use the history command to view the previous command you typed and you want to execute it again, you will need to type ESC n G, where n is the number of the command you want to execute.


Example:

$ history -2

125 pwd

126 cd

$ ESC 126G Return


You can also run commands from the command history by the use of r command.

Syntax:

r c

where c is the number of command you want to execute. You can also type the first character of the command and execute the recent command that starts with the specified character.


Example:

$ history -2

125 pwd

126 cd

$ r 125

/home/usr/tree

$ history 1 3

1 ls

2 ls –l

3 ls –F

$ r l

fruits/ lecy* owen* hello/


Command Line Editing

There are times that you may need to recall and reuse a command, but may need to modify this command. By pressing ESC and k you can recall the last commands you have entered. But if you already know the command number, you can start typing the command number followed by G to bring you to that command. You can also type locate commands in the command stack by typing ESC k followed by a / and pattern.


Example:

$ history –6

121 pwd

122 cd

123 ls

124 banner hello

125 echo hello everybody

126 banner hi

$ ESC k /banner

# # #####

# # #

###### #

# # #

# # #

# # #####


After locating the commands, you can now modify or do some editing with that command line before executing. In editing a command line you need to know some keys that will be used in vi editor. To move the cursor to the left type ESC thenh but if you want to move the cursor to the right you need to type l. If there’s something you want to delete in that command line, move the cursor to the first character you want to delete then type ESC x. x will delete each character. If you want to append a character, move the cursor at the ending character of the word then type ESC a. But if you want to insert a character, move the cursor at the beginning character of the word then type ESC i.


Keys to Press in Editing Command Line


l or Space Move the cursor to the right
h or backspace Move the cursor to the left
x To delete a character
dw To delete a word
a To append a character
i To insert a character
cw To change a word
p To paste text


Common Variable Assignments

There are still environment variables that need to be discussed. The following variables are some of the environment variables that you can view if you type the command env. As observed, environment variables are in upper cases. This is implemented to identify local variables from environment variables.


EDITOR Use vi for command line editing
HOME Assigns your login directory
LOGNAME Assigns your username
PS1 Assigns your primary prompt
PS2 Assigns your secondary prompt
PWD Designates your current working directory
PATH Assigns directories where you can find commands
MAIL Assigns the system mailbox