CSc 352: Lecture-23

C Shell

Note: All the programs mentioned in this lecture are in:

/home/cs352/SUMMER02/lecture23progs/

- C Shell Variables:
  - Hold strings or sets of strings
  - To save long strings, and to build new commands.
  - set var = string  
    set var = (str1 str2 ... strn)    (to set a list)
    to further access the variable use, $var
    unset var                         (to unset the variable)
    $?var                             (to check if var is set)
    $#var                             (#of elems in the list)
    set                               (to list all var. settings)

    Ex: 
    set home352 = /home/cs352/SUMMER02/
    ls -l $home352

  - Predefined variables:
    path     When executing a command shell searches for the
    directories listed in path(Searches from left to right.) 

    cdpath   Same, but only sed for changing directories
    Ex: set cdpath = (/home/cs352/ /cs/www/classes/cs352)
	cd SUMMER02
	cd summer02

    home       Abs. pathname of the home dir.
    
    history    How many commands to remember.

    prompt     What the prompt will be.
    
    ignoreeof  If set Ctrl-D will not log you out.
    
    noclobber  If set no overwriting to an existing file when
               redirection

    cwd        Current working directory



- Environment variables:
  - Similar to variables, but UNIX can pass these to the programs
    executed from the shell(scripts, some commands, appl. programs etc.)
  - set VAR string         (to set the env. var)
    $VAR                   (to access it)
    env                    (to see all the env. variables)
    unsetenv VAR           (to remove VAR)

  - Predefined Environment Variables:
    HOME     abs. pathname of home dir.  

    PATH     same as path variable, but diff. format. 
    Note: The var. path is a wordlist, but PATH is a string.
    
    USER     user name

- Command Substitution:
  - Putting a command inside ` character 
    replaces a command with its output.
    
    Ex:
    set wsh = `cat /etc/passwd | grep $user`
    echo $wsh
    set files = `ls $home`
    echo $files[1]
    echo $#files

- How to Escape Special characters?
  - A single character can be escaped by putting \ before it.
  
  Ex:
  echo $files[1]
  echo \$files\[1\]
  
  - Escaping a whole string either by using
    double quotes: escapes most characters but allows var subst.
    and command subst.
    single quotes: same, but doesn't allow var or command subst.

  Ex:
  echo "***-> $files[1]"
  echo '***-> $files[1]'

- Aliasing:  
  - We can give an alias standing for a command or a set of commands.
    alias alias_name definition     (creates an alias)
    alias                           (lists all aliases) 
    unalias alias_name              (removes alias_name)

    see dotlogin dottcshrc file

    Diff. bw .login file and .cshrc: The latter is used by every 
    csh upon invocation, but .login is only read by the login csh.
    (So can put env. variables and top-level aliases in .login, but
    the rest of the stuff, like variables that will be useful in 
    shell procedures should be in .cshrc)

- Job Control:
  - A job may consist of one proces or several processes
  Ex:
    ls -l        (one process)
    ls -l|more   (two processes)
  - Background jobs
    Created with the &

  - The command ps shows all the processes that are running
    with the process id, terminal, time, and name of the process.

  - The command jobs shows the status of the background jobs.  

  - kill pid       (kills the process with pid)
    kill %1        (kills the job with job id 1)
    
  - Job control:
    fg %2          (run job 2 in the foreground)
    bg %2          (run job 2 in the background)
    Ctrl-Z         (suspend the foreground job)            
    stop %2        (suspend the background job 2)

- How to write simple shell scripts?
  see /scripts