1. script1 - Write a script that will test for three arguments. If those arguments are not present, prompt the user for those missing arguments. 2. script2 - Write a shell script that will loop through the arguments on the command line and display them to the screen only if they are UNIX files. 3. script3 - Write a shell script that will extract, sort and list all users' names on the screen. 4. script4 - Write a script that tells you whether the permissions for two file, whose names are given as arguments to the script are identical. If the permissions for the two files are identical, display the permission field; otherwise, display each file name followed by its permissions. 5. script5 - Write a script that takes the name of a directory as an argument and searches the file heirarchy at that specific directory for any files that have zero lengths. Write the names of any zero length files to standard output. 6. discover - Write a script to discover other IP devices on the local subnet. /home/cis130/simmsric/project $ cat script1 #!/bin/bash # # CIS 130 Final Project Scripts # # Rich Simms # Spring 2007 # # script1 - Test for three arguments. If those arguments are not present, # prompt for them. # # usage: script1 [-d] [arg1] [arg2] [arg3] # # options -d enables debug tracing # arg1, arg2, arg3 - any test arguments # # Initialize usage="script1 [-d] [arg1] [arg2] [arg3]" TRUE=1 FALSE="" # Process options trace=$FALSE while getopts ":d" opt; do case $opt in d) trace=$TRUE ;; *) echo "-- Usage: " $usage >&2 exit 1 ;; esac done # Shift out any options if [ $OPTIND -ge 1 ]; then shift $(($OPTIND - 1)); fi [ $trace ] && echo "$# arguments found on command line" for i in 1 2 3 do if [ $# -eq 0 ]; then echo -n "Enter argument $i: " read arg[$i] else arg[$i]=$1 fi shift done echo "arg1=${arg[1]}, arg2=${arg[2]}, arg3=${arg[3]}" exit 0 /home/cis130/simmsric/project $ cat script2 #!/bin/bash # # CIS 130 Final Project Scripts # # Rich Simms # Spring 2007 # # script2 - Test for three arguments. If those arguments are not present, # prompt for them. If they are are UNIX files then display them. # # usage: script2 [-d] [arg1] [arg2] [arg3] # # options -d enables debug tracing # arg1, arg2, arg3 - any test arguments # # Initialize usage="script2 [-d] [arg1] [arg2] [arg3]" TRUE=1 FALSE="" # Process options trace=$FALSE while getopts ":d" opt; do case $opt in d) trace=$TRUE ;; *) echo "-- Usage: " $usage >&2 exit 1 ;; esac done # Shift out any options if [ $OPTIND -ge 1 ]; then shift $(($OPTIND - 1)); fi [ $trace ] && echo "$# arguments found on command line" for i in 1 2 3 do if [ $# -eq 0 ]; then echo -n "Enter argument $i: " read arg[$i] else arg[$i]=$1 fi if [ -f ${arg[$i]} ]; then ls -l ${arg[$i]} fi shift done [ $trace ] && echo "arg1=${arg[1]}, arg2=${arg[2]}, arg3=${arg[3]}" exit 0 /home/cis130/simmsric/project $ cat script3 #!/bin/bash # # CIS 130 Final project Script3 # # Rich Simms # # # This script prints all user names in /etc/passwd to the screen. # # usage: script3 [-d] (where d = debug tracing on) # # Initialize usage="script3 [-d]" # Process debug option (-d) while getopts ":d" opt; do case $opt in d) debug="y" echo "** Debug tracing enabled" ;; *) echo "-- Usage: " $usage >&2 exit 1 esac done # Shift out any options if [ $OPTIND -ge 1 ]; then shift $(($OPTIND - 1)); fi tempfile="/tmp/tmp$$" #restore when Opus /tmp is fixed #tempfile="tmp$$" #and comment out this one > /tmp/tmp$$ while read inputline do login="$(echo $inputline | cut -d: -f1)" uid="$(echo $inputline | cut -d: -f3)" fulln="$(echo $inputline | cut -d: -f5)" if [ "$debug" = "y" ]; then echo "** trace: login=$login, uid=$uid, fulln=$fulln" fi if [ $uid -ge 99 ] && [ "$fulln" != "" ] ; then #ignore system uid's echo "$fulln" >> $tempfile fi done < /etc/passwd cat $tempfile | sort rm $tempfile exit 0 /home/cis130/simmsric/project $ cat script4 #!/bin/bash # # CIS 130 Final Project Scripts # # Rich Simms # Spring 2007 # # script4 - Test for files having identical permissions. If they are # identical, display the permission field otherwise print each # file name and their permissions. # # usage: script4 [-d] [file1] [file2] # # options -d enables debug tracing # file1, file2 - file names # # Initialize usage="script4 [-d] [file1] [file2]" TRUE=1 FALSE="" # Process options trace=$FALSE while getopts ":d" opt; do case $opt in d) trace=$TRUE ;; *) echo "-- Usage: " $usage >&2 exit 1 ;; esac done # Shift out any options if [ $OPTIND -ge 1 ]; then shift $(($OPTIND - 1)); fi [ $trace ] && echo "$# arguments found on command line" # Check for two arguments if [ $# -ne 2 ]; then echo "-- Usage: " $usage >&2 exit 1 fi # Check that they are file names file1=$1 if [ ! -f $file1 ] ; then echo "-- $file1 is not a file" exit 1 fi shift file2=$1 if [ ! -f $file2 ] ; then echo "-- $file2 is not a file" exit 1 fi # Get permissions of each file perm1=$(ls -l $file1 | cut -c2-10) perm2=$(ls -l $file2 | cut -c2-10) [ $trace ] && echo "perm1=$perm1" [ $trace ] && echo "perm2=$perm2" if [ "$perm1" = "$perm2" ] ; then echo "$perm1" else echo "$file1: $perm1" echo "$file2: $perm2" fi exit 0 /home/cis130/simmsric/project $ cat script5 #!/bin/bash # # CIS 130 Final Project Scripts # # Rich Simms # Spring 2007 # # script5 - Write a script that takes the name of a directory as an # argument and searches the file heirarchy at that specific directory for # any files that have zero lengths. Write the names of any zero length files # to standard output. # # usage: script5 [-d] [directory] # # options -d enables debug tracing # # Initialize usage="script5 [-d] [directory]" TRUE=1 FALSE="" # Process options trace=$FALSE while getopts ":dla" opt; do case $opt in d) trace=$TRUE ;; *) echo "-- Usage: " $usage >&2 exit 1 ;; esac done # Shift out any options if [ $OPTIND -ge 1 ]; then shift $(($OPTIND - 1)); fi [ $trace ] && echo "$# arguments found on command line" # Check for one arguments if [ $# -ne 1 ]; then echo "-- Usage: " $usage >&2 exit 1 fi # Check that argument is a directory directory=$1 if [ ! -d $directory ] ; then echo "-- $directory is not a directory" exit 1 fi # Loop through files in directory and print any with zero length cd $directory [ $trace ] && echo "*** Trace: We are now in: $(pwd)" [ $trace ] && echo "*** Trace: $(ls)" for i in * do if [ -f $i ] ; then length=$(ls -l $i | cut -d' ' -f6) [ $trace ] && echo "*** Trace: $i length=$length" if [ $length -eq 0 ] ; then # print file name ls -l $i | sed -e 's/\(^.*\)\( \)\(.*$\)/ \3/g' fi fi done exit 0 /home/cis130/simmsric/project $ cat discover #!/bin/bash # # CIS 130 Final Project Scripts # # Rich Simms # Spring 2007 # # discover - This script discovers local ip neighbors # on the local subnet. It does a broadcast then # dumps the arp cache. # # usage: discover [-d] (where d = debug tracing on) # # Initialize usage="discover [-d]" # Process debug option (-d) while getopts ":d" opt; do case $opt in d) debug="y" echo "** Debug tracing enabled" ;; *) echo "-- Usage: " $usage >&2 exit 1 esac done # Shift out any options if [ $OPTIND -ge 1 ]; then shift $(($OPTIND - 1)); fi x=`/sbin/ifconfig` bcast=${x#*eth0 *Bcast:} bcast=${bcast%% *} [ $debug ] && echo "Broadcast address for eth0: $bcast" [ $debug ] && ping -b -c 2 $bcast ping -b -c 2 $bcast > /dev/null 2> /dev/null /sbin/arp -a | cut -f1,2 -d' ' [ $debug ] && /sbin/arp -a exit 0 /home/cis130/simmsric/project $