#!/bin/bash # # CIS 130 Midterm # # Rich Simms # Spring 2007 # # This script is a ls command for directoreis # # usage: lsd [-dla] [path1] [path2] ... [pathn] # # path - path to the location of the directories # d - debug tracing # l - long listing # a - show hidden directories # # Initialize usage="lsd [-dla] [path1][path2] ... [pathn]" TRUE=1 FALSE="" trace=$FALSE long=$FALSE hidden=$FALSE result=0 # Process options while getopts ":dla" opt; do case $opt in d) trace=$TRUE ;; l) long=$TRUE ;; a) hidden=$TRUE ;; *) echo "-- Usage: " $usage >&2 exit 1 ;; esac done # Debug tracing to verify options [ $trace ] && echo "trace=$trace" [ $trace ] && echo "long=$long" [ $trace ] && echo "hidden=$hidden" #ls args: l=long, a=show hidden, L=show linked file types arg="lL" if [ $hidden ]; then arg="lLa"; fi # Shift out any options if [ $OPTIND -ge 1 ]; then shift $(($OPTIND - 1)); fi if [ $# -eq 0 ]; then path="$(pwd)"; fi if [ $# -gt 1 ]; then multiple=$TRUE; fi # Get first path path=$1 # If not path specified default to current directory if [ "$path" = "" ]; then path="$(pwd)"; fi while [ $path ] do [ $trace ] && echo "path=$path" # Check if it is a directory if ! [ -d $path ]; then echo "$path is not a directory." >&2 result=1 # Check for read access elif ! [ -r $path ]; then echo "$path: permission denied." >&2 result=2 # If more than one path, print path and indent, otherwise just list directories elif [ $multiple ]; then echo "$path:" if [ $long ]; then # Long listing of directories indented ls -$arg $path | grep '^d.*' | sed -e 's/\(^.*\)\( \)\(.*$\)/ \1\2\3/g' else # Just directory name indented ls -$arg $path | grep '^d.*' | sed -e 's/\(^.*\)\( \)\(.*$\)/ \3/g' fi else if [ $long ]; then # Long listing of directories ls -$arg $path | grep '^d.*' else # Short listing of directories ls -$arg $path | grep '^d.*' | sed -e 's/\(^.*\)\( \)\(.*$\)/\3/g' fi fi shift path=$1 done exit $result