\documentclass{article}
\usepackage{amstext}
\def\reg{\setbox0\hbox{$\mathchar"20D$}%
\hbox to 0pt{\hbox to \wd0{\hfill\,\rm R\hfill}\hss}%
$\mathchar"20D$}\usepackage[latin1]{inputenc}
\title {Lab 2: Shell, Environment Variables, Useful C-Shell Commands}
\date{}
\begin{document}

\maketitle

\section {Introduction}
\begin{itemize}

\item main topics:
\begin{itemize}


        \item what is shell, different shells,
        \item bourne shell, c-shell, 
        \item path, environment variables, 
        \item other useful commands.
\end{itemize}

\item Useful links:

A good reference to UNIX providing basic facts from history, 
introduction to its structure, shells and commands is at  
\\
http://wks.uts.ohio-state.edu/unix\_course/
\\

Other pages discussing different shells are:
\begin{itemize}

\item  shell in general, bourne shell:
\\
http://www.cc.vt.edu/cc/us/docs/unix/shells.html
\\
http://www.ling.helsinki.fi/users/reriksso/unix/shell.html
\\
\item korn shell
\\
http://www.kornshell.com/info/
\item  why not cshell:
\\
http://www.cs.ruu.nl/wais/html/na-dir/unix-faq/shell/csh-whynot.html
\\
\end{itemize}

\end{itemize}



\section {UNIX, Kernel, Shell.}

In UNIX, communication with the operating system is not direct. Instead, UNIX commands are interpreted by a program called shell. Shell translates the commands into actions that are taken by the operating system, the kernel. Kernel interacts directly with the hardware and provides services to the user programs.

As mentioned above, 
{\bf Shell  is  a program that allows the system to understand user's commands}
        ( a command interpreter)
Shell is analogous to command.com in DOS.
\subsection {
Purpose of Shell}
\begin{itemize}

\item  {\bf interactive use:} system waits for the command 
at  the UNIX prompt and interprets it.
\item  {\bf customization of your UNIX session:}
shell defines variables controlling the behavior of user's UNIX session.
\item  {\bf programming:} shell allows to create programs called shell scripts. 
\end{itemize}


\subsection { Different Shells: which, when, why}
\begin{itemize}

\item  {\em sh}: the Bourne shell 
\begin{itemize}

\item  developed by Steven Bourne at AT\&T Bell Laboratories
\item  most compact but also simplest shell
\item  very good features for controlling input and output, 
but not well suited for an interactive user
\item  good choice for writing shell scripts
\item  default prompt is \$
      
\end{itemize}

\item  {\em csh}: the C shell
\begin{itemize}

\item  developed by Bill Joy at the University of California
\item  uses C type syntax 
\item  input/output implementation is worse than in Bourne shell which makes it not very good for programming.
\item  has a job control allowing to reattach a job running in the background to the foreground. 
\item  provides a history feature allowing modification and repetition of previously executed commands
\item  is suitable for interactive use.
\item  the default prompt is \%.
\end{itemize}


\item  {\em ksh}: the Korn shell
\begin{itemize}

\item  designed and developed by David G. Korn at AT\&T Bell Laboratories
\item  contains the best features of sh and csh, plus many new features of its own
\end {itemize}

\item  {\em bash}: the "bourne again" shell
\begin{itemize}

\item originally written by Brian Fox of the Free Software Foundation,  current developer and maintainer is Chet Ramey of Case Western Reserve University
\item  contains a number of enhancements over bourne shell, both for interactive use and shell programming

\end {itemize}

\item  {\em tcsh}: the extended version of c shell
\begin{itemize}

\item  is suitable for interactive use
\item has the same features as c shell plus several additional user 
friendly features: use of arrows to repeat previous commands, 
the first letters can be completed into a unique file name by hitting the tabulator.
\end {itemize}
\end {itemize}

By default, U. of Waterloo user UNIX account is set up with c-shell, 
though other shells are also available. For interactive use you can choose any 
shell you wish. However, our labs will refer to c-shell ({\em csh})
and tc-shell ({\em tcsh}) respectively for interactive use. 
For programming purpose we will always use bourne shell ({\em sh}). 
     
\subsection{Changing your shell}

To check what shell you are using right now, type:
\\

\hspace*{1 cm} {\em grep \$USER /etc/passwd}
\\

To change your shell, type\\

\hspace*{1 cm} {\em chsh}
\\

{\em chsh}  shows first the current shell and then prompts for the new shell. 
Less experienced users may choose {\em tcsh} instead. The shell will be changed 
permanently in few hours after typing the {\em tcsh}. 
For a temporary shell change type the name of the shell on the command prompt.

For example, type  \\

\hspace*{1 cm} {\em sh}\\

to work in bourne shell immediately. The {\em sh} prompt,\$, 
appears and you can enter {\em sh} commands, say,\\

\hspace*{1 cm} {\em      ls}\\
\hspace*{1 cm} {\em cd cs241}\\
\hspace*{1 cm} {\em mkdir lab2}\\
\hspace*{1 cm} {\em      pwd}\\

Once finished, you may end the work and return to the previous shell using {\em CTRL-D}.

Notice that most UNIX commands introduced till now are valid in all shells.
From now on, when discussing interactive use, we focus on c-shell or tc-shell only. 
The valid syntax for korn shell or bourne shell can be found in UNIX in a nutshell book. 

Before discussing internal and external commands, 
we need to understand the structure of the filesystem in UNIX.


\subsection {The Tree Structure of the Filesystem and a  Home Directory} 

Files are organized into directories. 
A directory is a special kind of file that list other files. 
A directory can contain any number of files and other directories. 
The directory at the top is called the "root" and has a special name "/"  ("slash") 
\\
(picture)
\\


Log in leads in the directory created for you by the system administrator. 
This directory is called your home directory. For example cs241 home directory is:\\
\hspace*{1 cm} {\em /u/cs241}.\\
/ is the root and 'u' stores home directories for all users.
The current directory can be changed to your home directory by typing \\
\hspace*{1 cm} {\em cd}
\\with no pathname.

\subsubsection {Current Directory, Absolute and Relative Pathname} 

Pathnames locate files in the UNIX filesystem. 
By default, UNIX looks for files or directories starting from the current directory. 
For example, if the current directory is /u/youruserid/cs241 then the UNIX command \\

\hspace*{1 cm} a) \hspace*{1 cm} {\em cd a1} \\

will switch into /u/youruserid/cs241/a1, if such a directory exists. 
Otherwise it will print an error message. 
If /u/youruserid/cs241 is the current directory then typing\\

\hspace*{1 cm}  b) \hspace*{1 cm} {\em     more a1/q1.txt}\\ 

you ask UNIX to locate the file q1.txt within the directory  /u/youruserid/cs241/a1.
\\
You can also type:\\

\hspace*{1 cm} c) \hspace*{1 cm} {\em        more /u/youruserid/cs241/a1/q1.txt}  
\\

