UNIX and Shell Programming (06CS36)

23
Shrinivas R. Mangalwede, GIT, Belgaum UNIX and Shell Programming (06CS36) Unit 3continued… Shrinivas R. Mangalwede Department of Computer Science and Engineering K.L.S. Gogte Institute of Technology, Belgaum. INDIA. [email protected]

description

UNIX and Shell Programming (06CS36). Unit 3 continued…. Shrinivas R. Mangalwede Department of Computer Science and Engineering K.L.S. Gogte Institute of Technology, Belgaum. INDIA. [email protected]. Customizing the Environment. The Shells – A Re-look Environment Variables - PowerPoint PPT Presentation

Transcript of UNIX and Shell Programming (06CS36)

Page 1: UNIX and Shell Programming  (06CS36)

Shrinivas R. Mangalwede, GIT, Belgaum

UNIX and Shell Programming (06CS36)

Unit 3continued…

Shrinivas R. MangalwedeDepartment of Computer Science and Engineering

K.L.S. Gogte Institute of Technology, Belgaum. INDIA.

[email protected]

Page 2: UNIX and Shell Programming  (06CS36)

Shrinivas R. Mangalwede, GIT, BelgaumShrinivas R. Mangalwede, G.I.T., Belgaum

Customizing the Environment

The Shells – A Re-look Environment Variables Common Environment Variables – their usage Aliases – Shorthand names for commands Command History In-line Command Editing – vi-like capability Miscellaneous Features

- Using set –o- Tilde Substitution

The Initialization Scripts- The Profile- The rc File

Page 3: UNIX and Shell Programming  (06CS36)

Shrinivas R. Mangalwede, GIT, BelgaumShrinivas R. Mangalwede, G.I.T., Belgaum

The Shells

UNIX shell is both an interpreter and a scripting language.An interactive shell runs a non-interactive shell when executing a shell script.Bourne Shell – Strong programming features, weak interpreter.C Shell – Improved interpretive features, wasn’t suitable for programming.Korn Shell – Combines best of the two. Has features like aliases, command history. But lacks some features of C.Bash Shell – Superset that combined the features of Korn and C Shells. Conforms to POSIX shell specification.

Page 4: UNIX and Shell Programming  (06CS36)

Shrinivas R. Mangalwede, GIT, BelgaumShrinivas R. Mangalwede, G.I.T., Belgaum

Environment VariablesShell variables are of two types: Local and environment.PATH, HOME, SHELL etc. are examples of environment variables.Environment variables are available in the user’s total environment including the sub-shells that run the shell scripts.Local variables are more restrictive in scope. The value of a local variable is not available to child processes. (Eg. Sub shell)

set: Displays all variables available in the current shell.env: Displays only environment variables.

env is an external command that runs in a child process whereas set is a shell built-in.

Page 5: UNIX and Shell Programming  (06CS36)

Shrinivas R. Mangalwede, GIT, Belgaum

Common Environment Variables

Variable Significance

HOME Home directory – the directory a user is placed on login

PATH List of directories searched by the shell to locate a command

LOGNAME Login name of user

USER Login name of user

MAIL Absolute pathname of user’s mailbox file

MAILCHECK Mail checking interval for incoming mail

TERM Type of terminal

Page 6: UNIX and Shell Programming  (06CS36)

Shrinivas R. Mangalwede, GIT, Belgaum

Common Environment Variables

Variable Significance

PWD Absolute pathname of current directory(Bash and Korn only)

CDPATH List of directories searched by cd when used with a non-absolute pathname

PS1 Primary prompt string

PS2 Secondary prompt string

SHELL User’s login shell

Page 7: UNIX and Shell Programming  (06CS36)

Shrinivas R. Mangalwede, GIT, BelgaumShrinivas R. Mangalwede, G.I.T., Belgaum

Some ExamplesTo include the directory /home/srm/bin in the search use,

PATH=$PATH:/home/srm/bin

To display your home directory use,echo $HOME

The location of the user’s mailbox is stored in MAIL. It is generally /var/mail or /var/spool/mail (Linux)MAILCHECK determines how often the shell checks the mailbox file for the arrival of new mail. Once the shell finds the file modified since last cjeck, it informs the user with the message,

