4.6 Editor  -  vi4 Labs4.4 Editor 1 - About Me4.5 First Shell Script

4.5 First Shell Script

This lab introduces shell scripts, file protection bits, several commands, the pico (or vi) text editor, and the back-quote (or grave accent (or back-tick)).
  1. Create the following shell script file called cmd using the pico (or vi) text editor:
    #!/bin/tcsh
    echo your-name
    echo TABER CSC137
    echo Lab 4.5: UNIX shell script                     
    echo -n "pwd        >> "         
    pwd                               
    echo -n "who am i   >> "           
    who am i                            
    echo "id            >> "   `id`      
    echo '$HOME         >> ' $HOME        
    
    To do this use the command:
    gort:~>  pico cmd
    
    Please note that the "id" is surrounded by grave accents, not single quotes. Also note that the string:
    $HOME        >>  
    
    is quoted by single quotes, not grave accents.
  2. When you have finished, verify your work with the following command:
    gort:~>  cat cmd
    
    This command, cat, is short for catenate. It is the UNIX "type" command. It can also be used to concatenate files together. The output you get from this command should match the file shown above.
  3. Try to run your shell script. This should fail. It might fail in a different way. The example below assumes that you are running tcsh with spell-checking on. You might just get permission denied if you are running csh.
    gort:~>  cmd 
    CORRECT>mcd (y|n|e|a))?  n
    cmd: permission denied.
    
  4. Check the file protection bits. Use the ls command. This is the UNIX "dir" command. The -l is an option for the "long" format output.
    gort:~>  ls -l cmd 
    -rw-r--r--  1 ltaber    167 Jan 16 10:21 cmd 
    
    This is showing that I, the user, have read and write access. My group has read access. Everyone else also has read access. No one has eXecute permission.
  5. Change the protection bits. To give everyone eXecute permission, use the chmod command:
    gort:~>  chmod a+x cmd
    
    The chmod command also takes octal arguments, for example:
    gort:~>  chmod 755 cmd
    
    I find the symbolic names easier to remember.
    r    read
    w    write
    x    eXecute
    u    user
    g    group
    o    other
    a    all users (user, group, and other)
    +    add permission
    -    remove permission
    =    set permission
    
  6. Check the protection bits.
    gort:~>  ls -l cmd
    -rwxr-xr-x  1 ltaber          167 Jan 16 10:21 cmd 
    
    Note the new x's in the permission bits.
  7. If you are running csh, not tcsh, you will need to issue the rehash command. This looks "down" your search path for executable files and builds an internal hash table to speed up locating executable files.

    The c-shell (and tcsh) maintains an internal hash table of where executable programs are located on the path. If you add a new executable program, it will not be on your current list. The rehash rebuilds your hash table from scratch.

    With tcsh you only need to do this if the program is not in the current directory. With csh it always needs to be done.

    Because the hash table itself is inside the shell, the rehash command is an internal command, built into the shell. Because it is an internal command, you may need to look in the shell's manual pages to find out the details of the rehash command.

    The hash table keeps the system from re-searching the path for each program that is run, thus speeding up overall operation at the expense of complexity and storage.

    gort:~>  rehash
    
  8. Run your shell script and check your output.
    gort:~>  cmd
    
    Louis Taber
    TABER CSC137
    Lab 4.5: UNIX shell script
    pwd        >> /home/ltaber 
    who am i   >> gort!ltaber tty04 Jan 16 10:18 
    id         >> uid=69(ltaber) gid=10(staff) 
    $HOME      >> /home/ltaber 
    
    gort:~>  
    
    You may need to edit the shell script and try again. If you do, you will not need to set the eXecute bits a second time or issue the rehash command. Once is enough. UNIX keeps the existing protection bits. The location of the file will not change so a rehash is not necessary.
  9. Print out a copy of your results (when your output is correct). There are two ways to print out your results:
    1. Pipe your cmd's output into the print program. To do this, use the pipe symbol "|". The command will look like:
      gort:~>  cmd | lpr
      
    2. Create a temporary file by using re-direction. Then print the file. The output redirection symbol is ">". When used with an exclamation point, it can be used to over-write an exisiting file. (">!"). The commands will look like this:
      gort:~>  cmd >! temp 
      gort:~>  lpr temp
      
    The print command may be lp on your system. To look at the printer queue use the lpq command.
Turn in your output from step 4.5 marked as follows:
your-name
Lab 4.5: First Shell Script
TABER CSC137
Place the lab in the instructor hand-in box in BUS R6E, the "terminal room".

 
Several UNIX commands

Command

Usage & Function
cal Displays the current calendar.
cat Types a file to the terminal.
date Shows current date and time.
echo Echoes whatever is on the line.
lpr Prints a file on the line printer.
ls Lists a directory.
ls -l Lists a directory with permission bits.
id Shows your user id number and name,
  and your group id number(s) and name(s).
more A paging program.
pico A simple to use text editor.
pwd Shows the path to the current working directory.
tty Shows the communications port you are connected to.
vi The standard UNIX text editor.
w Shows who is using the system.
who Also shows the users that are logged on to the system.
who am i Displays information about you.

Special shell symbols

Symbol

Meaning
> Send output to a file
| Send output to another program
< Get input from a file
>> Append output to an existing file
& Run command in the background
; Used to separate two commands
|| Run second command if first fails
&& Run second command if first succeeds
>! Overwrite existing output file

UNIX String substitution

Grave accents

- Near the character "1" & "!" on most
or keyboards. Causes the enclosed string to be
Back-ticks passed to a sub-shell.
or The result is passed back to the original
Back-quotes shell with all new line characters removed.
  These can be nested with the proper use of
  escape characters.
environment variables When referenced in a shell script they need
  to be proceeded with the "$" character.
  When the variable is set no, "$" is used.
single quotes Prevents the shell from doing any substitution
  of the string. It is passed
  directly to the command.
double quotes Allows substitution of variables prior to
  passing the arguments to a command. Leaves
  spaces alone and passes it as a single
  argument.

Instructor: ltaber@pima.edu** My new Home on phRed** The Pima College Site** The Mad Dr. G.'s home page on phred.

4.6 Editor  -  vi4 Labs4.4 Editor 1 - About Me4.5 First Shell Script