4.6 Editor  -  vi4 Labs4.4 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)).

While you are doing this lab keep in mind that many of the following labs are shell scrips. What you learn in this lab will be used in other labs and has general application when using UNIX and/or Linux systems. It also has general application to Mac OS-X systems as well.

  1. Create the following shell script file called cmd using the pico, nano, or vi text editor:
    #!/bin/bash
    echo your-name
    echo TABER CIS137
    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:

    username@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:
    username@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. The example below assumes that you are running bash.
    username@gort ~ $ cmd 
    bash: ./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.
    username@gort ~ $ ls -l cmd 
    -rw-r--r--  1 ltaber    167 Jan 16 10:21 cmd 
    

    The permissions are shown as four (4) fields. The first is just a single character. The other three (3) are groups of three (3) bits.

    The first field has several options, but two (2) are very common. A hyphen "-" indicates a regular file and a "d" indicates a directory. (A folder in MicrosoftWindows terms.)

    The next three fields usually just show Read, Write and eXexute permissions for the user, group, and other. So the above long listing 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:

    username@gort ~ $ chmod a+x cmd
    

    The chmod command also takes octal arguments, for example:

    username@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.
    username@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. Run your shell script and check your output.
    username@gort ~ $ cmd
    
    Louis Taber
    TABER CIS137
    Lab 4.5: UNIX shell script
    pwd        >> /home/ltaber 
    who am i   >> ltaber pts/0        Aug 21 08:24 (:0.0) 
    id         >> uid=69(ltaber) gid=10(staff) 
    $HOME      >> /home/ltaber 
    
    username@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. Once is enough. UNIX keeps the existing protection bits. The location of the file will not change so a rehash is not necessary.

  8. 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:
      username@gort ~ $ cmd | lpr
      
    2. Printing a file This is a two step process. First create the file to print using re-direction, then print the file. The output redirection symbol is ">". The commands will look like this:
      username@gort ~ $ cmd > temp 
      username@gort ~ $ lpr temp
      

    You will be using the command to print quite a bit this semester. You may want to look at the output of the following commands to find out more. Both of the following commands are exited using the letter "q" (for quit).

    man lpr
    
    less /etc/printcap
    

    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 CIS137

Please turn your lab to Louis Taber or to Pima Community College employee in room A-115 of the Santa Rita Building. Ask them to place it in the dark blue folder in Louis Taber's mailbox.

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

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: Louis Taber, louis.taber.at.pima at gmail dot com (520) 206-6850
My web site in Cleveland, OH
The Pima Community College web site


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