Dr. Mark Humphrys

School of Computing. Dublin City University.

Home      Blog      Teaching      Research      Contact

Search:

CA249      CA318      CA425      CA651

w2mind.computing.dcu.ie      w2mind.org


Further Shell



How to pass information to a program

In general, how to pass information to a program:

  1. Set environment variables. All programs can read these.

  2. Command-line arguments.
    Program icon - Properties.

  3. Pipe to the program, where the program expects to read from standard input.

  4. Configuration files.
    See   .* files in home directory on UNIX.
    e.g. See:
    • $HOME/.mozilla/firefox/id/prefs.js
    Equivalents exist on Windows. e.g. See:
    • C:\Program Files\Mozilla Firefox\defaults\pref\firefox.js
    Edit - Preferences
    Tools - Options

  5. Talk to the program while it is running. How to do this depends on the OS.




What shells are installed?

Linux default on login at DCU is Bourne-again shell bash, but there are others installed.

See what is installed:

ls -l /bin/*sh
On DCU Linux, something like:
-rwxr-xr-x 1 root root 663704 2008-06-06 21:04 /bin/bash
lrwxrwxrwx 1 root root      4 2008-09-11 12:47 /bin/csh -> tcsh
lrwxrwxrwx 1 root root     16 2008-09-11 12:48 /bin/ksh -> /lib/ast/bin/ksh
-rwxr-xr-x 1 root root 705552 2008-06-06 21:44 /bin/sash
lrwxrwxrwx 1 root root      4 2008-09-11 12:42 /bin/sh -> bash
-rwxr-xr-x 1 root root 334380 2008-06-06 20:48 /bin/tcsh
-rwxr-xr-x 1 root root 569692 2008-06-07 09:18 /bin/zsh
On DCU Solaris, something like:
-r-xr-xr-x   1 root     bin       578964 Mar  2  2002 /bin/bash
-r-xr-xr-x   2 root     bin       159332 Apr  6  2002 /bin/csh
-r-xr-xr-x  17 root     bin          134 Apr  6  2002 /bin/hash
-r-xr-xr-x   4 root     root       95488 Apr  7  2002 /bin/jsh
-r-xr-xr-x   3 root     bin       201044 Mar 14  2003 /bin/ksh
-r-xr-xr-x   2 root     bin       159332 Apr  6  2002 /bin/pfcsh
-r-xr-xr-x   3 root     bin       201044 Mar 14  2003 /bin/pfksh
-r-xr-xr-x   4 root     root       95488 Apr  7  2002 /bin/pfsh
lrwxrwxrwx   1 root     root           5 Nov  7  2002 /bin/remsh -> ./rsh
-r-xr-xr-x   3 root     bin       201044 Mar 14  2003 /bin/rksh
-r-sr-xr-x   1 root     bin         9176 Apr  6  2002 /bin/rsh
-r-xr-xr-x   4 root     root       95488 Apr  7  2002 /bin/sh
-r-xr-xr-x   1 root     bin       737948 Apr  7  2002 /bin/ssh
-r-xr-xr-x   1 root     bin       358848 Mar  2  2002 /bin/tcsh
-r-xr-xr-x   1 root     bin       474736 Mar  2  2002 /bin/zsh

To start using one as the command-line, just type its name. e.g.

ksh
"exit" to return.



The shebang line

The first line of a shell script should state which shell it is to run in, using syntax like:
#!/bin/shellname
This is called a "shebang" line.

For example:


Can have endless number of interpreters of source text. Can invent your own.



If no shebang line

It is usually possible to omit the shebang line, and your system will try (for example) to run the script in the simple Bourne shell sh.

But you cannot depend on this - exactly what happens depends on each system.
Your shell scripts may not be portable to different UNIX-family systems.

So it is good practice to define the shell in the first line.



Exercise

Get the test programs above to work, and see your environment variables:

  1. shtest
    • on DCU Linux, runs in bash, because sh is bash (see above)
    • Some environment variables:
      HOME=/users/gdf1/mhtest09
      OSTYPE=linux
      PATH=/users/gdf1/mhtest09/bin:/usr/local/bin:/usr/bin:/bin:....
      PWD=/users/gdf1/mhtest09/bin
      SHELL=/bin/bash
      USER=mhtest09
      USERNAME=mhtest09
      http_proxy=http://wwwproxy.computing.dcu.ie:8000
      no_proxy='localhost, 127.0.0.1, dcu.ie'
      
    • To change them:
      PATH=$PATH:$HOME/bin:.

  2. cshtest
    • on DCU Linux, runs in tcsh, because csh is tcsh (see above)
    • Some environment variables:
      cwd     /users/gdf1/mhtest09
      home    /users/gdf1/mhtest09
      path    (/users/gdf1/mhtest09/bin /usr/local/bin /usr/bin /bin ....)
      shell   /bin/tcsh
      user    mhtest09
      
    • To change them:
      set path = ( . $home/bin /bin /usr/local/bin .... )


Question

In shtest, why not:
if test $1 = ""





Environment variables in parent and child processes

  1. Script cannot change parent environment:
    cd (change directory) in Shell script only has effect within that script.
    In general, environment variables changed within Shell script (such as "cwd" or "PWD" - current working directory) are only changed for the duration of that script.

  2. Use alias to change current process's environment variables:
    Instead of a Shell script, use an alias, which is a command-line text substitution.

  3. Script's variables are not usable by children (scripts called by that script) by default:
    You need to "export" them to make them available to children.
    Example: prog1:

    x=3
    export x
    echo "prog1 [$x]"
    prog2
    

    calls prog2:

    echo "prog2 [$x]"
    

    Try it with and without export.
    export will make variable x available to a script called by this script, and any scripts called by that script, etc., without any further exporting needed. Only a single export statement is needed (in the top parent script).

  4. Functions in the same script can access variables:
    Note that environment variables are by default available to functions within the same Shell script without exporting.



Caching the PATH (C shell)

It is a bit of an overhead to search all the directories in the PATH on disk every time you type a command.
So some shells make a list of executable files in these directories (with the exception of ".") once, at login, or at the start of running a script, and then caches that list in memory for future use.

This can cause some problems. e.g. You add a new program to your $home/bin directory, which is in the PATH. You then type the name of the program at the command-line and it says "Command not found". The solution is you need to re-build that cache.


How to re-build the cache of the PATH

  source .FILE
where .FILE (insert correct file name) is the file where the path is defined.

If you do this a lot, you might like to put the following alias in .cshrc:

  alias redo      source $home/.FILE
or .bashrc:
  alias redo="source $HOME/.FILE"
And then, every time your PATH cache seems to be out of date, you just type:
  redo


CDPATH (C shell)

The "cdpath" variable is also very useful:

set cdpath = ( $home $home/public_html )
It is a quick way of jumping around the disk. Wherever you are on the disk, you can jump direct to a subdirectory off your home directory or off your web directory by just typing "cd (subdirectory)".

Using tools like this, jumping around the disk and performing tasks on the command-line can actually be quicker than doing it through the File Manager.




Characters



echo '\0nn'       echo char by octal numeric code (on csh)
echo '\0nnn'       
echo -e (string)  on bash

echo "\0115"      echo "M"
echo "\0275"      echo "½"

works on csh on DCU Linux
works on sh on DCU Solaris

ASCII chars
ISO/IEC 8859
To build list of chars




Feeds      HumphrysFamilyTree.com

Bookmark and Share           On Internet since 1987.