CISC474, Unix Knowledge

See this page for an explanation of the purpose of this list, and how it relates to studying for exams.

Putting web pages on strauss/copland etc.

  1. (MIDTERM) Web pages go under the ~/public_html subdirectory.
  2. (MIDTERM) You need to do a chmod command to make them readable. A quick and dirty way to do that is with the following command, which recursively sets the permissions to the correct ones for every file under ~/public_html
       chmod -R 755 ~/public_html
    

Shell Scripting

  1. (MIDTERM) How to test the exit status of a program in a /bin/sh shell script:
             someUnixCommand -foo -bar -fum
             # now test the exit status of that command
    
             if [ $? -eq 0 ]
             then
               doSomethingBasedOnExitStatusBeingZero
             fi
    
    
    
  2. (MIDTERM) Making a shell script excecutable: To make a shell script executable, you need to set the "executable" bit. The following two commands will both work:
    chmod 700 script.sh
    chmod u+x script.sh
    
  3. (MIDTERM) First line of a shell script: The first line of a shell script specifies which shell is used to interpret that script. The most command and basic shell script is the Bourne Shell, called "sh", and usually found in /bin/sh. The syntax is as follows:
    #!/bin/sh
  4. (MIDTERM) Printing output. The "echo" command is used to print output on standard output. For example:
    echo "This script will now start up the web server"
  5. (MIDTERM) Assigning variables, dereferencing variables. You can assign a variable by simply listing the variable name (usually, as a matter of style, variables are in all capital letters) followed by an equals sign (the assignment operator) followed by the value (treated as a string). Note that there must not be any space on either side of the equals sign. Variables are often used to store the names of the full path to a utility. Variables are dereferenced with the $ operator. For example:
    CP=/bin/cp
     USER=pconrad
     CURL=/usr/local/bin/curl
     URL=http://copland.udel.edu/~$USER/cisc474
     echo "About to see if the web page $URL for user $USER is accessible"
     $CURL -f $URL 
     

  6. (MIDTERM> Redirecting standard output and standard error, using /dev/null, and storing return status. In the Bourne shell, you can redirect standard output with the syntax 1> and you can redirect standard error with the syntax 2>. In the example below, the first two lines set up variables to store the path to a utility called curl, and a URL to be retrieved. The third line runs the curl utility to retreive a web page, and stores the standard output of the curl command in a file called page.html, and stores the standard error messages in a file called page.log. The fourth line redirects both standard output and standard error to /dev/null, which means they should be discarded. In the latter case, since all output is discarded, the result of the operation is returned in a status code, which is retrieved by the expression $?.
    CURL=/usr/local/bin/curl
    URL=http://www.somehost.com
    $CURL $URL 1>page.html 2>page.log
    $CURL -f $URL 1>/dev/null 2>/dev/null
    CURL_RETURN_STATUS=$?
    
  7. (MIDTERM) Backtics: In a shell script, you can take the output of one command and "use it" as a value in an expression (e.g. assign it to a variable) by using so called "backtics". For example, the following executes the unix command "hostname", and stores the result in a variable called CURRENT_HOST:
    CURRENT_HOST=`hostname
    
    
    
  8. (MIDTERM) if tests: In the Bourne shell, and if test can be structured as follows. Note that the spaces after the [ symbol and before the ] symbol are required:
     if [ $RETURN_STATUS -eq 0 ] 
     then
        echo "Successful"
     else
        echo "Failure"
     fi
     
    Another option for the "then" part is to use a semicolon and put it on the same lines, as follows:
     if [ $RETURN_STATUS -eq 0 ] ; then
        echo "Successful"
     else
        echo "Failure"
     fi
     
    For the exam, you are not required to learn all of the operators that can be used in place of -eq, however however it is good to know -eq, -ne, -gt, -ge, -lt, -le. All of those compare numbers. The regular = and != operators also exist for comparing strings. The man page for "test" (i.e. man test) lists the full set of what can go inside the square brackets.

X11 and ssh topics

  1. (USEFUL) Free, open source, version of X11 for Microsoft Windows: (Useful for running Eclipse on strauss from home, for example, or using the mouse and menus in Emacs. Note that Mac OS X already has an X11 server included with the operating system if you install from the correct disks.)

    The latest version of cygwin (http://www.cygwin.com has a pretty good implementation of X11 for windows in it.

    The following might not be "optimal", but it worked for me:

    1. On the first pass, follow the directions to install cygwin; when you get to the screen for choosing packages, choose only the defaults. When done, click on the Cygwin icon that was installed, and you should now have a "bash" shell on your local Windows box. (You can install tcsh later if you want.)
    2. Once that is working, do a second pass for installing X11. Under "networking", choose to install OpenSSH and under X11, choose "install" for the entire tab. Finish up that install.
    3. You'll then have a "xterm" option under your start menu. However, if you double click it, nothing happens. Instead, you need to open your regular "local shell", and type "startx". This should fire up the X11 Server, and an xterm. In that xterm, you can run the command in the item below, either:
          ssh -X username@strauss.udel.edu
      
      or
          ssh -Y username@strauss.udel.edu
      
      to get logged onto strauss. (See "X11 forwarding on strauss", below, for more information.) Once on strauss, type
          xterm &
      
      to open up additional xterms.
  2. (USEFUL) X11 forwarding with ssh: If you already have an xterm open, and you have access to the command line "ssh" program (e.g. on Unix, a cygwin shell under Windows, or a Mac OS X shell), the following command will log you in with X11 forwarding. That way you can continue to use X-Windows applicaions on the new host you are logged into.
      
       ssh -X username@hostname
    
    Some programs may not work with the -X parameter, giving errors such as:
    X Error of failed request:  BadWindow (invalid Window parameter)
      Major opcode of failed request:  25 (X_SendEvent)
      Resource id in failed request:  0xa0000d
      Serial number of failed request:  88
      Current serial number in output stream:  89
    
    In that case, use so-called "trusted X11 forwarding", which is marginally less secure, but more backwards compatible.
       ssh -Y username@hostname 
    
    I do not have a good reference for the exact differences between -X and -Y and the security risk involved; I would welcome contributions on this point.

Phillip T Conrad
Last modified: Tue Feb 8 08:52:37 EST 2005