Lecture 23: UNIX Shell Programming - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

Lecture 23: UNIX Shell Programming

Description:

NOTE: Once the variable var is unset, there is no value that. is part of the variable. ... and in this shell, my variable var is not accessible anymore. Example ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 46
Provided by: axgo
Category:

less

Transcript and Presenter's Notes

Title: Lecture 23: UNIX Shell Programming


1
Lecture 23 UNIX Shell Programming
  • Homework Read Chapters 15 17 of Sarwar

2
Lecture 23 Outline
  • Unix Shell programming
  • Shell environment.
  • Shell variables.
  • Shell startup.

3
Shell 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.

4
Shell 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.

5
Shell 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.

6
Shell Variables (contd.)
  • Many are defined in .cshrc and .login for the C
    shell and in .bashrc and .bash_profile for bash.

7
Shell Variables (contd.)
  • Example .bashrc file

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
8
Shell 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.

9
Shell 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

10
Shell 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.

11
Shell 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.

12
Shell Variables (contd.)
  • Example

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.
13
Shell Variables (contd.)
  • Declaring regular variables in bash
  • varnamevarvalue
  • No space between varname and varvalue.
  • Sets the variable varname to have value varvalue.

14
Shell Variables (contd.)
  • Example

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
15
Shell 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
16
Shell 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.

17
Example
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.
18
Shell 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.

19
Shell Variables (contd.)
  • Example

nitrogen2 setenv test "this is a
test" nitrogen3 echo test this is a
test nitrogen4
20
Shell 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.

21
Example
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.
22
Shell 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.

23
Example
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.
24
Shell 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.

25
Shell 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
26
Shell 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.

27
Example
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.
28
Example
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.
29
Shell 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.

30
Shell 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.

31
Shell Variables (contd.)
  • Common shell variables
  • HOST the name of the machine logged on to.
  • REMOTEHOST the name of the host logged in from.

32
Shell 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.

33
Example
  • Single quotes

axgopala_at_nitrogen axgopala echo "Welcome
USER" Welcome axgopala axgopala_at_nitrogen
axgopala echo 'Welcome USER' Welcome
USER axgopala_at_nitrogen axgopala
34
Example
  • Back quotes

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
35
Shell 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
36
Shell 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.

37
Example .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 "
38
Shell 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.

39
Shell 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 ?

40
Shell 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 ?
41
Shell startup
  • These files can be used for writing very useful
    commands.
  • Setting aliases.
  • Setting environment variables.
  • System setup.
  • Setting prompt.
  • Etc. etc.

42
Command 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 ?

43
Filename 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.

44
Differences b/w filename and command completion
  • Difference
  • Enter first word and then TAB ? command
    completion.
  • Enter other words and then TAB ? file name
    completion.

45
Next Lecture
  • Unix Shell programming
  • Shell scripts.
  • Definition.
  • Uses of shell scripts.
  • Writing shell scripts.
Write a Comment
User Comments (0)
About PowerShow.com