Introduction to Unix: mini-course modules

by Andrew Newman

Getting Started:

This course is broken up into several sections:
  1. Get Access to a UNIX/Linux machine. See Zhigang or me for an account on the geophysics system.
  2. Log in.
    • From Windows: Follow these instructions.
    • From Macs: Follow these instructions.
    • From UNIX/Linux: You're already on a suitable machine.. for getting on the Geophysics system, follow the Mac instructions above.
  3. Basic Introduction to Unix pdf presentation
  4. An Editor Primer (this page)
  5. Introduction to Shell Scripting (this page)
  6. Introduction to GMT shell scripts for figure generation

An editor primer: VI(M)

    vim is a very powerful Unix-based text editor that has been specifically developed to help in writing/manipulating/viewing computer code languages and data files. While there are other editors out there, this is still the standard by which most text editing is done in Unix.

    While vi is the default editor on older Unix machines, vim is the modern version of the editor on Linux machines. vim acts in much the same way as vi but has much more extensive functionality.

    To make the course more productive please spend about an hour with the following online tutorial if you are not already comfortable within vi(m).

    Here is a quick-reference chart of VIM functionality.

    Very briefly, there are two main modes within VI(M):

  • Command mode: This is the default mode of vi. From this mode, you can run a series of commands. Basics:
    • /[?] -- forward [reverse] search
    • :q[!] -- quit [forced]
    • :w -- write
    • h, j, k, l -- move left, down, up, right (arrows also work)
    • u -- undo
    • CNTRL^r -- redo
  • Insert mode: This is where you can simply type in text.
    • Arrows will work to move around, but not h,j,k,l
    • Use the _ESC_ key to return to command mode.
      Different ways to get into insert mode
    • i -- normal insert
    • I -- insert at beginning of line (BOL)
    • a -- append (start at next character)
    • o -- open a new line below
    • O -- open a new line above
    • r -- replace one character and return to command mode
    • R -- replace characters until _ESC_
    • c -- change word
    • C -- change rest of line
    • v -- visual replace
    • V -- visually replace select columns

Introduction to Shell Scripts:

    Shell scripts are simply a combination of command-line programs that are run sequentially. Shell are tremendously useful tools for performing repetitive or automated tasks on a system. They are also, wonderful for documenting even a one-time processes for future reference. You will find that shell scripting goes hand-in-hand with working on UNIX.

    Please look through this online tutorial to help get you started.

    My first shell script:

    	#!/bin/bash    
            # hello_world
            # version 1.0
            # Andrew Newman
    	# created Mon Sep 18 18:33:30 EDT 2006 
            echo "hello world"
    	exit 0 
  • The first line is a special 'comment' that tells the computer what type of shell the script should run in. Options include bash, csh, tcsh, sh.
  • The next 4 comment lines (lines that start with #), do nothing, but are useful to the reader of the script.
  • the echo command prints the next text to screen.
  • the exit command, when followed by 0, cleanly exits the program without an error. (not necessary, but good practice).

    Running the program:
  • Important: After creating the script file, it is imperative that you make it executable using the chmod program.
    % chmod ugo+x hello_world   #this will give execute permission to you, your group and others. 
  • Now, all you need to do to run the program is type 'hello_world' (this assumes that your path is set to allow this).
    %  hello_world 
    hello world
    Not very exciting, but works!

  • Using variables (defined with a '='), we can make the program more useful.
  • Once a variable is created, it can be called by preceding it with an $.
  • We can modify the script to declare who said hello, by calling a default shell variable $USER.
  • We can also call set a new variable $TIME as the output of the program date.
    	#!/bin/bash    
            # hello_world
            # version 1.1a
            # Andrew Newman
    	# created Mon Sep 18 18:41:59 EDT 2006
            TIME=`date`
    	echo "At $TIME, $USER said 'hello world'"
    	exit 0 

    We will work together on a script to search a GPS station list for stations within a certain range.
    Here is the final version of lfilesearch, which was built interactively.
    This was further modified, with additional options and renamed from nearbyGPS.

    A few example scripts (depricated)

    • define: a google-based dictionary
    • myradar: a program that calls up a browser to display the current default radar
    • myforecast: weather forecast using default or user-defined input (text if screen not available)

    Home | anewmangatech.edu | Updated: Thu Aug 1 09:19:44 EDT 2013