You have mail in /var/mail/srm

Page 8: UNIX and Shell Programming  (06CS36)

Shrinivas R. Mangalwede, GIT, BelgaumShrinivas R. Mangalwede, G.I.T., Belgaum

Some ExamplesThe primary prompt string is $ and secondary prompt string is >.You can change the primary prompt string to C> as,

$ PS1=“C>”C> . . .

TERM indicates the terminal type. Every terminal has certain control characteristics that are defined in a separate control file in the /usr/share/lib/terminfo directory.

IFS contains a string of characters that are used as field separators for command and arguments. They are normally the space, tab and newline characters.

Page 9: UNIX and Shell Programming  (06CS36)

Shrinivas R. Mangalwede, GIT, BelgaumShrinivas R. Mangalwede, G.I.T., Belgaum

Bash and Korn Shells

$ PS1=‘[PWD] ‘[/home/srm] cd progs[/home/srm/progs] _

Bash and Korn also support a history facility that treats a previous command as an event and associates it with a number.$ PS1=‘[!] ‘ $ PS1=‘[! $PWD] ‘[42] _ [42 /home/srm/progs] _

$ PS1=“\h> “ // Host name of the machinesaturn> _

Page 10: UNIX and Shell Programming  (06CS36)

Shrinivas R. Mangalwede, GIT, BelgaumShrinivas R. Mangalwede, G.I.T., Belgaum

Aliases (bash and korn)

Alias is a shorthand name that you can assign to frequently used commands$ alias dir=‘ls –l’$ alias cdsys=“cd /usr/include/sys”

You can also use aliasing to redefine an existing command.$ alias cp=“cp –i”Every time you invoke an alias, their aliased version will be executed.To use original external command, precede the command with a \ (backslash).$ \cp file1 file2 overrides the alias

Page 11: UNIX and Shell Programming  (06CS36)

Shrinivas R. Mangalwede, GIT, BelgaumShrinivas R. Mangalwede, G.I.T., Belgaum

Aliases (bash and korn)

To display an alias definition use alias with the name,$ alias cpcp=“cp –i”

You can list all aliases by using alias without arguments and unset an alias with the unalias statement.

$ unalias cp

Page 12: UNIX and Shell Programming  (06CS36)

Shrinivas R. Mangalwede, GIT, BelgaumShrinivas R. Mangalwede, G.I.T., Belgaum

Command History (bash and Korn)

Bash and Korn treat a previous command as an event and associate it with an event number. Using this number, you can recall previous commands, edit them if required and reexecute them.The history command displays the history list showing event number of every previously executed commandBy default, bash lists the complete command history while korn lists the last 16 most recently used commands.

$ history 5 Bash$ history -5 Korn

History is stored in $HOME/.bash_history or $HOME/.sh_history

Page 13: UNIX and Shell Programming  (06CS36)

Shrinivas R. Mangalwede, GIT, BelgaumShrinivas R. Mangalwede, G.I.T., Belgaum

Accessing previous commands

By event numbers (! In bash and r in korn)$ !38 The command with event number 38 is displayed and executed (Use r 38 in korn)$ !38:p The command is displayed. You can edit and

execute it $ !! Repeats previous command (Use r in korn)$ !-2 Executes command prior to the previous one

( r -2 in korn)By Context$ !v Repeats the last command beginning with v

(r v in korn)

Page 14: UNIX and Shell Programming  (06CS36)

Shrinivas R. Mangalwede, GIT, BelgaumShrinivas R. Mangalwede, G.I.T., Belgaum

Substitution in previous commandsIf you wish to execute a previous command after some changes, you can substitute the old string with new one by substitution.

