#!/bin/sh -u # Demonstrate a "while" loop, "read", "exec", and "let" # Syntax: $0 (no arguments) # # Demonstrate the use of a "read" statement in a "while" loop. # Demonstrate the use of "exec" to open a file on standard input. # Demonstrate how to do simple arithmetic using "let". # # NOTE: This script fragment is incomplete - it does not follow all # the script guidelines set out in "script_style.txt". It also # contains many "Instructor-Style" comments that you must not use in # your own scripts. See script_style.txt for details. # # -IAN! idallen@idallen.ca # Execute the "who" command and save the output in a unique tmp file. # Quick-exit if the command fails for any reason. # tmp=/tmp/whoout$$ who >"$tmp" || exit 1 # Open the tmp file as standard input to the rest of the script. # (Any command between here and the end of the script that tries to read # standard input will read from this file.) # Quick-exit if the command fails for any reason. # exec <"$tmp" || exit 1 # Loop reading lines from standard input (from the file) until EOF. # Split the input lines into three pieces: userid terminal date # Count the lines as we read them. # Output the count and the userid and date of the login. # count=0 while read userid terminal date ; do let count=count+1 echo "$count. $userid logged in on $date" done # Remove the tmp file. # rm "$tmp" # Exit with success if we counted at least one user. # if [ $count -gt 0 ] ; then exit 0 fi exit 1 # bad status exit (no users found)