Title: Lecture 23: UNIX Shell Programming
1Lecture 23 UNIX Shell Programming
- Homework Read Chapters 15 17 of Sarwar
2Lecture 23 Outline
- Unix Shell programming
- Shell environment.
- Shell variables.
- Shell startup.
3Shell Environment
- Shell environment
- Consists of a set of variables with values.
- These values are important information for the
shell and the programs that run from the shell. - You can define new variables and change the
values of the variables.
4Shell Environment (contd.)
- Example PATH determines where the shell looks
for the file corresponding to your command. - Example SHELL indicates what kind of shell you
are using.
5Shell Variables
- How do we use the values in the shell variables
????? - Put a in front of their names.
- e.g echo HOME
- Prints the value that is stored in the variable
HOME.
6Shell Variables (contd.)
- Many are defined in .cshrc and .login for the C
shell and in .bashrc and .bash_profile for bash.
7Shell Variables (contd.)
Global variables here export PATH TERM HOME
HISTFILE export PATHPATH/usr/afsws/bin/usr/loc
al/bin/usr/ local/sbin/usr/local/X11/bin/usr/sb
in/usr/bin./bin /usr/local/j2se/bin some
nice aliases here export PS1'\u_at_\h \W
' export PRINTERps2
8Shell Variables (contd.)
- Two kinds of shell variables
- Environment variables
- Available in the current shell and the programs
invoked from the shell - Regular shell variables
- Not available in programs invoked from this shell.
9Shell Variables (contd.)
- Comments on examples
- Examples are shown for both the C shell as well
as for bash. - To know which shell you are working in currently,
use the ps command. - Once you login, you are working in your login
shell, which can be found out by the command - echo SHELL
10Shell Variables (contd.)
- To explicitly invoke a particular shell, type the
name of the shell on the command line - E.g
- csh ? invokes the C shell.
- bash ? invokes bash.
11Shell Variables (contd.)
- Declaring regular variables in the C shell
- set varname varvalue
- Space between varname and varvalue is optional.
- Sets the variable varname to have value varvalue.
12Shell Variables (contd.)
nitrogen2 set test "this is a
test" nitrogen3 echo test this is a
test nitrogen4 echo test test nitrogen5
NOTE The is important to access the value in a
shell variable.
13Shell Variables (contd.)
- Declaring regular variables in bash
- varnamevarvalue
- No space between varname and varvalue.
- Sets the variable varname to have value varvalue.
14Shell Variables (contd.)
axgopala_at_nitrogen tmp test"this is a
test" axgopala_at_nitrogen tmp echo test this is
a test axgopala_at_nitrogen tmp echo
test test axgopala_at_nitrogen tmp
15Shell Variable (contd.)
- Example with space b/w varname and varvalue.
axgopala_at_nitrogen axgopala val "this is a
test" bash val command not found axgopala_at_nitro
gen axgopala val "this is a test" bash this
is a test command not found axgopala_at_nitrogen
axgopala
16Shell Variable (contd.)
- Remove declaration of regular variables
- Use the unset command
- Works for both the C shell and bash ?
- unset varname
- Once variable is unset, the value that previously
was assigned to that variable does not exist
anymore.
17Example
axgopala_at_nitrogen axgopala var"this is a
test" axgopala_at_nitrogen axgopala echo
var this is a test axgopala_at_nitrogen axgopala
unset var axgopala_at_nitrogen axgopala echo
var axgopala_at_nitrogen axgopala
NOTE Once the variable var is unset, there is no
value that is part of the variable.
18Shell Variables (contd.)
- Declaring environment variables in the C shell
- setenv varname varvalue
- Sets the environment variable varname to have
value varvalue. - Notice that there is no out here.
- Space b/w varname and varvalue is necessary.
19Shell Variables (contd.)
nitrogen2 setenv test "this is a
test" nitrogen3 echo test this is a
test nitrogen4
20Shell Variables (contd.)
- Declaring environment variables in bash
- Using the export command.
- To change a regular variable to an environment
variable, we need to export them. - varnamevarvalue
- export varname
- Sets the environment variable varname to have
value varvalue.
21Example
axgopala_at_nitrogen tmp test"this is a
test" axgopala_at_nitrogen tmp export
test axgopala_at_nitrogen tmp export testthis is
a test
NOTE The declaration with the export command can
be combined into one statement as shown.
22Shell Variables (contd.)
- Remove declaration of environment variables in
the C shell - Use the unsetenv command.
- unsetenv varname
- Once variable is unset, the value that previously
was assigned to that variable does not exist
anymore.
23Example
nitrogen1 setenv test "this is a
test" nitrogen2 echo test this is a
test nitrogen3 unsetenv test nitrogen4 echo
test test Undefined variable. nitrogen5
NOTE test is undefined as it has been unset.
24Shell Variables (contd.)
- Remove declaration of environment variables in
bash - Use the unset command.
- unset varname
- Once variable is unset, the value that previously
was assigned to that variable does not exist
anymore.
25Shell Variables (contd.)
axgopala_at_nitrogen axgopala var"this is a
test" axgopala_at_nitrogen axgopala export
var axgopala_at_nitrogen axgopala echo var this
is a test axgopala_at_nitrogen axgopala unset
var axgopala_at_nitrogen axgopala echo
var axgopala_at_nitrogen axgopala
26Shell Variables (contd.)
- We can use regular variables, just like
environment variables, so why have environment
variables ??? - Regular variables are only available to the
current shell. - Environment variables are accessible across
shells and to all running programs. - What does this mean ????? example follows.
27Example
axgopala_at_nitrogen axgopala var"testing the
variables" axgopala_at_nitrogen axgopala echo
var testing the variables axgopala_at_nitrogen
axgopala bash erase ? intr C kill
U axgopala_at_nitrogen axgopala echo
var axgopala_at_nitrogen axgopala
NOTE with the command bash, I invoke a new shell
(bash) and in this shell, my variable var is not
accessible anymore.
28Example
axgopala_at_nitrogen axgopala var"testing the
variables axgopala_at_nitrogen axgopala export
var axgopala_at_nitrogen axgopala echo
var testing the variables axgopala_at_nitrogen
axgopala bash erase ? intr C kill
U axgopala_at_nitrogen axgopala echo
var testing the variables axgopala_at_nitrogen
axgopala
NOTE the environment variable is accessible even
when I invoke another shell using the command
bash.
29Shell Variables (contd.)
- Common shell variables
- SHELL the name of the login shell of the user.
- PATH the list of directories searched to find
executables to execute. - MANPATH where man looks for man pages.
- LD_LIBRARY_PATH where libraries for executables
exist.
30Shell Variables (contd.)
- Common shell variables
- USER the user name of the user who is logged
into the system. - HOME the users home directory.
- MAIL the users mail directory.
- TERM the kind of terminal the user is using.
- DISPLAY where X program windows are shown.
31Shell Variables (contd.)
- Common shell variables
- HOST the name of the machine logged on to.
- REMOTEHOST the name of the host logged in from.
32Shell Variables (contd.)
- Quotes in Unix have a special meaning
- Single quotes
- Stops shell variable expansion.
- Example on the next slide.
- Back quotes
- Replace the quotes with the result of the
execution of the command. - Example two sides later.
33Example
axgopala_at_nitrogen axgopala echo "Welcome
USER" Welcome axgopala axgopala_at_nitrogen
axgopala echo 'Welcome USER' Welcome
USER axgopala_at_nitrogen axgopala
34Example
nitrogen1 set var hostname nitrogen2 echo
var nitrogen.cs.pitt.edu nitrogen3
NOTE The hostname command returns the name of
the machine, which in this case is
nitrogen.cs.pitt.edu
35Shell Variables (contd.)
- What about double quotes ??
- No difference if they are used or not.
axgopala_at_nitrogen axgopala echo Welcome
USER Welcome axgopala axgopala_at_nitrogen
axgopala echo "Welcome USER" Welcome
axgopala axgopala_at_nitrogen axgopala
36Shell startup
- When csh and tcsh are executed, they read and run
certain configuration files - .login run once when you log in
- Contains one time initialization, like TERM, HOME
etc. - .cshrc run each time another csh/tcsh process is
invoked. - Sets lots of variables, like PATH, HISTORY etc.
- Aliases are normally written in this file.
37Example .login file
if ((-e /sitedep/LINUX)) then source
.login.linux endif setenv MAIL /usr/spool/mail/US
ER setenv EXINIT 'set redraw wm8' mesg y set
prompt " Hello gt "
38Shell startup
- When bash is executed, it reads and runs certain
configuration files - .profile/.bash_profile runs when you log in.
- Contains one time initialization, like TERM, HOME
etc. - .bashrc run each time another bash process is
invoked. - Sets lots of variables, like PATH, HISTORY etc.
39Shell startup
- Only modify the lines that you fully understand!
- Can cause very bad errors if not careful.
- E.g
- alias lsexit
- Each time you type ls to execute, you will exit
the system ?
40Shell startup
- Adding the line logout to the .login file.
if ((-e /sitedep/LINUX)) then source
.login.linux endif setenv MAIL /usr/spool/mail/US
ER setenv EXINIT 'set redraw wm8' mesg y set
prompt " Hello gt logout
NOTE Every time, you log into the system, you
get logged out immediately, thanks to the last
line, which is logout ?
41Shell startup
- These files can be used for writing very useful
commands. - Setting aliases.
- Setting environment variables.
- System setup.
- Setting prompt.
- Etc. etc.
42Command completion
- In tcsh and bash, you can let the shell complete
a long command name by - Typing a prefix of the command.
- Hitting the TAB key.
- The shell will fill in the rest for you, if
possible ?
43Filename completion
- tcsh and bash also complete file names
- Type first part of file name.
- Hit the TAB key.
- The shell will complete the rest, if possible.
44Differences b/w filename and command completion
- Difference
- Enter first word and then TAB ? command
completion. - Enter other words and then TAB ? file name
completion.
45Next Lecture
- Unix Shell programming
- Shell scripts.
- Definition.
- Uses of shell scripts.
- Writing shell scripts.