squeak!
Syllabus Homepage
Course Overview
Course resources
Day 1
Day 2
Day 3
Day 4
Common errors
Internet Glossary
About Your Instructor
Credits: This site powered by the vi text editor, apache webserver, perl scripting, and Debian linux.
squeak!

shell - Day 2

Goals

In this session you will:
  • learn the $# and $* vars
  • learn about sourcing files
  • learn shell flow control
  • use conditional testing
  • use math
  • use exitcodes

POST

  1. what does the shift command in a shell script do?

  2. log into your Solaris workstation
  3. make a directory called Scripts under your home directory
  4. move your shell scripts there (hint: wildcard). All your subsequent scripting will be done there.
  5. make a shell script named day1post.sh. Make sure you have a good magicline (hint: 1,1)
  6. make the script ask how many minutes it took to commute to UTA this morning, and display the inputted value.
  7. Now run it, and redirect the output to a file called day1post.out (note: why can't you see the promptstring?). Do it again, and append the output to the same file.

  8. Now run it, supplying the input via pipe.
  9. Now run it, supplying the input via an input file called day1post.in
  10. Now look back over your code in day1post.sh. Is it clearly presented, with adequate whitespace, comments, and meaningful variable names? Who can run your script? Is that what you want?
  11. While you relax and enjoy your coffee, make any stylistic changes that you noted in the previous step.
  12. look over the code written by your classmates. How well would you be able to troubleshoot, modify , or use their scripts? What does this tell you about your own scripts?

    $* and $#

    These two variables help you get info about the args passed to the script.

    learn about sourcing files

    . filename

    Note what happens to your vars...

    other ways to get vars into shells

    var1="value1" var2="value2" script

    ``

    learn shell flow control

    testing

    • exit codes
    • test condition
    • [ condition ] (spaces are critical, as [ is an alias for test)
    • see text for test switches

    if then - elif - else - fi

    if [ condition1 ]
    	then
    	do something
    	
    	elif [ condition2 ]
    		then	
    		do something
    
    	else
    		do something
    
    fi
    

    while

    while [ condition2 ]
    	do
    	do something
    	done
    

    while 1, while :

    for

    for var in list
    	do
    	do something with $var
    	done
    

    until

    until [ condition ] 
    	do
    	do something 
    	done
    

    switch/case

    case value in
    
    	pattern1|pattern1a) command;command;;
    	pattern2) command;command;;
    	pattern3) command;command;;
    	pattern4) command;command;;
    	*) command;command;;
    esac
    

    loop shortcuts

    • break bust out of current loop, perhaps more than one
    • continue force next iteration of current loop
    • exit exit current shell, perhaps with errorlevel

    Homework

    • command substitution
    • subshells
    • command stacking
    • intermediate redirection
    • debugging


    http://www.mousetrap.net/syllabus/shell/day2.html
    $Id: day2.orb,v 1.13 2002/11/25 13:21:31 mouse Exp $


© 1994-2002 jason carr.
distributed under the terms of the GNU Free Documentation License.

jason carr

Reminders

  • Classroom temperature can be wildly variable. Dress lightly and bring layers.
  • your username is based on the class title and the last two digits of your workstation's hostname.
  • remember to take your work with you.