to view the same file.

Cases a) and b) use relative pathnames, case c) an absolute pathname.

Several abbreviations simplifying the work with pathnames exist:\\

\hspace*{1 cm} 	.  refers to the current directory\\
\hspace*{1 cm}  .. refers to the parent directory\\

For example, if /u/myuserid/cs241/a1 is the working directory and q1.txt is 
the file in a1 directory, then\\ 

\hspace*{1 cm} {\em more q1.txt}
\\

has the same effect as 
\\

\hspace*{1 cm} {\em more ./q1.txt}\\

If the working directory is the same as above but you want to check the file q0.txt 
in directory a0 without changing the current directory, type\\

\hspace*{1 cm} {\em more ../a0/q0.txt} .
\\

In C shell, $\tilde{} $ is a shortcut to the home directory.
For example, 
$\tilde{} $/cs241/lab2 is equivalent to the absolute pathname {\em /u/myuserid/cs241/lab2}.
\,{\em $\tilde{} $username} switches to the home directory corresponding to username. 

It this sense, UNIX allows practically anything and 
it the user's responsibility to set up permissions on his/her files in a way 
that the above features cannot be misused.
\\

In summary,
\begin{itemize}
\item  the absolute pathname always starts with / 
\item  the C shell turns \, $\tilde{} $ \, into an absolute pathname starting 
at the home directory or at \,
{\em $\tilde{} $username} directory
\item  if the pathname does not begin with / or \, $\tilde{}$ \, (C shell) then the pathname 
is relative to the current directory. 
\end{itemize}    
  
