UNIX and Shell Programming (06CS36) Unit 3continued… Shrinivas R. Mangalwede Department of Computer Science and Engineering K.L.S.

Download Report

Transcript UNIX and Shell Programming (06CS36) Unit 3continued… Shrinivas R. Mangalwede Department of Computer Science and Engineering K.L.S.

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]
Shrinivas R. Mangalwede, GIT, 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
Shrinivas
Belgaum
ShrinivasR.
R.Mangalwede,
Mangalwede,GIT,
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.
Shrinivas
Belgaum
ShrinivasR.
R.Mangalwede,
Mangalwede,GIT,
G.I.T.,
Belgaum
Environment Variables
Shell 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.
Shrinivas
Belgaum
ShrinivasR.
R.Mangalwede,
Mangalwede,GIT,
G.I.T.,
Belgaum
Common Environment Variables
Variable
Significance
HOME
Home directory – the directory a user is placed
on login
List of directories searched by the shell to
locate a command
PATH
LOGNAME
USER
MAIL
Login name of user
Login name of user
Absolute pathname of user’s mailbox file
MAILCHECK Mail checking interval for incoming mail
TERM
Type of terminal
Shrinivas R. Mangalwede, GIT, Belgaum
Common Environment Variables
Variable
PWD
CDPATH
PS1
PS2
SHELL
Significance
Absolute pathname of current directory
(Bash and Korn only)
List of directories searched by cd when used
with a non-absolute pathname
Primary prompt string
Secondary prompt string
User’s login shell
Shrinivas R. Mangalwede, GIT, Belgaum
Some Examples
To 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
Shrinivas
Belgaum
ShrinivasR.
R.Mangalwede,
Mangalwede,GIT,
G.I.T.,
Belgaum
Some Examples
The 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.
Shrinivas
Belgaum
ShrinivasR.
R.Mangalwede,
Mangalwede,GIT,
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> “
saturn> _
// Host name of the machine
Shrinivas
Belgaum
ShrinivasR.
R.Mangalwede,
Mangalwede,GIT,
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
Shrinivas
Belgaum
ShrinivasR.
R.Mangalwede,
Mangalwede,GIT,
G.I.T.,
Belgaum
Aliases (bash and korn)
To display an alias definition use alias with the name,
$ alias cp
cp=“cp –i”
You can list all aliases by using alias without arguments and
unset an alias with the unalias statement.
$ unalias cp
Shrinivas
Belgaum
ShrinivasR.
R.Mangalwede,
Mangalwede,GIT,
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 command
By default, bash lists the complete command history while korn
lists the last 16 most recently used commands.
$ history 5
$ history -5
Bash
Korn
History is stored in $HOME/.bash_history or $HOME/.sh_history
Shrinivas
Belgaum
ShrinivasR.
R.Mangalwede,
Mangalwede,GIT,
G.I.T.,
Belgaum
Accessing previous commands
By event numbers (! In bash and r in korn)
$ !38
$ !38:p
$ !!
$ !-2
The command with event number 38 is displayed
and executed (Use r 38 in korn)
The command is displayed. You can edit and
execute it
Repeats previous command (Use r in korn)
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)
Shrinivas
Belgaum
ShrinivasR.
R.Mangalwede,
Mangalwede,GIT,
G.I.T.,
Belgaum
Substitution in previous commands
If 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 $_
Shrinivas
Belgaum
ShrinivasR.
R.Mangalwede,
Mangalwede,GIT,
G.I.T.,
Belgaum
The History variables
The 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.
Shrinivas
Belgaum
ShrinivasR.
R.Mangalwede,
Mangalwede,GIT,
G.I.T.,
Belgaum
Miscellaneous Features
Using set –o
set –o noclobber: Prevents overwriting of an existing file
with > symbol. To override this protection, use | after the >
ls –l >| file_list
set –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 Substitution
Tilde (~) acts as a shorthand representation of the home
directory. A configuration file like .profile can be referred to both
as $HOME/.profile and ~/.profile.
Shrinivas
Belgaum
ShrinivasR.
R.Mangalwede,
Mangalwede,GIT,
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
Shrinivas
Belgaum
ShrinivasR.
R.Mangalwede,
Mangalwede,GIT,
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.
Shrinivas
Belgaum
ShrinivasR.
R.Mangalwede,
Mangalwede,GIT,
G.I.T.,
Belgaum
.profile and .bash_profile
Sample entries
$ cat .profile
# User $HOME/.profile – commands executed at
# login time
MAIL=/var/mail/$LOGNAME
PATH=$PATH:$HOME/bin:/usr/ucb:.
PS1=‘$ ‘
PS2=>
TERM=vt100
Stty stop ^S intr ^C erase ^?
Echo “Today’s date is `date`”
Shrinivas
Belgaum
ShrinivasR.
R.Mangalwede,
Mangalwede,GIT,
G.I.T.,
Belgaum
The rc file
Normally 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/.bashrc
export 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
Shrinivas
Belgaum
ShrinivasR.
R.Mangalwede,
Mangalwede,GIT,
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 noclobber
set –o ignoreeof
set –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.
Shrinivas
Belgaum
ShrinivasR.
R.Mangalwede,
Mangalwede,GIT,
G.I.T.,
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
Shrinivas R. Mangalwede, GIT, Belgaum
End of Session
Shrinivas R. Mangalwede, GIT, Belgaum