If a previous command cp progs/*.doc backup is to be executed again with doc replaced with txt,$ !cp:s/doc/txt in bash$ r cp doc=txt in korn

$_ is a shorthand feature to represent the directory used by the previous command.$ mkdir progs$ cs $_

Page 15: UNIX and Shell Programming  (06CS36)

Shrinivas R. Mangalwede, GIT, BelgaumShrinivas R. Mangalwede, G.I.T., Belgaum

The History variablesThe command history will be maintained in default history files viz.,.bash_history in Bash.sh_history in Korn

Variable HISTFILE determines the filename that saves the history list.Bash uses two variables HISTSIZE for setting the size of the history list in memory and HISTFILESIZE for setting the size of disk file.

Korn uses HISTSIZE for both the purposes.

Page 16: UNIX and Shell Programming  (06CS36)

Shrinivas R. Mangalwede, GIT, BelgaumShrinivas R. Mangalwede, G.I.T., Belgaum

Miscellaneous Features

Using set –oset –o noclobber: Prevents overwriting of an existing file with > symbol. To override this protection, use | after the >

ls –l >| file_listset –o ignoreeof: Prevents accidental logout when you press [Ctrl-d] to terminate standard input.A set option is turned off with set +o keyword.

Tilde SubstitutionTilde (~) acts as a shorthand representation of the home directory. A configuration file like .profile can be referred to both as $HOME/.profile and ~/.profile.

Page 17: UNIX and Shell Programming  (06CS36)

Shrinivas R. Mangalwede, GIT, BelgaumShrinivas R. Mangalwede, G.I.T., Belgaum

The initialization scripts

The effect of assigning values to variables, defining aliases and using set options is applicable only for the login session; they revert to their default values when the user logs out. To make them permanent, use certain startup scripts. The startup scripts are executed when the user logs in.

.profile (Bourne shell)

.profile and .kshrc (Korn shell)

.bash_profile (or .bash_login) and .bashrc (Bash)

.login and .cshrc (C shell)

You can also execute them by using a special command (called dot).

$ . .profile

Page 18: UNIX and Shell Programming  (06CS36)

Shrinivas R. Mangalwede, GIT, BelgaumShrinivas R. Mangalwede, G.I.T., Belgaum

The initialization scripts

When logging into an interactive login shell, login will do the authentication, set the environment and start your shell. In the case of bash, the next step is reading the general profile from /etc, if that file exists. bash then looks for ~/.bash_profile, ~/.bash_login and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. If none exists, /etc/bashrc is applied.

When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists.

Page 19: UNIX and Shell Programming  (06CS36)

Shrinivas R. Mangalwede, GIT, BelgaumShrinivas R. Mangalwede, G.I.T., Belgaum

.profile and .bash_profile

Sample entries$ cat .profile# User $HOME/.profile – commands executed at # login time

MAIL=/var/mail/$LOGNAMEPATH=$PATH:$HOME/bin:/usr/ucb:.PS1=‘$ ‘PS2=>TERM=vt100Stty stop ^S intr ^C erase ^?Echo “Today’s date is `date`”

Page 20: UNIX and Shell Programming  (06CS36)

Shrinivas R. Mangalwede, GIT, BelgaumShrinivas R. Mangalwede, G.I.T., Belgaum

The rc fileNormally the profiles are executed only once, upon login. The rc files are designed to be executed every time a separate shell is created.There is no rc file in Bourne, but bash and korn use one. This file is defined by an environment variable BASH_ENV in Bash and ENV in Korn.

export BASH_ENV=$HOME/.bashrcexport ENV=$HOME/.kshrc

Korn automatically executes .kshrc during login if ENV is defined. Bash merely ensures that a sub-shell executes this file. If the login shell also has to execute this file then a separate entry must be added in the profile:

. ~/.bashrc

Page 21: UNIX and Shell Programming  (06CS36)

Shrinivas R. Mangalwede, GIT, BelgaumShrinivas R. Mangalwede, G.I.T., Belgaum

The rc file

You should define command aliases, variable settings, and shell options in your rc file.Some sample entries of an rc file are

alias cp=“cp –i”alias rm=“rm –i”set –o noclobberset –o ignoreeofset –o vi

The rc file will be executed after the profile. However, if the BASH_ENV or ENV variables are not set, the shell executes only the profile.

Page 22: UNIX and Shell Programming  (06CS36)

Shrinivas R. Mangalwede, GIT, Belgaum

To conclude, Environment-related features of the shell

Common Environment Variables Environment variables specific to bask and korn

Aliases Command History In-line Command editing set –o and Tilde substitution The initialization scripts

The Profile file The rc file

Page 23: UNIX and Shell Programming  (06CS36)

Shrinivas R. Mangalwede, GIT, Belgaum

End of Session