As we will see shortly, it is very important to understand the difference between absolute and relative pathname.

\subsection {Internal and External Commands}

Internal commands, such as {\em cd}, are build into the shell. 
The shell interprets the cd command and changes your current directory accordingly. 
External commands are just external programs. 
Example of an external command is {\em ls}, which is stored in the file /bin/ls.


A build-in command or a command containing an absolute pathname starting with /, 
is executed by shell directly. Say, /bin/ls executes the program called {\em ls} located in 
the directory bin. If the command is neither build-in, 
nor specified by an absolute pathname, shell looks in its search path first.   
\subsubsection{PATH}
The search path is set up in the environment variable {\em  PATH}. 
To see the content of {\em {\em PATH}}, i.e. all pathnames available to shell, type 
\hspace*{1 cm} {\em echo \${\em PATH}}\\

or 

\hspace*{1 cm} {\em printenv {\em PATH}}\\

After typing the above command, the search path will look like:\\
\hspace*{1 cm} {\em /bin:/usr/bin:/.software/local/.admin/bins/bin: usr/openwin/bin}\\  
where ':' distinguishes between different pathnames.
As mentioned before, the shell seeks the program name in directories set in {\em PATH}. 
The {\em PATH} is searched sequentially.
For example, if {\em PATH}=/bin:/usr/bin:/... shell starts to look for the program in /bin, then /usr/bin etc. At the moment the program is found, it is executed.
Presence of the /bin directory in the {\em PATH} is essential, for all external 
UNIX commands are located there. 
By default, the current directory is not in the {\em PATH}. 
It can be added to the {\em PATH}, but it is not recommended. Why?
\\
\subsubsection {Changing  PATH}
Very often it will be necessary to add new directories into {\em PATH}.
The command for setting or changing the environment variable is setenv.
To add, for example,  user's cs241/a1 directory to the {\em PATH},  type:
setenv {\em PATH} \$HOME/cs241/a1:\${\em PATH}
This will add cs241/a1 directory to the beginning, which means that 
the cs241/a1 directory will be searched by shell first. 
Similarly one can add a directory to the end of the list. 
Note, that {\em HOME} is another environment variable, which defines user's home directory. 
 



\section {A Simple Shell Script}

Scripting languages are similar to batch files in DOS environment. 
A shell script is a sequence of UNIX commands in a text (ASCII) file.
The following sequence of commands creates a script file named first with one UNIX command:\\
\\

\hspace*{1 cm} {\em 	cd $\tilde{}$ /cs241/lab2}\\
\hspace*{1 cm} {\em vi first}\\
\hspace*{1 cm} {\em         man -k shell $|$ sort -u $|$  more}\\
(save and exit the file by pressing {\em ESC :wq})
\\
 
To execute it in bourne shell use: 
\\

\hspace*{1 cm} {\em sh first}
\\

For shell, {\em first} is not an executable program yet. To become one, the command
\\

\hspace*{1 cm} {\em chmod a+x first}
must be executed. Now,\\

\hspace*{1 cm} {\em ./first}
\\

executes the shell script as desired.

More complicated shell programs are discussed in lab3.

\section {Accessing the Files and Security Issues.}

The chmod command we used previously changes the access mode into a file. 
It is used to change the permission for the owner and other users to read, 
modify or execute the file. For example, if\\ 
\hspace*{1 cm} {\em ls -l}\\


displays\\


\hspace*{1 cm} {\em $-$$rw$$-$$r$$-$$-$$-$$-$$-$   1 userid cs241          82 Jun 30 17:11 temp}\\
\hspace*{1 cm} {\em $-$$rw$$-$r$-$$-$$-$$-$$-$   1 userid cs241         304 Jul 14 10:48 first}\\
\hspace*{1 cm} {\em $drwxr$$-$x$-$$-$x   2 userid cs241        4096 May 15 17:23 labs}\\

then the information about the files and their permissions is in the first column.

Each line of the first column starts either with '$d$' or '$-$'.
'$d$' means that the file is a directory, '$-$' denotes a regular file.

