Lecture 23 UNIX Shell Programming by pkv14415

VIEWS: 19 PAGES: 45

									Lecture 23: UNIX
Shell Programming

      Homework: Read
     Chapters 15 & 17 of
          Sarwar
         Lecture 23: Outline
 Unix Shell programming:
  – Shell environment.
  – Shell variables.
  – Shell startup.
         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.
   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.
            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.
     Shell Variables (contd.)

 Many are defined in .cshrc and .login
  for the C shell and in .bashrc and
  .bash_profile for bash.
       Shell Variables (contd.)
 Example .bashrc file:
 #Global variables here
 export PATH TERM HOME HISTFILE
 export PATH=$PATH:/usr/afsws/bin:/usr/local/bin:/usr/
 local/sbin:/usr/local/X11/bin:/usr/sbin:/usr/bin:.:~/bin:
 /usr/local/j2se/bin

 #some nice aliases here
 export PS1='[\u@\h \W]$ '
 export PRINTER=ps2
     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.
     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
      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.
      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.
       Shell Variables (contd.)
 Example:

     nitrogen{2} set test = "this is a test"
     nitrogen{3} echo $test
     this is a test
     nitrogen{4} echo test
     test
     nitrogen{5}

 NOTE: The $ is important to access the value in a shell
 variable.
      Shell Variables (contd.)
 Declaring regular variables in bash:
  – varname=varvalue
  – No space between varname and varvalue.
  – Sets the variable varname to have value
    varvalue.
      Shell Variables (contd.)
 Example:

   [axgopala@nitrogen tmp] test="this is a test"
   [axgopala@nitrogen tmp] echo $test
   this is a test
   [axgopala@nitrogen tmp] echo test
   test
   [axgopala@nitrogen tmp]
       Shell Variable (contd.)
 Example with space b/w varname and
  varvalue.


  [axgopala@nitrogen axgopala]$ val = "this is a test"
  bash: val: command not found
  [axgopala@nitrogen axgopala]$ val= "this is a test"
  bash: this is a test: command not found
  [axgopala@nitrogen axgopala]$
      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.
                     Example
   [axgopala@nitrogen axgopala]$ var="this is a test"
   [axgopala@nitrogen axgopala]$ echo $var
   this is a test
   [axgopala@nitrogen axgopala]$ unset var
   [axgopala@nitrogen axgopala]$ echo $var

   [axgopala@nitrogen axgopala]$


NOTE: Once the variable var is unset, there is no value that
is part of the variable.
      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.
     Shell Variables (contd.)
 Example:

    nitrogen{2} setenv test "this is a test"
    nitrogen{3} echo $test
    this is a test
    nitrogen{4}
      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.
  – varname=varvalue
  – export varname
  – Sets the environment variable varname to have
    value varvalue.
                   Example


 [axgopala@nitrogen tmp] test="this is a test"
 [axgopala@nitrogen tmp] export test
 [axgopala@nitrogen tmp] export test=“this is a test”




NOTE: The declaration with the export command can be
combined into one statement as shown.
     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.
                Example

   nitrogen{1} setenv test "this is a test"
   nitrogen{2} echo $test
   this is a test
   nitrogen{3} unsetenv test
   nitrogen{4} echo $test
   test: Undefined variable.
   nitrogen{5}



NOTE: test is undefined as it has been unset.
     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.
   Shell Variables (contd.)

[axgopala@nitrogen axgopala]$ var="this is a test"
[axgopala@nitrogen axgopala]$ export var
[axgopala@nitrogen axgopala]$ echo $var
this is a test
[axgopala@nitrogen axgopala]$ unset var
[axgopala@nitrogen axgopala]$ echo $var

[axgopala@nitrogen axgopala]$
      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.
                      Example

[axgopala@nitrogen axgopala]$ var="testing the variables"
[axgopala@nitrogen axgopala]$ echo $var
testing the variables
[axgopala@nitrogen axgopala]$ bash
erase ^? intr ^C kill ^U
[axgopala@nitrogen axgopala]$ echo $var

[axgopala@nitrogen axgopala]$

NOTE: with the command bash, I invoke a new shell (bash)
and in this shell, my variable var is not accessible anymore.
                    Example
[axgopala@nitrogen axgopala]$ var="testing the variables“
[axgopala@nitrogen axgopala]$ export var
[axgopala@nitrogen axgopala]$ echo $var
testing the variables
[axgopala@nitrogen axgopala]$ bash
erase ^? intr ^C kill ^U
[axgopala@nitrogen axgopala]$ echo $var
testing the variables
[axgopala@nitrogen axgopala]$

NOTE: the environment variable is accessible even when I
invoke another shell using the command bash.
      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.
     Shell Variables (contd.)
 Common shell variables:
  – USER: the user name of the user who is
    logged into the system.
  – HOME: the user‟s home directory.
  – MAIL: the user‟s mail directory.
  – TERM: the kind of terminal the user is using.
  – DISPLAY: where X program windows are
    shown.
     Shell Variables (contd.)
 Common shell variables:
  – HOST: the name of the machine logged on to.
  – REMOTEHOST: the name of the host logged
    in from.
     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.
                   Example
 Single quotes:

[axgopala@nitrogen axgopala]$ echo "Welcome $USER"
Welcome axgopala
[axgopala@nitrogen axgopala]$ echo 'Welcome $USER'
Welcome $USER
[axgopala@nitrogen axgopala]$
                  Example
 Back quotes:

      nitrogen{1} set var = `hostname`
      nitrogen{2} echo $var
      nitrogen.cs.pitt.edu
      nitrogen{3}



 NOTE: The hostname command returns the name of the
 machine, which in this case is nitrogen.cs.pitt.edu
      Shell Variables (contd.)
 What about double quotes “ ” ??
  – No difference if they are used or not.


[axgopala@nitrogen axgopala]$ echo Welcome $USER
Welcome axgopala
[axgopala@nitrogen axgopala]$ echo "Welcome $USER"
Welcome axgopala
[axgopala@nitrogen axgopala]$
                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.
  Example .login file

if ((-e /sitedep/LINUX)) then
      source .login.linux
endif
setenv MAIL /usr/spool/mail/$USER
setenv EXINIT 'set redraw wm=8'
mesg y
set prompt = "* Hello *> "
                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.
               Shell startup
 Only modify the lines that you fully
  understand!
 Can cause very bad errors if not careful.
 E.g:
  – alias ls=„exit‟
  – Each time you type ls to execute, you will exit
    the system 
                 Shell startup
 Adding the line logout to the .login file.
         if ((-e /sitedep/LINUX)) then
               source .login.linux
         endif
         setenv MAIL /usr/spool/mail/$USER
         setenv EXINIT 'set redraw wm=8'
         mesg y
         set prompt = "* Hello *> “
         logout

NOTE: Every time, you log into the system, you get logged
out immediately, thanks to the last line, which is logout 
                Shell startup
 These files can be used for writing very
  useful commands.
  – Setting aliases.
  – Setting environment variables.
  – System setup.
  – Setting prompt.
  – Etc. etc.
       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 
        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.
  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.
                Next Lecture
 Unix Shell programming:
  – Shell scripts.
     • Definition.
     • Uses of shell scripts.
     • Writing shell scripts.

								
To top