Larger Text Normal Text Smaller Text

IT 280 Web Development II, Spring 2008

 

UNIX Shell Scripting

For background for this lesson and activity you should refer back to the first lab you did on using a command line interface. If you log in to wyrd, you can follow along with some of the commands in the lesson.

Unix manpages

Unix online reference manuals are called manpages.

Example

is the man page for the cal (calendar) command. (Note: The symbol <AD> is used like a hyphen to indicate continuation.)

The command used is "man cal"

 
[echang@wyrd csci599]$ man cal
Formatting page, please wait...date


CAL(1)                       UNIX Reference Manual                      CAL(1)

NAME
     cal - displays a calendar

SYNOPSIS
     cal [-mjy] [month [year]]

DESCRIPTION
     Cal displays a simple calendar.  If arguments are not specified, the cur<AD>
     rent month is displayed.  The options are as follows:

     -m      Display monday as the first day of the week.

     -j      Display julian dates (days one-based, numbered from January 1).

     -y      Display a calendar for the current year.

     A single parameter specifies the year (1 - 9999) to be displayed; note
     the year must be fully specified: ``cal 89'' will not display a calendar
     for 1989.  Two parameters denote the month (1 - 12) and year.  If no pa<AD>
     rameters are specified, the current month's calendar is displayed.

     A year starts on Jan 1.

     The Gregorian Reformation is assumed to have occurred in 1752 on the 3rd
     of September.  By this time, most countries had recognized the reforma<AD>
     tion (although a few did not recognize it until the early 1900's.)  Ten
     days following that date were eliminated by the reformation, so the cal<AD>
     endar for that month is a bit unusual.

HISTORY
     A cal command appeared in Version 6 AT&T UNIX.

BSD Experimental          nbsp;      June 6, 1993           nbsp;             nbsp;      1


It tells us that

Using man

Activity

Try the commands

echo-ing text

The echo command simply echos on the screen whatever you type on the line after it. For example, if you type "echo man ftp" the words are shown on the screen instead of being treated as a command.

[echang@wyrd echang]$ echo man ftp
man ftp
[echang@wyrd echang]$

If you type the following command

        [echang@wyrd man_pages]$ echo AN EXAMPLE OF COMMANDS 

The message AN EXAMPLE OF COMMANDS will appear on a line by itself

Punctuation characters

Many symbols, such as angle brackets (> and <) and the semicolon (;), have special meaning to the shell. If you want to echo them in text, you must place the text in quotation marks. For example,

      echo here > there
      echo me; you

will not echo the lines here > there and me; you. (Instead the first command will create a file named "there" and store the text "here" in it; the second will echo "me" and then display a shell error message.)

To echo the entire lines, you must use

      echo "here > there"
      echo "me; you"

Using special character codes in echo

Now suppose we want to put a blank line after the line "AN EXAMPLE OF COMMANDS ". But once you press ENTER (also called a carriage return), the command is completed.

Special characters which are "invisible" or have special meaning such as carriage returns, tabs and quotation marks are commonly encoded in commands and programming languages by a combination of "normal" characters - in Unix, a backslash followed by another character. This is referred to as "escaping" the character. Some frequently used codes are

  • \n - newline. When printed, it will start a new line.
  • \t - tab
  • \" - quotation mark

To use these character codes, you must use the -e option to tell the shell command interpreter to evaluate them as codes instead of as the ordinary characters. You also need to enclose the escaped codes in quotation marks.

example

echo -e "\n" creates extra blank lines between the first line, the date and the who report.

 
[echang@wyrd man_pages]$ echo AN EXAMPLE OF COMMANDS
[echang@wyrd man_pages]$ echo -e "\n"
[echang@wyrd man_pages]$ date
[echang@wyrd man_pages]$ echo -e "\n"
[echang@wyrd man_pages]$ who

Activity

Try the commands

  • echo HELLO
  • echo cal
  • echo "\n"
  • echo -e "\n"

Creating scripts

Echoing blank lines and messages onto the screen may seem strange, but there is a way to automate the process so that you can repeat an entire series of commands by typing a single line.

To create a script of shell commands:

  • You must store the commands in a file
  • then tell the shell to execute the commands in the file
  • The file is called a script.
  • The simplest way to to create the file is to use the text editor called pico.

Example, step 1

  • First type pico file_name at the shell prompt
  
[echang@wyrd man_pages]$ pico script1
  • The pico editor starts up and shows a blank for a new file with nothing in it.
  • At the bottom of the screen, the editor shows the commands it understands and the codes for invoking them
  • The symbol ^ means to hold down the control key while pressing the letter
 
   UW PICO(tm) 3.5                 File: script1
 
 
 
 
 
 
 

                                  [ New file ]
^G Get Help  ^O WriteOut  ^R Read File ^Y Prev Pg   ^K Cut Text  ^C Cur Pos
^X Exit      ^J Justify   ^W Where is  ^V Next Pg   ^U UnCut Text^T To Spell

Example, step 2

  • Type in the commands you want to execute.
  • The backspace (or delete, depending on how your telnet client is set up) key erases a character, and the arrow keys can be used to move around in the text.
  • Press ^X (Ctrl + X) to exit the pico editor.
 
   UW PICO(tm) 3.5                 File: script1                     Modified

echo An example script file
echo -e "\n"
date
echo -e "\n"
who

^G Get Help  ^O WriteOut  ^R Read File ^Y Prev Pg   ^K Cut Text  ^C Cur Pos
^X Exit      ^J Justify   ^W Where is  ^V Next Pg   ^U UnCut Text^T To Spell

Example, step 3

  • Tell the bash shell to execute the script with the command bash file_name.
 
[echang@wyrd ~]$ bash script1
An example script file


Wed Apr 19 11:25:52 EDT 2006


acrum    pts/0        Apr 17 09:17 (orchid.hood.edu)
echang   pts/1        Apr 18 14:47 (70-32-5-123.frdrmd.adelphia.net)

[echang@wyrd ~]$ 

Notice that all of the commands are executed.

File permissions

  • Unix is a multi-user operating system. You share wyrd with the other students in your class, and students from many other classes.
  • The system uses file permissions to control who can and cannot access your files in different ways.
  • Access is controlled at three levels: Owner (you), Group (users with whom you share a group) , Other (the rest of the world).
  • Each level may be granted or denied permission to Read, Write, or Execute the file.
  • File permissions are shown in long directory listings.

  • The first letter indicates if it is a directory.

Setting permissions - the chmod command.

  • You can set permissions from the command line using the chmod command.
  • One way to use it is to give the specific three-digit octal number that represents the permissions you want.
  • For example:
       chmod 755 my_filename
  • The code you will normally use is 755.
  • You can also set file permissions through the SSH file transfer window.
    1. right-click on the file name and choose "properties"
    2. check (or uncheck) the appropriate boxes.
      Missing image

Preparing a script for the web.

These instructions will let you call a script through a web server so its output can be seen in the browser.

  1. The FIRST TWO LINES in the script must be
    #!/bin/sh
    echo -e "Content-type: text/plain\n\n"
    
    Inportant: be sure that you do not indent the first line.
  2. The file MUST be saved with the special filename extension: .cgi For example,
    myscript.cgi
  3. The script file MUST be stored in your WWW directory or a subdirectory thereof.
  4. The permissions for the script file MUST be executable (use 755).
  5. The permissions for the script file AND the directory containing it MUST NOT be writable by group or other.
  6. To meet requirements 4 and 5, chmod 755 filename

Seeing the script output

  1. Prepare and save the script file.
  2. Check all six points listed above
  3. Determine the URL for the script (http://wyrd.hood.edu/~your_username/folder_names_if_used/script_file_name.cgi
  4. Enter the url into your browser

Example

Here is an example of a shell script that has been modified in this way.

[echang@wyrd WWW]$ cat script2.cgi
#!/bin/bash
echo -e "Content-type: text/plain\n"  
echo "================================="
date
finger
echo -e "\n"   
echo "This is who:"
who
echo -e "\n"
echo "================================="
echo "and this is a calendar:"
cal
echo "================================="
 

Here is a link to the example script. The URL is http://wyrd.hood.edu/~echang/script2.cgi

Lab/Homework Exercise 08

  1. Create (use pico) and run a script file which will do the following:
    • print out the date
    • echo the line "This is an example of a script"
    • skip a blank line
    • print out the users on the system
    • skip a blank line
    • print a calendar for the current month, with the weeks beginning on Monday
    • print out your name
  2. Run the script, using the bash command, to be sure it works.
  3. Modify the script to be callable over the web (add the initial two lines and change the file permissions)
  4. Test to see that it works.
  5. Put a link to the script file on your table of contents page
  6. Complete the Submission on Blackboard.
Comments to: chang@hood.edu
Last Modified: 25 March 2008. 11:12

 

Hood College Department of Computer Science: Course materials © 1997-2006 by Elizabeth Chang.