The next three characters describe the permission to the owner of the file. 
'$r$' means file is readable, '$w$' file can be modified (i.e. it is writable), 
'$x$', file is executable. 
Symbol '$-$' instead of '$x$' means that the user has no permission 
to execute the file, similarly for reading and modifying.
The next three characters, starting from fifth position, 
give the permission to a group including the owner. 
Membership of userid in the group cs241 is specified in columns 
three and four of the list. 
The last three characters of each line in column one describe 
the permission to all other users.\\   
For example to allow others to read your {\em first} script use:\\

\hspace*{1 cm} {\em chmod o$+$r first}\\

To remove that permission type:\\
\hspace*{1 cm} {\em chmod o$-$r first}.  



\section {Wildcards}
Wildcards may by used to specify a group of files. 
Here is a list of some most commonly used wildcards:
\begin{itemize}
\item  ? (question mark) matches any single character\\
{\bf example: }To list all dlx files needed for assignment 1 use:\\

\hspace*{1 cm} {\em ls a1q?.dlx}

\item * (asterisk) matches zero or more characters\\
\hspace*{1 cm}    be careful, . has no special meaning (except for ls command) and so,\\ 
        \hspace*{1 cm} {\em rm *}\\
 will remove everything without any warning or asking
 however, '.' is considered special for ls command:
\\
{\bf Example:}\\

\hspace*{1 cm} {\em ls a*.txt}\\
   
\item  $[$character-list$]$ matches any single character that appears in the list.
\\
{\bf Example:}\\
    \hspace*{1 cm} {\em [ab]}  means either a or b\\
    \hspace*{1 cm} {\em [a-z]} means any character between a and z, inclusive\\   
    \hspace*{1 cm} {\em ls test[12345].txt}\\
    or\\
    \hspace*{1 cm} {\em ls test[1-5].txt}\\

NOTE:  you will hear about regular expressions (REs) and their rules 
soon in class, in assignments and in lab3.  
Be aware, that *,?, $[$$]$ have a different meaning in shell and in 
rules used by RE. You will need to know and use both.

\end{itemize}


\section {C Shell Quoting}

It is important to know the meaning of different quotation characters in c shell.\\

\begin{itemize}

