#!/bin/sh -u # Sample solution to script_practice1.txt #------------------------------------------------------------ # Syntax: # $0 [ filename ] #------------------------------------------------------------ # Purpose: # (See the full specifications in file script_practice1.txt ) # Expect 0 or 1 pathname argument. If no arguments, use "/etc/passwd". # Make sure the pathname is a file, is readable, and is not empty. # Count the characters in the file. If the count is <= 10, # append the name and count to an existing file in the current # directory whose name comes from "$USER". # Print error messages and exit non-zero on errors. #------------------------------------------------------------- # Student Name: Ian! D. Allen # Algonquin EMail Address: alleni # Student Number: 000-000-000 # Course Number: DAT2330 # Lab Section Number: 010 # Professor Name: Ian! D. Allen # Assignment Name/Number/Date: Sample Exercise. # Comment: This is a sample assignment label. # see http://teaching.idallen.com/assignment_standards.html #------------------------------------------------------------- # Step 1 - 2 marks - correct header (interpreter, PATH, and umask) # Set a standard search path and non-restrictive umask. # PATH=/bin:/usr/bin ; export PATH umask 022 # Step 2 - 7 marks # Exit the script if more than one argument is given. # if [ $# -gt 1 ]; then echo 1>&2 "$0: expecting one optional filename, found $# ($*)" exit 1 fi # Step 3 - 4 marks # Use the Unix password file if no arguments are given. # if [ $# -eq 0 ]; then file='/etc/passwd' else file="$1" fi # Make sure the pathname is a file, readable, and not empty. # Step 4 - 6 marks # if [ ! -f "$file" ]; then echo 1>&2 "$0: '$file' is missing, inaccessible, or not a plain file" exit 2 fi # Step 5 - 6 marks # if [ ! -r "$file" ]; then echo 1>&2 "$0: file '$file' is not readable" exit 3 fi # Step 6 - 6 marks # if [ ! -s "$file" ]; then echo 1>&2 "$0: file '$file' is empty (zero size)" exit 4 fi # Step 7 - 3 marks # Count the characters in the file using shell input redirection. # count=$( wc -c <"$file" ) # Step 8 - 7 marks # Issue a message if the character count of the file is over the limit. # (Note the use of a variable to hold and give a name to the constant.) # max=10 if [ "$count" -gt "$max" ] ; then echo "File '$file' contains $count characters (more than $max)" exit 6 fi # Make sure that a file named with your Algonquin userid exists. # Append the count of characters in the $file to the $out file. # Step 9 - 7 marks # out="$USER" # or (less flexible) just enter your actual abcd0123 userid if [ ! -e "$out" ]; then echo 1>&2 "$0: Output file '$out' is missing from current directory" exit 7 fi # Step 10 - 3 marks # echo "File '$file' contains $count characters" >>"$out"