Unix (sh) Quick Reference

To send diagnostics to the standard error file, do:
	`echo ... 1>&2'

using test
	if test -f textfile;then        #returns true if file & !directory
	 echo database exists
	fi

unit separator
	BEGIN { FS = "c" }
		this will tell awk that 'c' is the word separator.  (very
		handy with the passwd file.  ie: ':')
printf
	awk '{printf "%-11s: %8d (%4s)\n","'$HOSTNAME'",$4,$5}'

awk loops
	awk 'BEGIN {
	  numquote='`head -1 $quotefile`'
	  ranquote=('$$' % numquote)+1
	  }
	  {
	  if ($1=="%%")
	    loop=loop+1
	  if (loop==ranquote)
	    if ($1!="%%")
	     print $0
	 }' $quotefile

input
	read VARIABLE

architecture query:
	arch

System Information (general)
  uname -a

Name
  hostname

Batch Processing
  nohup command &
  will background and release the command from the process

Protection
  for executing a shell script, the user needs to have rx protection.
  for executing a compiled program, the user only needs x protection.

Command-Line Arguments
  $n    the nth command line argument with $0 being the program pathname
  $#    the number of command line arguments
  $*    all command line arguments seperated by one space
  $@    all command line arguments seperated by one space

Command Protocol
  exec cmd      execute cmd and transfer control to that cmd destroying
		the invocation shell

Comparison Operators
  -eq   equal to
  =     equal to
  !     not

Algebra
  NUM=`expr 3 + 2 - $N`

Exit
  exit 1        exit fully out of a program

For
  Syntax
    for Variable in Set
     do
      commands
     done
  Variable
    is set to each object in the set in turn (ie: i)
  Set
    list of objects in order that are assumed by the Variable in turn
    (ie: 1 2 3 c d e)

If
  Syntax
    if [ flags arguments ];then
     commands
    else
     commands
    fi
  Flags
    -a  and?
    -d  return true if argument (directory) is present
    -f  return true if argument (file) is present
  Arguments
    =~  ( x$arg =~ x-* )

Output
  echo
    echo text   will output the text
    echo -n     output without \n

Quotes
  'text'        or "text" will return the text
  `cmds`        will be replaced with the executed command

Shell Execution
  #!/bin/sh     if this is the first line, it will run the program under sh

Text Processing
  "head -n filename" *or* "| head -n"   output the first n lines
  "tail -n filename" *or* "| tail -n"   output the last n lines

Variables
  FILE="file_path_name"
  VARIABLE=42

While
  while [ 1 ]; do
   commands
  done




foreach
	foreach arg ( $argv[2-] )
	end

	if ( $argv[1] == -p || $argv[1] == -P ) then

	if ( `whoami` != root ) then
	 echo "You must be root to run $0"
	 exit(1)
	endif

	if ( $#argv < 1 ) then
	 goto usage
	endif

	if ( $?PrivateMode ) then
while
	while [ -n "$1" ]; do
	 case "$1" in
	  -d) DEBUG=-YES-; shift;;
	  -h) SETHOSTNAME=-YES-; shift;;
	  *) iflist="$*"; break;;
	 esac
	done


----line-by-line reading
I just realized you probably want something simpler.

	NEWSTATUS=0
	while [ ":0:" = ":$NEWSTATUS:" ]
	do
	    read newline
	    NEWSTATUS="$?"
	done < wkmotd

NEWSTATUS is the status of the read command; 0 is good
wkmotd is the file we want to read
The colons are just there in case NEWSTATUS doesn't exist so that
the string won't be empty

newline gets the next line of text.
--------

#!/bin/sh

SEPARATOR='**********************************************************************'

ARTICLE=''
STATUS=0
COUNT=1
STUPIDMATCH=0

while [ 0 -eq "$STATUS" ]
do

    read line
    STATUS="$?"
    
    if [ "$SEPARATOR" = "$line" ]
    then
	NEWSTATUS=0
	NEWARTICLE=''
	NEWCOUNT=1
	STUPIDMATCH="0"
	while [ "$MATCH : 0" = "$MATCH : $NEWSTATUS" ]
	do
	    export STUPIDMATCH
	    STUPIDMATCH=0
	    read newline
	    NEWSTATUS="$?"
	    
	    if [ "$SEPARATOR" = "$newline" ]
	    then
	
		if [ "$ARTICLE" = "$NEWARTICLE" ]
		then
		    STUPIDMATCH="1"
		    while [ 0 -eq "$NEWSTATUS" ]
		    do
			read newline
			NEWSTATUS="$?"
		    done
		else
		    NEWCOUNT=`expr $NEWCOUNT + 1`
		    NEWARTICLE=''
		fi
	    else
		NEWARTICLE="${NEWARTICLE}\n${newline}"
	    fi
    
# you've gotta be joking.  This is ludicrious -- part 1
	    echo $STUPIDMATCH >/tmp/stupidmatch
	
	done < wkmotd
    
# you've gotta be joking.  This is ludicrious -- part 2
	read STUPIDMATCH </tmp/stupidmatch

	if [ "0" = "$STUPIDMATCH" ]
	then
	    echo "The following is in motd but not in wkmotd:\n${ARTICLE}"
	fi
    
	ARTICLE=''
	COUNT=`expr $COUNT + 1`
    
    else
	ARTICLE="${ARTICLE}\n${line}"
    fi

done < motd