\item  {\em single quotes: ' }$-$$>$ turn off special meaning of all characters until 
the next single quote is found.\\
{\bf Example:}\\
	\hspace*{1 cm} {\em echo \$PATH}\\
	\hspace*{1 cm} {\em echo '\$PATH'}\\
	\hspace*{1 cm} {\em echo Hey, what's next? Mike's \#1 friend has \$value.}\\
      (output: 
          Hey, whats next? Mikes ) \\
	  Everything after \# is ignored, since \# starts comment in shell\\ 
       
\item  { \em double quotes: ''} $-$$>$ work almost like single quotes, 
but double quoting allows the char \\
         \hspace*{1 cm}  \$ \,(dollar sign),\\ 
        \hspace*{1 cm}  `\,(back-quote), \\
          \hspace*{1 cm}  $\backslash$  \,(backslash) \\
	  to keep their special meaning.\\
{\bf Example:}\\

      \hspace*{1 cm} {\em echo "Hey, what's next? Mike's \#1 friend has \$value."}
\\

\item  {\em pair of back-quotes: `}  does command substitution
\\
{\bf Example:}
 How to send an e-mail message to all the users logged on to the system.
\\
  First use the command \\
  \hspace*{1 cm} {\em who $|$ cut -c1-8}\\ 
      which will list all users currently log in.\\
  \hspace*{1 cm} {\em who $|$ cut -c1-8 $|$ sort -u}
    \\  will sort them in alphabetical order and will only display unique names\\
  \hspace*{1 cm} {\em mail ` who $|$ cut -c1-8 $|$ sort -u`}\\ 
      will send e-mail to all of them.\\
If you are not sure, how this command works 
(or you really do not want to send an e-mail to everybody) 
replace mail command with echo command:
\\

\hspace*{1 cm} {\em echo ` who $|$ cut -c1-8 $|$ sort -u`.}

\end{itemize}

\section{Standard Input and  Output}

\subsection{Standart Input}
{\bf Standart input} is
     the source of information for a command. 
     This is assumed to be the keyboard unless input is redirected or piped 
     from a file or another command.

\subsubsection{Redirecting Standard Input}

To redirect the standard input for a command use the $<$ (less than) 
character followed by the name of the input file. \\
For example:\\

 \hspace*{1 cm} {\em   mail bill $<$ memo}\\

This redirects the standard input to the mail command so that it comes from the file memo. 
The effect of this is to mail the contents of this file to the user
bill.


\subsection{Standard Output}

{\bf Standart output} is  the destination for information from a command. 
This is assumed to be the terminal display unless output is redirected or piped to a 
file or another
     command.

To redirect the standard output from a command use the $>$ (greater than) 
symbol followed by the name of the output file. If the file that you redirect
standard output to does not already exist it will be created. \\
For example:\\

  \hspace*{1 cm} {\em    grep Smith /etc/passwd $>$ popular}
\\

This redirects the standard output from the grep command so that it goes to the file popular. This file will contain all occurrences of the string Smith found in the /etc/passwd file.
\\
Note:
Redirecting standard output to a file that already exists overwrites its contents with the standard output.
\\
You can append output to an existing file.
%Appending standard output to a file

To append the standard output from a command to a file use $>>$ (two greater than) 
symbols followed by the file name. If the file does not exist it is created. 
For example:\\

 \hspace*{1 cm} {\em     cat part1 $>$ chapt2}\\
  \hspace*{1 cm} {\em    cat part2 $>>$ chapt2}\\

The first line creates a file called chapt2 with the same contents as part1. 
The second reads the contents of part2 and appends them to the file chapt2. The file
chapt2 contains now the data from part1 followed by the data from part2.



\section {Other Useful C Shell Commands} 

\begin {itemize}
\item {\em  history} \\
displays the list of previously used commands.\\ 
 Type\\
 
 \hspace*{1 cm} {\em     history}\\
 
to see last 100 commands used.
For example, to repeat command 123 use:\\

   \hspace*{1 cm} {\em   !123}\\
   
 to repeat the previous command use:\\
 
  \hspace*{1 cm} {\em    !! }\\

For other options see UNIX in a nutshell book.

\item  {\em alias} \\
will assign a name to the command.
For example, if one wants to use dir instead of ls then he/she must create an alias:
\\

 \hspace*{1 cm} {\em  	alias dir ls}    \# but please, do learn UNIX
\\

\item  job control 
\\

 \hspace*{1 cm} {\em  CTRL-Z}\\
 
      stops the execution of the program
  \\
  
   \hspace*{1 cm} {\em  jobs}\\
   
     lists all running or stopped jobs
  
   \hspace*{1 cm} {\em  kill ID}\\
   
     terminates specific process ID

   
{\bf Example:}\\

    Suppose we have a program ./myprogram running.
    To stop it type\\
    
     	 \hspace*{1 cm} {\em  CTRL-Z}\\
	 
    To see what programs are running or stopped type\\
    
    	 \hspace*{1 cm} {\em  jobs}\\
	 
    If you will see  \\
    
     \hspace*{1 cm} 	$[$1$]$  myprogram  stopped
   \\
   
    you can kill this process by\\
    
    \hspace*{1 cm} {\em  	kill \%1} 



\item  {\em finger users}

     displays data about one or more users\\
   {\bf Example:}\\
          \hspace*{1 cm} {\em  finger youruserid}\\
          \hspace*{1 cm} {\em  finger cs241}\\ 

 To change the information about yourself visible to everybody use: 
      \\

      \hspace*{1 cm} {\em    chfn}
\\

and follow the instructions on the screen.

\end{itemize}

As mentioned above, the information can be viewed by anyone. 
Be thus sure you only include the kind of information everybody can know.
 

You may also include additional information stored in the file .plan in your home directory.
 \\
 To create or change your .plan file type:\\
    \hspace*{1 cm} {\em  cd }   \hspace*{1 cm}  \# this will switch into your home directory\\
    \hspace*{1 cm} {\em  vi .plan}   \hspace*{1 cm}   \# opens the file \\ 
    \hspace*{1 cm} {\em Hello}  (some message)\\

 After saving the file try\\

   \hspace*{1 cm} {\em finger youruserid}\\

   You will see the message: {\em plan exists, but it is unreadable} 
   (on the screen) because the file .plan is not world readable. 
   To change  the permission type:\\
   
        \hspace*{1 cm} {\em  chmod a+r .plan}\\
	
   Where 'a' stands for "all" = user + group + other as explained earlier.

\end{document}

